转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2154

KlayGE首次引入C++11特性之后,我顺便调研了一下个主流编译器对C++11的支持度,以便在下个版本中加入更多的C++11元素。这里还列出了在不支持的时候,可以采用的替代方案。主要参考了C++11 Features in Visual C++ 11Status of Experimental C++0x Support in GCC 4.6Status of Experimental C++0x Support in GCC 4.7Boost

C++11 Core Language Features

  VC 10 VC 11 GCC 4.6 GCC 4.7 替代方案
Rvalue references Yes Yes Yes Yes Boost.Move
Rvalue references for *this No No No No
Non-static data member initializers No No No Yes
Variadic templates No No Yes Yes
Initializer lists No No Yes Yes
static_assert Yes Yes Yes Yes Boost.Static Assert
auto Yes Yes Yes Yes Boost.Typeof
Trailing return types Yes Yes Yes Yes Boost.ReturnType
Lambdas Yes Yes Yes Yes Boost.Lambda
decltype Yes Yes Yes Yes Boost.Typeof
Right angle brackets Yes Yes Yes Yes TR1
Default template arguments for function templates No No Yes Yes
Expression SFINAE No No Yes Yes
Alias templates No No No Yes
Extern templates Yes Yes Yes Yes
nullptr Yes Yes Yes Yes 自己实现的null_ptr
Strongly typed enums No Yes Yes Yes #define
Forward declared enums No Yes Yes Yes
Attributes No No No No
constexpr No No Yes Yes
Alignment No No No No
Delegating constructors No No No Yes
Inheriting constructors No No No No
Explicit conversion operators No No Yes Yes
char16_t and char32_t No No Yes Yes
Unicode string literals No No Yes Yes
Raw string literals No No Yes Yes
Universal character names in literals No No Yes Yes
User-defined literals No No No Yes
Standard-layout and trivial types No Yes Yes Yes
Defaulted and deleted functions No No Yes Yes
Extended friend declarations Yes Yes No Yes
Extended sizeof No No No Yes
Inline namespaces No No Yes Yes
Unrestricted unions No No Yes Yes
Local and unnamed types as template arguments Yes Yes Yes Yes
Range-based for-loop No Yes Yes Yes Boost.Foreach
override and final No Yes No Yes #define
Minimal GC support Yes Yes No No
noexcept No No Yes Yes

C++11 Core Language Features: Concurrency

  VC 10 VC 11 GCC 4.6 GCC 4.7 替代方案
Reworded sequence points No No No No
Atomics No Yes No Yes Boost.Atomic
Strong compare and exchange No Yes No No
Bidirectional fences No Yes No No
Memory model No No No No
Data-dependency ordering No Yes No No
Data-dependency ordering: function annotation No No No No
exception_ptr Yes Yes Yes Yes Boost.Exception
quick_exit and at_quick_exit No No No No
Atomics in signal handlers No No No No
Thread-local storage No No No No Boost.Thread
Magic statics No No No No

C++11 Core Language Features: C99

  VC 10 VC 11 GCC 4.6 GCC 4.7 替代方案
__func__ No No Yes Yes __FUNCTION__
C99 preprocessor No No Yes Yes
long long Yes Yes Yes Yes __int64
Extended integer types No No No No

C++11 Library

  VC 10 VC 11 GCC 4.6 GCC 4.7 替代方案
shared_ptr, weak_ptr Yes Yes Yes Yes Boost.SmartPtr
make_/allocate_shared, owner_before Yes Yes Yes Yes Boost.SmartPtr
unique_ptr Yes Yes Yes Yes Boost.Interprocess
Fixed-Size Arrays Yes Yes Yes Yes Boost.Arrays
Hash Tables Yes Yes Yes Yes Boost.Unordered
Singly-linked list Yes Yes Yes Yes Boost.Container
Regular Expressions Yes Yes Yes Yes Boost.Regex
Tuple Yes Yes Yes Yes Boost.Tuple
Random Numbers & Distributions Yes Yes Yes Yes Boost.Random
Type Traits Yes Yes Yes Yes Boost.TypeTraits
Generalized Function Pointers Yes Yes Yes Yes Boost.Function
Generalized Binders Yes Yes Yes Yes Boost.Bind
Generalized Member Function Adapters Yes Yes Yes Yes Boost.Bind
Reference Wrappers Yes Yes Yes Yes Boost.Ref
Return type calculation Yes Yes Yes Yes Boost.ReturnType
New algorithms Yes Yes Yes Yes Boost.Algorithm
Time No No Yes Yes Boost.Chrono

其中,斜体的Yes表示VC 11和GCC 4.7都支持,同时还有非C++11的替代方案,所以很可能在下一版本的KlayGE中使用到。从上面的表可以看出,Library的支持度相当好。即使编译器不支持,也可以很容易地用Boost替换掉。语言核心则还远不到完美,但最常用的基本都已经涵盖了。有些feature,比如foreach和auto,用Boost在库级别支持,对编译速度有很大影响。换用C++11在核心语言级别支持的话应该有较大幅度的编译性能提升。