Vincent
Vincent

Reputation: 60341

Failing at deducing variadic template parameter?

What is the fundamental reason that explains that the following test case fails to compile :

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

template<typename Type, typename... Args> 
void apply(std::vector<Type> &v, Args... args, void(*algo)(Type*, Type*, Args...))
{
    algo(&*v.begin(), &*v.end(), args...);
}

int main()
{
    std::vector<int> v(10, 50);
    apply(v, 3, std::iota);
    for (unsigned int i = 0; i < v.size(); ++i) {
       std::cout<<v[i]<<std::endl;
    }
}

Is there a workaround for the function prototype ?

Upvotes: 1

Views: 108

Answers (1)

Wug
Wug

Reputation: 13196

The first problem is, as the compiler error states:

parameter packs must be at the end of the parameter list.

In other words, you must declare your function so Args ... args is the last argument in the list.

Also, I don't believe the compiler will infer the type of a template function that uses template templates the way you use them, so you'll have to explicitly specify the template:

apply<int, int>(v, std::iota, 3); // or something

Ideone of your snipped with proposed modifications

Upvotes: 2

Related Questions