Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++] Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++] xcode xcode

Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++]


The problem is the comma in the capture list.

The preprocessor has an extremely limited understanding of the C++ syntax, it mainly does trivial text substitution. If a comma is not between matching inner parenthesis (and not part of a token like a string literal of course), the preprocessor will treat it as a separator of arguments of the macro invocation.

So the preprocessor thinks you are invoking assert with the two arguments [this and the rest of the stuff behind the first comma, which yields the error.

You can fix this error by using an extra set of parenthesis:

int i = -7, j = 7;assert(([i,j](){return i + j;}()));

For the standard lovers:

The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro. The individual arguments within the list are separated by comma preprocessing tokens, but comma preprocessing tokens between matching inner parentheses do not separate arguments. If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives,155 the behavior is undefined.

16.3/11 in N4140, emphasis mine.


The preprocessor is very simple, it sees all commas as argument separators.

So you can't use a macro if you pass in anything with a comma as argument to the macro.