变量宏,其变量参数不带参数 [英] Variadic macro with no arguments for its variadic parameter

查看:592
本文介绍了变量宏,其变量参数不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用一个可变参数宏 M 是否合法,对其可变参数没有参数?

Is it legal to invoke a variadic macro M with no arguments for its variadic parameter?

相关标准报价是 [cpp.replace] / 4


如果宏定义中的标识符列表不是以省略号结束,调用函数式宏的参数(包括没有预处理令牌的那些参数)的数量应等于宏定义中的参数数量。否则,在调用中将有比宏定义中的参数(不包括 ... )更多的参数。将存在一个终止调用的预处理令牌。

If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...). There shall exist a ) preprocessing token that terminates the invocation.

没有非可变参数的情况下,以 M()形式的调用应该是合法的,因为调用具有一个参数(不包括预处理令牌)。因此,比非可变参数还有一个参数。

For the case with no non-variadic parameters, an invocation in the form M() should be legal as the invocation has one argument (consisting of no preprocessing tokens). So there is one more argument than non-variadic parameters.

对于具有一个非可变参数的情况,应该有一个尾随 / code>,如 M(1,),以引入由可变参数的无预处理标记组成的参数?否则,参数的数量将等于非可变参数的数量。即,

For the case with one non-variadic parameter, should there be a trailing , as in M(1,) to introduce an argument consisting of no preprocessing tokens for the variadic parameter? Otherwise, the number of arguments would be equal to the number of non-variadic parameters. i.e.,

#define variadic(x,...) #__VA_ARGS__

variadic(1,) // 2 arguments: ok
variadic(1) // 1 argument: wrong?

但是, Clang 接受以下测试用例:

Both Clang and GCC, however, accept the following test case:

#include <iostream>

#define variadic(x,...) #__VA_ARGS__

int main()
{
    std::cout << "'" variadic(1) "'" << std::endl;
}

并生成输出:

''

推荐答案


否则,调用中的参数比宏中的参数多定义(不包括...)。

Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...).

这个标准的摘要表明你的代码不应该是有效的:参数加上省略号。如果我们遵循上面标准的部分,你应该至少有两个参数。当你写 varidadic(1)时,你只需要提供一个参数。您的代码无效。

This very extract from the standard shows that your code should not be valid: you have one parameter plus the ellipsis. If we follow the portion of the standard above, you should have at least two arguments. When you write varidadic(1), you just provide one argument. Your code is not valid.

顺便说一下,clang会产生警告:

By the way, clang produces a warning:

main.cpp:7:32: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu]
    std::cout << "'" variadic(1) "'" << std::endl;

并且GCC也会产生警告:

And GCC also produces a warning:

main.cpp:7:32: warning: ISO C99 requires rest arguments to be used [enabled by default]
     std::cout << "'" variadic(1) "'" << std::endl;

由于这可能是程序员的麻烦,而且由于程序员的意图很容易猜到,都考虑 variadic(1)等价于 variadic(1,)

Since that may be a bother to the programmer, and since the programmer's intent is easy to guess, they both consider variadic(1) is equivalent to variadic(1,).

这篇关于变量宏,其变量参数不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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