c ++ 0x合并元组

时间:2011-08-05 13:57:53

标签: c++11 tuples

我正在尝试提出一个通用模板,用于在一个元组中合并元组或类型列表。我在编译时遇到错误。

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;

1 个答案:

答案 0 :(得分:1)

感谢Howard 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;
};