在 Xcode [c++] 中在 assert (assert.h) 内定义 lambda 时,为类似函数的宏调用编译错误提供了太多参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35957357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:37:41  来源:igfitidea点击:

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

c++xcodec++11lambdamacros

提问by T M

I am using assertion macro from assert.h I have defined lambda to perform assertion checking.

我正在使用 assert.h 中的断言宏我已经定义了 lambda 来执行断言检查。

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");
//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?

当我使用 assert 宏并在捕获列表中使用多个参数定义 lambda 时,会出现编译错误吗?

回答by Baum mit Augen

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.

预处理器对 C++ 语法的理解极其有限,它主要进行琐碎的文本替换。如果逗号不在匹配的内括号之间(当然也不是像字符串文字这样的标记的一部分),预处理器会将其视为宏调用参数的分隔符。

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

因此,预处理器认为您正在使用两个参数[this和第一个逗号后面的其余内容调用断言,从而产生错误。

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.

由最外面的匹配括号界定的预处理标记序列形成了类似函数的宏的参数列表。列表中的各个参数由逗号预处理标记分​​隔,但匹配内括号之间的逗号预处理标记不分隔参数。如果参数列表中存在预处理标记序列,否则将充当预处理指令,155 行为未定义。

16.3/11 in N4140, emphasis mine.

N4140 中的 16.3/11,重点是我的。

回答by Some programmer dude

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.

因此,如果您将带有逗号的任何内容作为参数传递给宏,则不能使用宏。