Re: [問題] C++ 11 tuple 的轉換

看板C_and_CPP作者 (inevitable......)時間9年前 (2014/07/30 16:44), 編輯推噓3(305)
留言8則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《johnjohnlin (LYSin8)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : C++11 : gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) <-- 這應該沒差吧 : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : N/A : 問題(Question): : 想問一下有沒有可能把 tuple type 全部經過一個轉換 : 例如把一個 tuple 的 cv-qualifier 都拿掉 : 像這樣 : std::tuple<const int, const float, double> : 把每個元素都使用 std::remove_cv : 轉換之後應該會變成 std::tuple<int, float, double> : 錯誤結果(Wrong Output): : 不會寫 QQ : 查了一些資料應該要用 parameter pack 實作 : 或許跟這個寫的方法類似? : http://goo.gl/Jvnp0o : 不過我對這個語法不熟,沒辦法類推出要怎麼寫 不知道是否是你要的 #include <tuple> #include <iostream> template <typename T> struct remove_tuple_cv; template <typename ...Types> struct remove_tuple_cv<std::tuple<Types...>> { typedef std::tuple<typename std::remove_const<Types>::type...> type; }; int main(void) { std::tuple<const int, const char> a; //std::get<0>(a) = 2; // error: assignment of read-only location //std::get<1>(a) = 'c'; // error: assignment of read-only location remove_tuple_cv<decltype(a)>::type x; std::get<0>(x) = 2; std::get<1>(x) = 'c'; std::cout << std::get<0>(x) << std::get<1>(x) << std::endl; std::get<0>(x) = 1; std::get<1>(x) = 'd'; std::cout << std::get<0>(x) << std::get<1>(x) << std::endl; } Output: 2c 1d Compiler: g++4.8.2 -std=c++11 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.129.1.231 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1406709859.A.11E.html

07/30 17:00, , 1F
是的,太感謝了
07/30 17:00, 1F

07/30 17:02, , 2F
template <typename T> struct remove_tuple_cv;
07/30 17:02, 2F

07/30 17:02, , 3F
↑想請問一下為什麼要這行?
07/30 17:02, 3F


07/30 18:07, , 5F
recursive basecase
07/30 18:07, 5F

07/30 18:57, , 6F
partial specialization 之前要有primary template
07/30 18:57, 6F

07/30 19:55, , 7F
樓上正解
07/30 19:55, 7F

07/30 20:08, , 8F
發現樓上就是原 PO, 傻了www
07/30 20:08, 8F
文章代碼(AID): #1JsB1Z4U (C_and_CPP)
文章代碼(AID): #1JsB1Z4U (C_and_CPP)