Re: [問題] C++ 11 tuple 的轉換
※ 引述《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
07/30 17:02, 2F
→
07/30 17:02, , 3F
07/30 17:02, 3F
推
07/30 18:07, , 4F
07/30 18:07, 4F
→
07/30 18:07, , 5F
07/30 18:07, 5F
→
07/30 18:57, , 6F
07/30 18:57, 6F
→
07/30 19:55, , 7F
07/30 19:55, 7F
→
07/30 20:08, , 8F
07/30 20:08, 8F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):