有没有为C++预定义的宏,以便代码能够识别标准?
例如,目前大多数编译器将“数组”放入“STL”文件夹,但对于C++11,它将是tr1的一部分。所以目前
#include <tr1/array>但是c++11
#include <array>为了使用#ifdef识别,03标准和11标准的预定义宏是什么?
另外,我假设有用于C90和C99的宏?
感恩节
发布于 2011-08-29 05:29:26
来自Stroustrup's C++11 FAQ
在C++11中,宏
__cplusplus将被设置为不同于(大于)当前199711L的值。
然后,您可能会测试它的值,以确定它是否为c++0x。
发布于 2011-08-29 15:08:48
吹毛求疵。
您的特定问题与您的编译器无关,它取决于标准库的实现。
由于您可以自由选择与编译器提供的标准库不同的标准库(例如,尝试使用libc++或stlport),因此任何编译器特定的信息都不会对您有所帮助。
因此,最好的办法是自己创建一个特定的头文件,在这个头文件中,您可以选择一个或另一个(取决于构建选项)。
// array.hpp
#ifdef STD_HAS_TR1_ARRAY_HEADER
#include <tr1/array>
#else
#include <array>
#endif然后记录编译器选项:
传递-DSTD_HAS_TR1_ARRAY_HEADER的
将意味着
std::tr1::array是在<tr1/array>中定义的,而不是默认的<array>。
你就完事了。
发布于 2011-09-01 19:34:49
从草案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.https://stackoverflow.com/questions/7223991
复制相似问题