简体   繁体   English

C ++ 11功能的可用性

[英]Availability of C++11 features

Compiler vendors have been adopting C++11 features piecemeal, which was predictable, as many of them are not easily implemented. 编译器供应商已经采用零碎的C ++ 11功能,这是可预测的,因为其中许多功能都不易实现。

The standard way for reporting which spec the compiler fully supports is via the __cplusplus predefined macro. 报告编译器完全支持的规范的标准方法是通过__cplusplus预定义宏。 However, major vendors are reporting __cplusplus = 199711L, meaning they are only fully supporting C++98 (eg. MSVC14). 但是,主要供应商报告__cplusplus = 199711L,这意味着他们只是完全支持C ++ 98(例如MSVC14)。 This (presumably) means that they do not fully support the C++11 spec, even though they may have implemented a lion's share of the functionality. 这(大概)意味着它们并不完全支持C ++ 11规范,即使它们可能实现了大部分功能。

I would like to start using C++11 features, when they are available (and fallback to existing code when they are not). 我想开始使用C ++ 11功能,当它们可用时(并且当它们不可用时回退到现有代码)。 However, my code must support many compilers, including proprietary compilers which I may not have access to use. 但是,我的代码必须支持许多编译器,包括我可能无法使用的专有编译器。 Is there any standard way to know which C++11 features are available from a compiler, without knowing specifically which compiler is being used? 是否有任何标准方法可以知道编译器可以使用哪些C ++ 11功能,而不知道具体使用哪个编译器? (if a compiler behaves in a non-standard way, then it is acceptable for the detection behavior to be incorrect). (如果编译器以非标准方式运行,则检测行为可能不正确)。

NOTE: This question is a generalization of my question ' Availability of static_assert c++11 ', which was not very well received, because I think my motivation was misunderstood. 注意:这个问题是我的问题“ static_assert c ++ 11可用性 ”的概括,这个问题并不是很受欢迎,因为我觉得我的动机被误解了。

You might be interested in feature test macros , which enable you to test for specific C++11, C++14 or even C++17 features, such as __cpp_static_assert , __cpp_lib_make_unique or __cpp_variable_templates . 您可能对功能测试宏感兴趣,它使您能够测试特定的C ++ 11,C ++ 14甚至C ++ 17功能,例如__cpp_static_assert__cpp_lib_make_unique__cpp_variable_templates Clang and GCC already support this, see a live demo . Clang和GCC已经支持这个,请看现场演示

g++ / clang++ do not have C++11 enabled by default, not even the latest versions. g++ / clang++默认情况下没有启用C ++ 11,甚至没有最新版本。 Whenever you compile with g++ using -std=c++11 , your macro __cplusplus will have the expected value. 无论何时使用-std=c++11使用g++进行编译,您的宏__cplusplus都将具有预期值。

VS seem to have all features enabled by default, thanks @Comic, but it is not updating the macro since it is not yet fully C++11 compliant. VS似乎默认启用了所有功能,感谢@Comic,但它没有更新宏,因为它还不完全符合C ++ 11。

As far as detecting "C++11 availability" for a generic compiler, I am not aware of any portable way of doing it, unless you check for the __cplusplus macro. 至于检测通用编译器的“C ++ 11可用性”,我不知道有任何可移植的方法,除非你检查__cplusplus宏。 But, as you observed, the macro may not be implemented for some compilers by default (as is the case for g++ / clang++ ), or not implemented at all ( VS ). 但是,正如您所观察到的那样,默认情况下可能没有为某些编译器实现宏(如g++ / clang++ ),或者根本没有实现( VS )。 Your only choice at this stage seem to be an external tool like CMake, which can detect the compiler, and conditional on the compiler type CMake can then define some macro which you can check in your code to enable C++11. 你在这个阶段唯一的选择似乎是像CMake这样的外部工具,它可以检测编译器,并且以编译器类型为条件,然后CMake可以定义一些宏,你可以在代码中检查它以启用C ++ 11。

I think many of the C++11 features are available on GCC 4.8 also. 我认为许多C ++ 11功能也可以在GCC 4.8上获得。 Compile your program with -std=c++11 option. 使用-std = c ++ 11选项编译程序。

I think some of the C++11 features were in GCC 4.6.3 as well. 我认为一些C ++ 11特性也在GCC 4.6.3中。 Compile option to enable it -std=c++0x . 编译选项以启用它-std = c ++ 0x。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM