转载请注明来源:http://blog.youkuaiyun.com/thesys/archive/2010/06/02/5641338.aspx
c++0x是下一代c++标准,原计划于2010前推出,所以命名为c++0x(比如c++09),现在已经2010年了,都还没有最终定稿,因此我们只能无奈地认为这0x代表的是16进制了,也暗合c/c++中0x代表16进制前缀的习惯,不过如果2015年还没搞定的话,就不知道可以怎么解释了。
c++0x应该说对现行c++标准的改变很大,增加了很多新特性,十分令人期待,之前我也花了不少时间来学习和了解。最近vc10和gcc4.5都release了,而这两个重要的c++编译器都部分支持了c++0x标准,因此给了我们更好的尝试和探索的机会,接下来我计划对c++0x标准进行一系列更多的学习和探索,看看我们能用新特性来干什么,以及它能给我们的工作带来多少改进。
这里先简单罗列一下c++0x标准的新特性,以及vc和gcc对它们的支持,以便接下来做逐一的学习和探索。
下面的表格来自http://gcc.gnu.org/projects/cxx0x.html,在原始版本的基础上,我做了一点点修改,把VC的支持给标注出来。
Language Feature | Proposal | Available in GCC and VC |
---|---|---|
Rvalue references | N2118 | GCC4.3, VC10 |
Rvalue references for *this | N2439 | No |
Initialization of class objects by rvalues | N1610 | GCC4.4, VC10 |
Non-static data member initializers | N2756 | No |
Variadic templates | N2242 | GCC4.3 |
Extending variadic template template parameters | N2555 | GCC4.4 |
Initializer lists | N2672 | GCC4.4 |
Static assertions | N1720 | GCC4.3,VC10 |
auto -typed variables | N1984 | GCC4.4,VC10 |
Multi-declarator auto | N1737 | GCC4.4,VC10 |
Removal of auto as a storage-class specifier | N2546 | GCC4.4,VC10 |
New function declarator syntax | N2541 | GCC4.4,VC10 |
New wording for C++0x lambdas | N2927 | GCC4.5,VC10 |
Declared type of an expression | N2343 | GCC4.3,VC10 |
Right angle brackets | N1757 | GCC4.3,VC9 |
Default template arguments for function templates | DR226 | GCC4.3 |
Solving the SFINAE problem for expressions | DR339 | GCC4.4 |
Template aliases | N2258 | No |
Extern templates | N1987 | GCC4.3 |
Null pointer constant | N2431 | GCC4.6,VC10 |
Strongly-typed enums | N2347 | GCC4.4 |
Forward declarations for enums | N2764 | No |
Generalized attributes | N2761 | No |
Generalized constant expressions | N2235 | No |
Alignment support | N2341 | No |
Delegating constructors | N1986 | GCC patch |
Inheriting constructors | N2540 | No |
Explicit conversion operators | N2437 | GCC4.5 |
New character types | N2249 | GCC4.4 |
Unicode string literals | N2442 | GCC4.5 |
Raw string literals | N2442 | GCC4.5 |
Universal character name literals | N2170 | GCC4.5 |
Extensible literals | N2765 | No |
Standard Layout Types | N2342 | GCC4.5 |
Defaulted and deleted functions | N2346 | GCC4.4 |
Extended friend declarations | N1791 | No |
Extending sizeof | N2253 | GCC4.4 |
Inline namespaces | N2535 | GCC4.4 |
Unrestricted unions | N2544 | No |
Local and unnamed types as template arguments | N2657 | GCC4.5 |
Range-based for | N2930 | No |
Explicit virtual overrides | N2928 | No |
Minimal support for garbage collection and reachability-based leak detection | N2670 | No |
Allowing move constructors to throw [noexcept] | N3050 | No |
Defining move special member functions | N3053 | No |
Concepts [no longer part of C++0x] | N2773 | Rejected |
Concurrency | ||
Sequence points | N2239 | No |
Atomic operations | N2427 | GCC4.4 |
Strong Compare and Exchange | N2748 | No |
Bidirectional Fences | N2752 | No |
Memory model | N2429 | No |
Data-dependency ordering: atomics and memory model | N2664 | No |
Propagating exceptions | N2179 | GCC4.4 |
Abandoning a process and at_quick_exit | N2440 | No |
Allow atomics use in signal handlers | N2547 | No |
Thread-local storage | N2659 | No |
Dynamic initialization and destruction with concurrency | N2660 | No |
C99 Features in C++0x | ||
__func__ predefined identifier | N2340 | GCC4.3 |
C99 preprocessor | N1653 | GCC4.3,VC9 |
long long | N1811 | GCC4.3,VC9 |
Extended integral types | N1988 | No |
从表中可以看出,C++0x对现有标准进行了大量的改进,有望提高C++的市场占有率,而目前GCC对C++0x标准的支持从特性数量上明显要好于VC10。
- 改进中最引人注目的是现代化特性,包括垃圾收集的引入,并行计算的支持,lambda表达式等,总算是让c++这门古典的语言有了一些现代编程语言的先进特性,可谓老树发新芽。
- 很多改进是用来降低使用门槛,简化代码的,例如auto, initializer list, none-static data member initializers等,让C++这们号称最复杂的语言更容易入门和使用。
- 还有一些用来简化模板定义的改进,例如 Variadic templates, Right angle brecket等,以往那些丑陋重复的模板定义将来或许能变得美观简洁些,特别是boost那些write only的代码。
- 当然还有改进性能的,例如rvalue reference的引入,解决了C++中臭名昭著的临时对象的性能损失。
- 不过其实也有不少遗憾,concepts最终还是被否决了,而反射这么重要的设施也没有被纳入,就算纳入一个编译期静态反射也好啊。
在后续的博客中我将使用VC10和GCC4.5 开始探索C++0x的新特性,为了方便起见,我使用的GCC是MinGW GCC4.5,并使用MinGW GDB7.1进行调试。需要注意的是,由于标准还没有最终定案,将来搞不好还会改,我们得出的结论将来或许会失效。