编译: 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&lt;&gt; t2;
auto size = tuple_size&lt;decltype(t2)&gt;::value;
cout&lt;&lt;size&lt;&lt;endl;
//if ( size &gt; 0 )
//    cout&lt;&lt;get&lt;0&gt;(t2)&lt;&lt;endl;

auto in2 = RTuple&lt;decltype(t2),0&gt;::reverse(t2);
size = tuple_size&lt;decltype(in2)&gt;::value;
cout&lt;&lt;size&lt;&lt;endl;
//if ( size &gt; 0 )
//    cout&lt;&lt;get&lt;0&gt;(in2)&lt;&lt;endl;
return 0;

}