获取过多的参数提供给函数式宏调用编译错误,而在Xcode中定义lambda内部assert(assert.h)[c ++] [英] Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++]

查看:4421
本文介绍了获取过多的参数提供给函数式宏调用编译错误,而在Xcode中定义lambda内部assert(assert.h)[c ++]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


为什么我会得到


为函数式宏调用提供的参数过多

too many arguments provided to function-like macro invocation

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

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.

预处理器对C ++语法的理解非常有限,它主要是简单的文本替换。如果逗号不在匹配的内括号之间(而不是像字符串文字一样的令牌的一部分),预处理器将它作为宏调用的参数的分隔符。

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.

因此,预处理器认为你使用两个参数 [this ]和第一个逗号后面的其他东西调用assert,产生错误。

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:


由外部最匹配的括号限定的预处理令牌的序列形成函数式宏的
参数的列表。 列表中的单个参数由逗号
预处理标记分​​隔,但逗号在匹配的内括号之间的预处理标记不会分隔
参数
。如果在

16.3 / 11在N4140中,参数列表中的
作为预处理伪指令,强调我。

16.3/11 in N4140, emphasis mine.

这篇关于获取过多的参数提供给函数式宏调用编译错误,而在Xcode中定义lambda内部assert(assert.h)[c ++]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆