开发者

c++0x merging tuples

开发者 https://www.devze.com 2023-03-26 08:15 出处:网络
I am trying to come up with a generic template for merging a list of tuples or types in one tuple. I am getting an error when compiling.

I am trying to come up with a generic template for merging a list of tuples or types in one tuple. I am getting an error when compiling.

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,t开发者_StackOverflowypename...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;

The error I am getting is the following

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;


Thanks for the advice Howard Hinnant I took your advice and made some additional changes and it works

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;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消