15
votes

When I try to make a variadic lambda:

#include <cstdarg>

int main() {
    [] (int x, ...) { va_list xs; va_start(xs, x); va_end(xs); };
    return 0;
}

With GCC this compiles fine, but clang rejects it:

main.cpp:4:35: error: 'va_start' used in function with fixed args
    [] (int x, ...) { va_list xs; va_start(xs, x); va_end(xs); };
                                  ^

Which compiler is correct here? Is it possible to make a variadic lambda?


Curiously, the following results in an ICE with clang, so it looks like clang is being buggy with variadics:

int f(...) {
    [] (int x, ...) { va_list xs; va_start(xs, x); va_end(xs); };
    return;
}
1
Your original example works in Visual Studio 2013 as well. - Robinson
"Which compiler is correct here?" Certainly not the one that ICEs. - Griwes
@Griwes and the standard says....? - Mgetz

1 Answers

10
votes

Variadic lambdas seem to be allowed - the syntax to specify the parameters is the same parameter-declaration-clause used by any other function declaration, and there's no specific rule to disallow it from ending with an ellipsis. (C++11 does ban default arguments, but not ellipses, and that restriction was removed in C++14.)

In any case, if there's an internal compiler error (which I assume is what you mean by ICE), then that means the compiler has a bug, regardless of the correctness of the code.