有没有办法在编译时检测编译器是否支持C++ 11的某些功能?例如,像这样:
#ifndef VARIADIC_TEMPLATES_SUPPORTED
#error "Your compiler doesn't support variadic templates. :("
#else
template <typename... DatatypeList>
class Tuple
{
// ...
}
#endif
Run Code Online (Sandbox Code Playgroud) 如果有一些代码我想尽可能多地使用C++ 11x扩展,但如果不支持则有后备.目前,GCC的OSX版本和VisualC编译器几乎不支持C++ 11x,所以我使用:
#if (defined(__APPLE__) || (defined(_WIN32)))
...fallback code without C++11x ...
#else
... code using C++11x ...
#endif
Run Code Online (Sandbox Code Playgroud)
这可行,但不是真正正确的事情,特别是因为MacPorts中的gcc编译器支持c ++ 11x.
有#define C11X_SUPPORTED型宏吗?也许GCC只有一些东西?
我的系统编译器(gcc42)可以正常使用我想要的TR1功能,但是试图支持除系统之外的新编译器版本,尝试访问TR1标头和#error要求-std = c ++ 0x选项因为它如何与库或某些集线器接口.
/usr/local/lib/gcc45/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
Run Code Online (Sandbox Code Playgroud)
必须提供额外的开关是没有问题的,在这个系统(FreeBSD)下支持GCC 4.4和4.5,但显然它改变了画面!
使用我的系统编译器(g ++ 4.2默认方言):
#include <tr1/foo>
using std::tr1::foo;
Run Code Online (Sandbox Code Playgroud)
使用带有-std = c ++ 0x的较新(4.5)版本的编译器:
#include <foo>
using std::foo;
Run Code Online (Sandbox Code Playgroud)
无论如何使用预处理器,我可以判断g ++是否在启用C++ 0x功能的情况下运行?
我正在寻找的东西是这样的:
#ifdef __CXX0X_MODE__
#endif
Run Code Online (Sandbox Code Playgroud)
但是我没有在手册中或网上找到任何内容.
按照这个速度,我开始认为生活会更容易,使用Boost作为依赖,而不用担心在TR4之前到达的新语言标准......呵呵.