mercredi 27 avril 2016

Variadic template and template template

I try to write the small template function make to facilitate the construction of some functors following the same structure as Functor. Works fine as follows for a functor with one argument:

template <class ARG1>
struct Functor{
    Functor(ARG1 x){ }
};

template <template <class> class FCT, class ARG>
FCT<ARG> make(ARG arg){
    return FCT<ARG>(arg);
}

void main(){
    int a = 5;
    make<Functor>(a);
}

Now I try to extend make to use it for functors with any number of arguments, for example the now two argument functor Functor:

template <class ARG1, class ARG2>
struct Functor{
    Functor(ARG1 x, ARG2 y){ }
};

template <template <class, class...> class FCT, class... ARG>
FCT<ARG...> make(ARG... arg){
    return FCT<ARG...>(arg...);
}

void main(){
    int a = 5;
    double b = 6;
    make<Functor>(a, b);
}

That does not work any longer, compiler says:

basic.cpp(199): error: no instance of function template "make" matches the argument list argument types are: (int, double)

Honestly, I have no idea what's wrong here. I do not see a conceptional difference to the first example. What do I need to do to make it work?

Aucun commentaire:

Enregistrer un commentaire