在 Xcode [c++] 中的 assert (assert.h) 中定义 lambda 时,为类似函数的宏调用提供了太多参数编译错误 [英] Getting too many arguments provided to function-like macro invocation compile error while defining lambda inside assert (assert.h) in Xcode [c++]

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

问题描述

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

int val1 = 0;诠释 val2 = 1;常量自动检查 = [val1,val2]()->布尔{返回值1 布尔{返回值1 

<块引用>

//此调用的编译错误提供给类似函数的宏调用的参数过多"断言([val1,val2]()->布尔{返回值1 

我为什么会得到

<块引用>

提供给类函数宏调用的参数过多

当我使用断言宏并在捕获列表中定义具有多个参数的 lambda 时,编译错误?

解决方案

问题是捕获列表中的逗号.

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

因此预处理器认为您正在调用带有两个参数 [this 的断言以及第一个逗号后面的其余内容,这会产生错误.

您可以通过使用一组额外的括号来修复此错误:

int i = -7, j = 7;断言(([i,j](){返回 i + j;}()));

<小时>

对于标准爱好者:

<块引用>

由最外面的匹配括号包围的预处理标记序列形成了类函数宏的参数.列表中的各个参数用逗号分隔预处理标记,但匹配的内括号之间的逗号预处理标记不分开参数. 如果参数列表中有预处理标记序列,否则充当预处理指令,155 行为未定义.

N4140 中的 16.3/11,强调我的.

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.

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

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