dimanche 17 mai 2015

Deduce template parameter pack from function call

I have the following code, where I have a template class, and a type in it, which I would like to use in a separate template function.

template <typename... Types>
struct MyClass
{
    enum SomeEnum { value0 = -1 };
};

template <typename... Types>
struct OtherClass
{
};

template <typename T, typename... Types>
T check(typename MyClass<Types...>::SomeEnum value) 
{
    OtherClass<Types...> obj;
    T result;
    // calculate result from obj;
    return result;
}

int main() {
    auto value = MyClass<int, bool>::value0;
    // ... 
    int t = check<int>(value);
}

I tought that the compiler will be able to deduce the parameter pack from the function call, so I can use it in the function template also. Unfortunately the compiler (gcc 4.8.2) can't deduce it:

$ g++ -std=c++11 op.cpp

op.cpp: In function ‘int main()’:
op.cpp:25:27: error: cannot convert ‘MyClass<int, bool>::SomeEnum’ to ‘MyClass<>::SomeEnum’ for argument ‘1’ to ‘T check(typename MyClass<Types ...>::SomeEnum) [with T = int; Types = {}; typename MyClass<Types ...>::SomeEnum = MyClass<>::SomeEnum]’
   int t = check<int>(value);

Is there a solution to "transfer" the template parameter pack to the template function?

Aucun commentaire:

Enregistrer un commentaire