#如果!如果 SOME_MACROS 始终具有数值,则 SOME_MACROS 相当于 #ifndef SOME_MACROS [重复]

问题描述 投票:0回答:1

可能这是重复的,我找不到类似的问题。

令我惊讶的是,以下代码适用于所有三大编译器,没有错误

#include <cstdio>

int main() {
    #if !_LIBCPP_VERSION 
        std::printf("_LIBCPP_VERSION not defined");
    #else
        std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);
    #endif

    #ifndef _LIBCPP_VERSION
        std::printf("_LIBCPP_VERSION not defined");

    #else
        std::printf("_LIBCPP_VERSION defined and equal to %d", _LIBCPP_VERSION);

    #endif
}

链接到上帝螺栓

我的问题是:有检查

#if !_LIBCPP_VERSION
- 总是与标准 C 或 C++ 的
#ifndef _LIBCPP_VERSION
类似吗?

c++ macros language-lawyer conditional-compilation
1个回答
2
投票

它们对于数值而言并不等价。

SOME_MACROS
#if !SOME_MACROS
#ifndef SOME_MACROS
x
正确 错误
1
错误 错误
0
正确 错误
未定义 正确 正确

现在,您说您只关心数值,因此这里只有中间行相关。然而我们看到这两行存在差异。

© www.soinside.com 2019 - 2024. All rights reserved.