Search code examples
c++lambdac++14variadic-templatesauto

Eliding "auto" keyword in a C++ variadic lambda?


I recently found out that this piece of code compiles fine in both GCC and MSVC:

auto foo = [](...){
    cout << "foo() called" << endl;
};

It takes it any number of any kind of parameters, and simply does nothing with those parameters, so it works as if auto was placed before the ...:

// All of these lines will call the lambda function
foo();
foo(100);
foo("Test");
foo("Testing", 1, 2, 3);

The C++ reference on lambda functions does not seem to mention about this, and neither does the page on parameter packs.

More surprisingly, this fails to compile:

auto foo = [](... x){ // compile error
    cout << "foo() called" << endl;
};

Is this behaviour dictated by the standard, and if so, why should the former compile and the latter fail?


Solution

  • This is just plain old type-unsafe C-style variadic arguments.