幾個C編譯器對 C++ 11特性的支持
距離我上次對C++11支持的不同編譯器的比較已經(jīng)有大半年了。這次我來根據(jù)這些預(yù)覽版的編譯器的文檔來看下它們是如何堆砌起來的。
GCC的下個版本是4.8,以及Clang即將到來的版本是3.3 。如果你使用Visual Studio 2012,你可以安裝2012年11月更新支持C++11額外特征的體驗版CTP。
我也對V.13.0的Intel C++編譯器感到好奇,雖然它還不是預(yù)覽版并且我也找不到有關(guān)它的新特性的信息。我沒找到任何有關(guān)這個即將發(fā)行版本的編譯器的信息。
| Feature | 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已經(jīng)增加了好幾個重要C++11特性,像變參模板,初始化器和原生字。
我真的不能在一個更為細(xì)致的層面去評論這些編譯器實現(xiàn)到底完成得怎樣,還有沒有bug。(除了VS2012——我在我的書(《C++11 Rocks》)里詳細(xì)列出過VS2012最初版本的大量bug)。
看下庫的支持情況也是比較有用的。由于各編譯器對標(biāo)準(zhǔn)庫的支持都有較多小改動,我并不打算對此給出詳細(xì)的細(xì)節(jié)。我也打算在這次的比較中省略Intel的庫。
我可以說,這些庫的主要附件大多由第三方實現(xiàn)提供(隨后在下面的表格中展現(xiàn)),盡管這是有各種各樣的警告。
微軟的庫實現(xiàn)中沒有那些需求尚未被實現(xiàn)的語言功能的東西,例如 constexpr(如VS2012的最初發(fā)行版)。庫文件還沒有更新,以支持2012年11月在 CTP 提出的編譯器功能,如初始化列表和可變參數(shù)模板。
GCC 的 libstdc++ 也有些滯后,例如它并不支持正則表達(dá)式以及地稱并發(fā)功能。同樣,在很多情況下,它也沒有實現(xiàn) constexpr 方法。
Clang的libc++是100%兼容MacOS的,但是它有部分的特性還不兼容Windows和Linux。
| Feature | 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 |
比較高興的是能看到語言和庫的支持在穩(wěn)步改善。Clang和GCC距離完全支持C++11已經(jīng)很近了。Visual Studio同樣在改善對C++11的支持,令我感到欣慰的是C++編譯器的更新都是在主分支上。這份Intel的編譯器特征支持列表也越來越多。
誰知道明年這4個編譯器會不會全部支持C++11的特征呢!
原文鏈接:http://www.oschina.net/translate/c11-compiler-support-shootout-visual-studio-gcc-clang-intel
英文原文:C++11 compiler support shootout: Visual Studio, GCC, Clang, Intel



















