10

What's the correct way to do this with g++:

template < typename F >
void g (F f);

template < typename ... A >
void h (A ... a);

template < typename ... A >
void f (A ... a) {
  g ([&a] () { h (a...); }); // g++-4.6: error: parameter packs not expanded with »...«
}
AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
Thomas
  • 115
  • 1
  • 5

2 Answers2

16

I think you need to expand the pack a in the capture list as well, like this:

template < typename ... A >
void f (A ... a) {
  g ([&, a...] () { h (a...); }); 
}

Here is the relevant text from the C++0x Final Committee Draft, section 5.1.2.23:

A capture followed by an ellipsis is a pack expansion (14.5.3). [ Example:

template<class... Args> void f(Args... args) {
    auto lm = [&, args...] { return g(args...); }; lm();
}

— end example ]

SCFrench
  • 8,244
  • 2
  • 31
  • 61
  • Thank you. g++-4.6 doesn't accept this syntax from the draft yet: test01.cc:2:23: Fehler: expected »,« before »...« token – Thomas Jul 31 '10 at 19:58
  • 3
    I solved it by making a tuple, passing it to the lambda function and unpacking there. – Thomas Jul 31 '10 at 21:08
  • 3
    It looks like g++-4.8 still doesn't accept this syntax, but ICC 13.0.1 does: http://bit.ly/14auYGy – SCFrench May 10 '13 at 16:56
0
#include <functional>
template < typename ... A >
void f (A ... a) {
  g (std::bind(h, a...));
}
Ivan Shcherbakov
  • 2,063
  • 14
  • 23