主流编译器对C++11的支持分析
更新时间:2013/3/21 点击:4798次
GCC 4.8刚刚发布,Clang 3.3也发布在即,如果你使用Visual Studio 2012,也可以通过安装一个实验性的CTP更新(VS2012 Nov CTP)来获取额外的C++11支持;除此之外,这里还比较了Intel C++编译器v.13.0,不过这仍然是一个预览版。
| 特性 | VS2012 Nov CTP | g++ 4.8 | Clang 3.3 | Intel 13.0 |
|---|---|---|---|---|
| auto | Yes | Yes | Yes | Yes |
| decltype | Yes | Yes | Yes | Yes |
| Rvalue references and move semantics | Yes | Yes | Yes | Yes |
| Lambda expressions | Yes | Yes | Yes | Yes |
| nullptr | Yes | Yes | Yes | Yes |
| static_assert | Yes | Yes | Yes | Yes |
| Range based for loop | Yes | Yes | Yes | Yes |
| Trailing return type in functions | Yes | Yes | Yes | Yes |
| extern templates | Yes | Yes | Yes | Yes |
| >> for nested templates | Yes | Yes | Yes | Yes |
| Local and unnamed types as template arguments | Yes | Yes | Yes | Yes |
| Variadic macros | Yes | Yes | Yes | Yes |
| Variadic templates | Yes | Yes | Yes | Yes |
| Default template arguments in function templates | Yes | Yes | Yes | Yes |
| final method keyword | Yes | Yes | Yes | No |
| override method keyword | Yes | Yes | Yes | No |
| Strongly typed enums | Yes | Yes | Yes | Partial |
| Forward declared enums | Yes | Yes | Yes | Partial |
| Initializer lists | Yes | Yes | Yes | Partial |
| explicit type conversion operators | Yes | Yes | Yes | No |
| Raw string literals | Yes | Yes | Yes | No |
| Forwarding constructors | Yes | Yes | Yes | No |
| Template aliases | No | Yes | Yes | Yes |
| Defaulted methods | No | Yes | Yes | Yes |
| Deleted methods | No | Yes | Yes | Yes |
| New built-in types | Partial | Yes | Yes | Partial |
| Alignment support | Partial | Yes | Yes | No |
| Inline namespaces | No | Yes | Yes | No |
| sizeof on non-static data members without an instance | No | Yes | Yes | No |
| Changed restrictions on union members | No | Yes | Yes | No |
| User defined literals | No | Yes | Yes | No |
| Encoding support in literals | No | Yes | Yes | No |
| Arbitrary expressions in template deduction contexts | No | Yes | Yes | Don’t know |
| Non-static data member initializers | No | Yes | Yes | Don’t know |
| noexcept | No | Yes | Yes | Partial |
| constexpr | No | Yes | Yes | Partial |
| C99 compatibility | Partial | Yes | Partial | Yes |
| Generalized attributes | No | Yes | Partial | Yes |
| Thread local storage | Partial | Yes | No | Partial |
| Inheriting constructors | No | Yes | No | No |
| Rvalue references for *this | No | No | Yes | No |
| Minimal support for garbage collection | Yes | No | No | Don’t know |
从表格数据我们可以看到,GCC正在取代Clang成为C++11支持最佳的编译器,而Visual Studio在对C++11特性的支持上也有明显的改进,特别是新增对variadic templates、 initializer lists以及raw literals的支持。当然这些支持中那些属于完全支持,哪些是无bug的支持不得而知,需要开发者自己去体验。
除了C++11特性之外,对其类库的支持同样需要的到重视,但下表中不会标注各编译器支持与标准库之间的细小差别。
- 微软的库并没有实现那些需要尚未被实现的语言功能支持的东西,例如 constexpr(在Visual Studio 2012最初的发行版中)。库文件并没有为支持2012年11月CTP中使用的特性——如 initializer list(初始化列表)和variadic template(可变参数模板)——而更新。
- GCC libstdc++有点滞后,因为它不支持正则表达式以及低级并行特性,此外,在很多情况下也没有实现constexpr方法。
- Clang对libc++的支持在Mac OS上非常完美,但部分功能尚未在Windows和Linux下实现。
| 特性 | MSVC | libstdc++ | libc++ |
|---|---|---|---|
| Concurrency: async/future/promise/packaged_task | Yes | Yes | Yes |
| Concurrency: thread and related | Yes | Yes | Yes |
| Concurrency: condition variables | Yes | Yes | Yes |
| Concurrency: mutexes | Yes | Yes | Yes |
| Concurrency: atomic types and operations | Yes | Yes | Yes |
| Concurrency: relaxed memory ordering and fences | Yes | No | Yes |
| Smart pointers | Yes | Yes | Yes |
| Tuples | Yes | Yes | Yes |
| std::bind | Yes | Yes | Yes |
| std::function | Yes | Yes | Yes |
| Regular expressions | Yes | No | Yes |
| Type traits | Yes | Partial | Yes |
| std::forward_list | Yes | Yes | Yes |
| std::array | Yes | Yes | Yes |
| Hash tables | Yes | Yes | Yes |
| Random number generation | Yes | Yes | Yes |
| Compile time rational numbers (ratio) | Yes | Yes | Yes |
| Time utilities (chrono) | Yes | Yes | Yes |
| Initializer lists | Yes | Yes | Yes |
| Diagnostics (system_error) | Yes | Yes | Yes |
| STL refinements and new algorithms | Yes | Yes | Yes |
| General purpose (move, forward, declval etc.) | Yes | Yes | Yes |
可以看到,C++及其类库的支持已经大有提升,Clang和GCC几乎已经为C++11提供了完整的支持;Visual Studio也增强了对C++11的支持;同时Intel的编译器对C++11的支持大幅提升。
相信到明年,各主流编译器应该都能够完全支持C++11标准。


