加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – MSVC未正确评估定义(VARIABLE)?

发布时间:2020-12-16 04:58:26 所属栏目:百科 来源:网络整理
导读:运行以下代码 #include iostream#define FOO#define BAR defined(FOO)int main() {#if BAR std::cout "BAR enabled!" std::endl;#else std::cout "BAR disabled!" std::endl;#endif return 0;} 在Visual Studio中显示Bar disabled!,在gcc或clang中运行相同
运行以下代码
#include <iostream>

#define FOO
#define BAR defined(FOO)

int main() {
#if BAR
    std::cout << "BAR enabled!" << std::endl;
#else
    std::cout << "BAR disabled!" << std::endl;
#endif
    return 0;
}

在Visual Studio中显示Bar disabled!,在gcc或clang中运行相同的代码时显示Bar enabled!.

这是Microsoft编译器中的错误吗?根据标准,什么是正确的?

解决方法

根据标准,这是未定义的行为.

[cpp.cond],强调我的

Prior to evaluation,macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator),just as in normal text. If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement,the behavior is undefined.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读