Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c++ - Variadic variadic template templates

I'm currently struggling with the following code, the intent of which is to implement variadic variadic template templates:

template
<
  template <typename... HeadArgs> class Head,
  template <typename... TailArgs> class...
>
struct join<Head<typename HeadArgs...>, Head<typename TailArgs...>...>
{
  typedef Head<typename HeadArgs..., typename TailArgs......> result;
};

Ideally, I'd be able to use this template metafunction to achieve the following:

template <typename...> struct obj1 {};
template <typename...> struct obj2 {};

typedef join
<
  obj1<int, int, double>, 
  obj1<double, char>,
  obj1<char*, int, double, const char*>
>::result new_obj1;

typedef join
<
  obj2<int, int, double>, 
  obj2<double, char>,
  obj2<char*, int, double, const char*>
>::result new_obj2;

/* This should result in an error, because there are 
   different encapsulating objects
typedef join
<
  obj1<int, int, double>, 
  obj1<double, char>,
  obj2<char*, int, double, const char*>
>::result new_obj;
*/

The output of the above would hopefully create new_obj1 and new_obj2 in the form template<int, int, double, double, char, char*, int, double, const char*> struct new_obj[1|2] {};

I'm using gcc 4.6.2 on Windows, which outputs an "expected parameter pack before '...'" for the expansion of "Head<typename TailArgs...>...".

This error is reproducable with gcc 4.5.1.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:

template <typename...> struct join;

template <template <typename...> class Tpl,
          typename ...Args1,
          typename ...Args2>
struct join<Tpl<Args1...>, Tpl<Args2...>>
{
    typedef Tpl<Args1..., Args2...> type;
};

template <template <typename...> class Tpl,
          typename ...Args1,
          typename ...Args2,
          typename ...Tail>
struct join<Tpl<Args1...>, Tpl<Args2...>, Tail...>
{
     typedef typename join<Tpl<Args1..., Args2...>, Tail...>::type type;
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...