Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic template deduction in variadic template template

I am not sure if the title makes much sense, but the example is actually quite staightforward:

// A converter struct with a generic constructor.
template <template <typename ...> class TT>
struct converter
{
        template <typename ... Args>    
        converter(const TT<Args ...> &);
};

// A few class templates.
template <typename T>
struct foo {};

template <typename T, typename U>
struct foo2 {};

template <typename ... Args>
struct foo_variadic {};

template <typename Arg0, typename ... Args>
struct foo_variadic2 {};

int main()
{
        // All these compile.
        converter<foo>(foo<int>{});
        converter<foo2>(foo2<int,double>{});
        converter<foo_variadic>(foo_variadic<>{});   
        converter<foo_variadic>(foo_variadic<int>{});
        converter<foo_variadic>(foo_variadic<int,double>{});
        // This will not compile.
        // converter<foo_variadic2>(foo_variadic2<int>{});                     
}

I have tried with GCC 4.8.1 and clang 3.3, the error messages vary a bit but they all point to some problem deducing Args around line 5 (and subsequent exclusion of the converter constructor from the candidate list).

How is foo_variadic2 special or different with respect to the other foos?

(For the record, I am trying to implement an is_instance_of type trait to detect instances of template classes)

UPDATE

Now both GCC 4.8.3 and 4.9.1 on my setup accept this. clang 3.4.2 still barking.

like image 220
bluescarni Avatar asked Sep 10 '13 16:09

bluescarni


1 Answers

This is not the solution, but might help you or others to figure out what the problem is. The following compiles:

template <template <typename ...> class TT>
struct converter2
{
    template <typename Arg0, typename ... Args>    
    converter2(const TT<Arg0, Args ...> &);
};

// ...

converter2<foo_variadic2>(foo_variadic2<int>{});

I have to admit I don't understand why this is necessary and why your code doesn't work.

like image 130
Daniel Frey Avatar answered Nov 15 '22 21:11

Daniel Frey