2011-08-05 81 views
2

我想拿出一個通用模板來合併一個元組中的元組或列表的類型列表。編譯時出現錯誤。C++ 0x合併元組

template<typename... Types> 
struct merge_tuples{ typedef std::tuple<> type;}; 

template<typename Type> 
struct merge_tuples<Type> { 
    typedef std::tuple<Type> type; 
}; 

template<typename...Types> 
struct merge_tuples<std::tuple<Types...>> { 
    typedef std::tuple<Types...> type; 
}; 

template<typename...Types1,typename...Types2> 
struct merge_tuples<std::tuple<Types1...>,std::tuple<Types2...>> { 
    typedef std::tuple<Types1...,Types2...> type; 
}; 

template<typename...Types1,typename...Types2,typename...Rest> 
struct merge_tuples<std::tuple<Types1...>,std::tuple<Types2...>,Rest...> { 
    typedef decltype(merge_tuples<std::tuple<Types1...,Types2...>,decltype(merge_tuples<Rest...>::type)>::type) type; 
}; 

template<typename...Types1,typename Type> 
struct merge_tuples<std::tuple<Types1...>,Type> { 
    typedef std::tuple<Types1...,Type> type; 
}; 
typedef std::tuple<int,double> int_double; 
typedef merge_tuples<int_double,int_double,int_double>::type megred; 

我得到的錯誤是以下

lalr_grammar.cpp:152:97: error: unexpected type name 'type': expected expression 
    typedef decltype(merge_tuples<std::tuple<Types1...,Types2...>,decltype(merge_tuples<Rest...>::type)>::type) type; 
                           ^
lalr_grammar.cpp:420:11: note: in instantiation of template class 'merge_tuples<std::__1::tuple<int, double>, std::__1::tuple<int, double>, std::__1::tuple<int, double> >' requested here 
    typedef merge_tuples<int_double,int_double,int_double>::type megred; 
+3

什麼錯誤?當你想要答案時,你爲什麼不給它,讓我們更難? – Nobody

+1

'std :: tuple_cat'已經做了你想做的事情嗎? (我不確定什麼'merge_tuples'實際上)。 –

+0

merge_tuples將從元組或類型列表中創建一個新的元組 tuple_cat似乎沒有做我想要的東西我會研究它agian – James

回答

1

感謝您的意見霍華德Hinnant(欣南特) 我把你的意見和做一些修改和它的作品

template<typename... Types> 
struct merge_tuples{ 
    typedef std::tuple<Types...> type; 
}; 

template<> 
struct merge_tuples<> { 
    typedef std::tuple< > type; 
}; 

template<typename Type> 
struct merge_tuples<Type> { 
    typedef std::tuple<Type> type; 
}; 

template<typename...Types> 
struct merge_tuples<std::tuple<Types...>> { 
    typedef std::tuple<Types...> type; 
}; 

template<typename...Types1,typename...Types2> 
struct merge_tuples<std::tuple<Types1...>,std::tuple<Types2...>> { 
    typedef std::tuple<Types1...,Types2...> type; 
}; 

template<typename Type1,typename Type2> 
struct merge_tuples<Type1,Type2> { 
    typedef std::tuple<Type1,Type2> type; 
}; 

template<typename Type,typename... Types> 
struct merge_tuples<Type,std::tuple<Types...>> { 
    typedef std::tuple<Type,Types...> type; 
}; 

template<typename... Types,typename Type> 
struct merge_tuples<std::tuple<Types...>,Type> { 
    typedef std::tuple<Types...,Type> type; 
}; 

template<typename...Types,typename Type,typename... Rest> 
struct merge_tuples<std::tuple<Types...>,Type,Rest...> { 
    typedef typename merge_tuples<Rest...>::type temp; 
    typedef typename merge_tuples<std::tuple<Types...,Type>,temp>::type type; 
}; 

template<typename Type,typename... Types,typename... Rest> 
struct merge_tuples<Type,std::tuple<Types...>,Rest...> { 
    typedef typename merge_tuples<Rest...>::type temp; 
    typedef typename merge_tuples<std::tuple<Type,Types...>,temp>::type type; 
}; 

template<typename...Types1,typename... Types2,typename... Rest> 
struct merge_tuples<std::tuple<Types1...>,std::tuple<Types2...>,Rest...> { 
    typedef typename merge_tuples<Rest...>::type temp; 
    typedef typename merge_tuples<std::tuple<Types1...,Types2...>,temp>::type type; 
}; 

template<typename Type1,typename Type2,typename... Rest> 
struct merge_tuples<Type1,Type2,Rest...> { 
    typedef typename merge_tuples<Rest...>::type temp; 
    typedef typename merge_tuples<std::tuple<Type1,Type2>,temp>::type type; 
};