user11914177
user11914177

Reputation: 965

Problems with template <typename ...Args>

I try to make a function that will run a for loop on multiple threads. To make the function better to use with all sorts of functions that should be used in the for loop I decided to use templates. For the return type of each function I used a simple:

template <typename T>

That is working without any problems. But for the arguments that the function may need I use this template construction:

template <typename ...Args>

So now my code looks something like this:

template <typename T, typename ...Args>
T threadedFor(T func(Args... args), Args... args, int nThreads, unsigned int max, unsigned int min = 0) {
    T result;
    /* ... */
    return result;
}

The problem now is that I don't know how to pass in the Args... args in my function call. I tried several methods, like:

threadedFor(function(1), 4, 5, 0);

or this:

threadedFor(function, 1, 4, 5, 0);

Assuming my arguments here are 1.

Also it would be nice to know what the name of this construction typename ...Args is, since I couldn't find something on google...

Upvotes: 0

Views: 1412

Answers (1)

user11914177
user11914177

Reputation: 965

Thanks to "NathanOliver- Reinstate Monica" I found the name of this construct and also found a solution to my problem. A parameter package must always be the last argument of a function. So changing my function like this:

template <typename T, typename ...Args>
T threadedFor(T func(Args... args), int nThreads, unsigned int min, unsigned int max, Args... args) {

solves the problem.

Upvotes: 2

Related Questions