Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x Variadic Parameter Pack: Syntax

Tags:

c++

gcc

c++11

the following code snippet won't compile under gcc4.6.1:

template <typename... TS>
void do_stuff(TS... ts)
{
  auto f = [](TS... things) { };
}

It throws an error stating that the pack things was not expanded. The following code does compile however:

template <typename... TS>
void do_stuff(TS... ts)
{
  auto f = [](TS... things...) { };
}

Notice the extra unpacking operator after things inside the parameter list. I've never seen a situation where a variadic pack had to be expanded during its declaration. So my question to you kind folks is:

Is this legal C++0x syntax (the snippet that compiles) or is it just a quirk with GCC when it comes to dealing with variadic types?

like image 778
bstamour Avatar asked Jul 26 '11 14:07

bstamour


1 Answers

Two things:

  • Yes, GCC is wrong to reject [](TS... things) { }. It's possible that it hasn't been implemented yet.
  • What you declared by [](TS ... things...) { } is equivalent to [](TS... things, ...). In C++ (not in C), you can leave off the comma before the C-style variadic ellipsis. So instead of doing void printf(char const *fmt, ...) you can declare void printf(char const *fmt...). That's what happens in your lambda. The first ellipsis is the parameter pack unpacking, and the second ellipsis is the C-style variadic ellipsis.
like image 78
Johannes Schaub - litb Avatar answered Sep 20 '22 07:09

Johannes Schaub - litb