//遍歷輸出tuple元素的簡潔方式(C++11) //Win32Con17_VS2017_01.cpp #include #include using namespace std; template void myprint_impl(tuple tup) //泛化版本 { //cout (tup... ...
//遍歷輸出tuple元素的簡潔方式(C++11) //Win32Con17_VS2017_01.cpp #include <iostream> #include <tuple> using namespace std; template<typename... Args> void myprint_impl(tuple<Args...> tup) //泛化版本 { //cout << get<0>(tup) << ((tuple_size<decltype(tup)>::value > 1) ? ", " : ""); //cout << tup._Myfirst._Val << ((tuple_size<decltype(tup)>::value > 1) ? ", " : ""); cout << tup._Myfirst._Val << ((tup._Mysize > 1) ? ", " : ""); //輸出tup的頭1項 myprint_impl(tup._Get_rest()); //對除頭1項之外的tup遞歸調用 } template<> void myprint_impl(tuple<> tup) //終止條件 { cout << endl; } int main() { auto t = make_tuple(3, 4.67, "Hello", true); myprint_impl(t); //3, 4.67, Hello, 1 system("pause"); return 0; }