I am using assertion macro from assert.h I have defined lambda to perform assertion checking.
int val1 = 0;
int val2 = 1;
const auto check = [val1,val2]()-> bool
{
return val1 < val2;
};
// no error for this call
assert(check() && "Test is failed");
// no error for this call
assert([=]()-> bool
{
return val1 < val2;
}() && "Test is failed");
//compile error for this call "too many arguments provided to function-like macro invocation" assert([val1,val2]()-> bool { return val1 < val2; }() && "Test is failed");
why I am getting
too many arguments provided to function-like macro invocation
compile error for the case when I am using assert macro and defining lambda with more than one argument in the capture list?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With