"gnu-zero-variadic-macro-arguments"是否可以忽略? [英] Is the `gnu-zero-variadic-macro-arguments` safe to ignore?

查看:332
本文介绍了"gnu-zero-variadic-macro-arguments"是否可以忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码 (实时示例) :

Consider the following code (live example):

#define TEST_VA(mX, ...) TEST
#define STRINGIFY_IMPL(mX) #mX
#define STRINGIFY(mX) STRINGIFY_IMPL(mX)

#include <iostream>

int main()
{
    std::cout << STRINGIFY(TEST_VA(1)) << std::endl;
    std::cout << STRINGIFY(TEST_VA()) << std::endl;
    return 0;
}


clang ++ 3.4 抱怨:


clang++ 3.4 complains:

main.cpp:9:37: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments]
    std::cout << STRINGIFY(TEST_VA(1)) << std::endl;
                                    ^
main.cpp:1:9: note: macro 'TEST_VA' defined here
#define TEST_VA(mX, ...) TEST
        ^
main.cpp:10:33: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments]
        std::cout << STRINGIFY(TEST_VA()) << std::endl;
                                       ^
main.cpp:1:9: note: macro 'TEST_VA' defined here
#define TEST_VA(mX, ...) TEST


我在项目中的示例代码中使用了一些可变参数宏.


I use some variadic macros like in the example code in my projects.

不可变参数宏的...参数不传递任何参数吗? (基本上忽略了警告)

Is it safe to pass no arguments for the ... parameter of variadic macros? (Ignoring the warning, basically)

还是会导致问题?

该标准明确禁止了吗?

推荐答案

该标准在技术上禁止使用.从C ++ 11§16.3/4(添加重点)开始:

It's technically forbidden by the standard. From C++11 §16.3/4 (emphasis added):

如果宏定义中的标识符列表不以省略号结尾,则参数数量(包括 调用类似函数的宏的那些参数(不包含预处理令牌)应等于 宏定义中的参数数. 否则,调用中的参数要比宏定义中的参数(...除外)要多.必须存在)预处理 终止调用的令牌.

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.

某些编译器可能会将其作为扩展,但是如果您要使用符合标准的代码,则应确保始终为可变参宏的省略号参数至少包含一个参数.

Some compilers may allow this as an extension, but if you want standards-compliant code, you should make sure to always include at least one argument for the ellipsis parameter of a variadic macro.

这篇关于"gnu-zero-variadic-macro-arguments"是否可以忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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