C++ 11预定义的宏

xis*_*xis 23 c++ c++11

是否有任何预定义的C++宏,以便代码可以识别标准?

例如,目前大多数编译器将"数组"放入"tr1"文件夹,但对于C++ 11,它将成为STL的一部分.所以目前

#include <tr1/array>
Run Code Online (Sandbox Code Playgroud)

但是c ++ 11

#include <array>
Run Code Online (Sandbox Code Playgroud)

什么是03标准和11标准的预定义宏,我可以使用它#ifdef来识别?

另外,我想C90和C99有宏吗?

Thanksx

jco*_*der 20

这里开始

在C++ 0x中,宏__cplusplus将被设置为与当前199711L不同(大于)的值.

您可以测试它的值,以确定它是否是c ++ 0x


Mat*_* M. 7

鸡蛋里挑骨头......

您的特定问题不依赖于您的编译器,它取决于标准库实现.

由于您可以自由选择与编译器提供的标准库不同的标准库(例如,尝试使用libc ++或stlport),因此没有大量特定于编译器的信息可以帮助您.

因此,您最好的选择是自己创建一个特定的头文件,您可以在其中选择一个或另一个(取决于构建选项).

// array.hpp
#ifdef STD_HAS_TR1_ARRAY_HEADER
#include <tr1/array>
#else
#include <array>
#endif
Run Code Online (Sandbox Code Playgroud)

然后,您将记录编译器选项:

传递-DSTD_HAS_TR1_ARRAY_HEADER意味着std::tr1::array定义<tr1/array>而不是默认值<array>.

而且你已经完成了.


Seb*_*ach 7

从N3242草案:

16.8 Predefined macro names                          [cpp.predefined]
...
   The name _ _ cplusplus is defined to the value 201103L when
   compiling a C++ translation unit. 155)
...
155) It is intended that future versions of this standard will
     replace the value of this macro with a greater value.
     Non-conforming compilers should use a value with at most five 
     decimal digits.
Run Code Online (Sandbox Code Playgroud)