您好,登錄后才能下訂單哦!
編譯: g++ tuple.cpp -std=c++14
c++14支持函數返回auto的返回類型,c++11需要返回類型后置。
由于 get<N>(tuple)中的N必須是常量,用普通的循環遍歷不了,tuple的特性很多是在編譯期
完成的。
#include <iostream>
#include <tuple>
using namespace std;
template<typename T, size_t N>
struct RTuple
{
static auto reverse(T& tup)
{
return move( tuple_cat( make_tuple(get<N-1>(tup)), RTuple<T,N-1>::reverse(tup)) );
}
};
template<typename T >
struct RTuple<T, 1>
{
static auto reverse(T& tup)
{
return move( make_tuple(get<0>(tup)) );
}
};
template<typename T>
struct RTuple<T,0>
{
static auto reverse(T& tup)
{
return move(tup);
}
};
int main(int argc, char* argv[])
{
auto t1 = make_tuple(1,2,3,4);
auto in1 = RTuple<decltype(t1),4>::reverse(t1);
cout<<get<0>(t1)<<",";
cout<<get<1>(t1)<<",";
cout<<get<2>(t1)<<",";
cout<<get<3>(t1)<<endl;
cout<<get<0>(in1)<<",";
cout<<get<1>(in1)<<",";
cout<<get<2>(in1)<<",";
cout<<get<3>(in1)<<endl;
tuple<> t2;
auto size = tuple_size<decltype(t2)>::value;
cout<<size<<endl;
//if ( size > 0 )
// cout<<get<0>(t2)<<endl;
auto in2 = RTuple<decltype(t2),0>::reverse(t2);
size = tuple_size<decltype(in2)>::value;
cout<<size<<endl;
//if ( size > 0 )
// cout<<get<0>(in2)<<endl;
return 0;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。