21

I am looking for something like that:

template< typename T>  
void func(T t)
{
}

template< typename... Parms> 
void anyFunc( Parms... p)
{
    func<Parms>(p)... ;  //error 
    func(p)... ;         //error 
}

If the parameter pack expansion is done inside another function call it works:

template< typename T>
int some(T t)
{}

template< typename... Parms>
void func(Parms ...p)
{}

template< typename... Parms>
void somemore(Parms... p)
{
   func( some(p)...);
}

int main() 
{
 somemore(1,2,3,4,10,8,7, "Hallo");
}

The parameter pack expansion will also work for a list of base class initializers.

Is there any solution which will also work for functions which will return 'void'. The above workaround will not, while using the function calls returning void inside a parameter list could never work.

Any ideas?

1
  • Hacky solution would be to combine the function call with comma operator (so the pack expansion pattern doesn't yield void) and use some kind of dummy function that just "eats" its arguments. Something like this. Doubt it's the thing you wanted, though. :)
    – Vitus
    Aug 4, 2011 at 15:12

2 Answers 2

10

Unfortunately, as you noticed, expanding a parameter pack is only valid in certain contexts where the parser expects a comma-separated list of entries – contexts where the comma is just a syntactic separator, not the comma operator. This is arguably a deficiency in the current text.

An ugly workaround:

func((some(p), 0)...);

Do note that the evaluation order of function arguments, and thus the order of the some invocations, is unspecified, so you have to be careful with any side effects.

3
  • It is easy to get the functions evaluated in the correct order by using the pack expansion in a initializer list. Attention: g++ 4.8.1 has a bug and evaluates backwards!
    – Klaus
    Sep 7, 2013 at 8:17
  • 2
    What is the purpose of , 0?
    – AaronBueno
    May 25, 2014 at 21:19
  • 4
    @Peregring-lk: to make the expression under ellipsis (some(p), 0) have a non-void type—this is required, because you need a valid non-void type to pass into the helper function func.
    – liori
    Aug 30, 2014 at 20:26
6

How about a small helper class:

template <typename Func, typename A, typename ...Args> struct Caller
{
  static void call(Func & f, A && a, Args && ...args)
  {
    f(std::forward<A>(a));
    Caller<Func, Args...>::call(f, std::forward<Args>(args)...);
  }
};

template <typename Func, typename A> struct Caller<Func, A>
{
  static void call(Func & f, A && a)
  {
    f(std::forward<A>(a));
  }
};

template <typename Func, typename ...Args>
void Call(Func & f, Args && ...args)
{
  Caller<Func, Args...>::call(f, std::forward<Args>(args)...);
}

Then you can put the following in your client code:

void foo(A);
Call(foo, a1, a2, a3);
2
  • @Dolanor: Thanks for the fixes. That was a spectacular amount of mistakes for such a short post :-S
    – Kerrek SB
    Apr 26, 2013 at 16:50
  • 1
    No problems. I needed to make it work to experiment, why not sharing the correction ? ;)
    – Dolanor
    Apr 29, 2013 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.