![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| c++ 09 :一览未来 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-12 ![]() |
||||||
|
c++ 09 :一览未来
conceptes).其他主要特色还包括,比如初始化时候的类型自动推演,代理构造器(delegating constructors),NULL指针,甚至会处理讨厌的右尖括号。
c++: 一种艺术的状态。 今天的c++相比c++98,已经有很大的提升;这些包括微妙的技术勘正,比如vector的(内存)连续性问 题。 然而,c++98和c++03最大的改变是增加了TR1库。TR1库包括了hash容器,正则表达式,tuples, new smart pointer类,和一个数组类模板。 .右值引用 在叫做左值引用。与左值引用不同,右值引用可以绑定在右值,甚至他们不具备const 特性。右值引用 可使得程序员: . 消除 当执行移动语义时的不必要和昂贵的对象拷贝开销。 在讨论“移动语义”的实行性问题前,让我们先来看看右值引用的语法。 The Move Movement( ?)
表达式,和基于把已存在对象拷贝到新的目标对象的argument passing。这些特性在c++中是如此的基础 ,以至于要花很长时间去抓住“移动语义”的那些微妙不同。简单的说,“移动”就是根据原对象直接 构造目标对象。所以,你要结束的是一个对象拥有了新的状态,而不是拥有两个相互拷贝的对象。 c++98在某些地方已经实行了“移动语义”。比如 "具名返回值优化",就是"移动语义"的表现。 STL 容器的 swap()函数也是这种情况。当你用vector::swap()交换两个vector时,这两个容器不会再在 新的内存构造他们的元素;他们仅仅是交换一些指针和flag。这种表现是动态的,当拷贝一个包含有N个 元素的容器时,包含了N 个拷贝构造和N个析构动作,这是一种线形开销。相比之下,"移动"是的开销是 常量的。 另外一种"移动语义"的实例是 std::auto_ptr。由于它的特殊“移动语义”(确切的说是最纯的“移动语 义”),使得它成为了标准库的一个子集。但是,随着右值引用的到来,“移动”相关的算法和容器将会 重新勾起一些对类,比如auto_prt的兴趣。
类型概念 Tpye Concepts 从一开始,C++模板就禁受着一个重大的局限,也就是对模板缺少一种定义和强制约束的机制。在一些常 见的工作范畴,比如tag dispatching(标签派送?)和type traits(类型萃取),他们的天性是复杂 的,冗长的,和约束性不易被早期发现的。在某种意义上,是没有办法在编译期进行强制约束的。 Douglas Gregor 和 Bjarne Stroustrup的概念性提议才结束掉这种窘境。概念性提议介绍了针对模板的 独立的类型确认系统。模板声明被一种“概念”扩充,所谓的“概念”往往是一组必要条件。当模板实 例化时,编译器会确认这个实例是否和“概念”的要求符合。 比如 std::min()算法: template<typename T>
The user has to examine the body of min() to know what the requirements from T are. In this case, T must be a type that has an operator< taking two references to T and returning a Boolean result. With concepts, this requirement can be exdivssed as follows: 用户需要察看min的实体去了解T到底需要那些东西。对这个例子,T必须是一个类型,而其包括以两个T template<LessThanComparable T> //T must be a LessThanComparable type Henceforth, whenever the compiler encounters an instantiation of min, it checks whether T meets the LessThanComparable requirements before generating the code for that instantiation. If T isn't LessThanComparable a descriptive error message will be issued. ,如果没有,就会发布一个错误! Removing Tiny Annoyances from C++ C++09 includes a few core language changes that remove inelegance and minor annoyances. The angle brackets proposal removes a long standing syntactic displeasure, i.e., the requirement that the right angle brackets of a nested template argument shall be separated with spaces: c++09 对一些核心语言作了改变,来消除那些小麻烦。
一个空格: //C++98
C++ compilers are smart enough to figure out that the last >> belong to two templates, so there's no reason to burden programmers with this restriction anymore. (It is legal to use spaces in C++09, though.) c++98限制这种情况发生的背后原理是"maximal munch"规则。但是现代的c++编译器已经足够智能地去推 认出最后的 >>是属于两个模板的,所以就没有理由限制程序员再去写这样的代码了。(当然有在c++90 种用空格也是合法的)。 nullptr is another simple yet welcome addition to C++. Many programming languages have a specific keyword that designates a null pointer or reference. C++ uses the literal 0 for this purpose. This hack causes ambiguity problems, particularly in overload resolution. The proposed nullptr solves this problem:
采用用标志 0。 这就应起表达不清楚地问题,特别在重载决议时。空指针提议解决了这个问题: void f(char *);
newcomers feel at home when using C++09. A delegating constructor uses a member initializer list that invokes another constructor (called the target constructor) of the same class. Once the target constructor returns, the delegating constructor may perform additional operations. This way, common initialization operations are encapsulated in one constructor of the class, instead of being repeated in every constructor: 代理构造器(或者叫向前构造器),使得c#和java在使用c++09感到很舒服。代理构造器用一个成员构造 器列表来调用同一个类中的另外一个构造器(叫做目标构造器)。一旦目标构造器返回了,代理构造器 器会作附加动作。这样子,公共的初始化操作就被压缩在一个构造器里,而不是在每个构造器中重复: struct S The output shows the constructor execution order: target ctor
Additional C++09 features include, among the rest: extern templates (not to be confused with exported templates) which is a mechanism for supdivssing the implicit instantiation of templates At this stage, it isn't clear yet whether all of these proposals will make it into the next standard. While there is a clear intention to do so, the voting on the Final Candidate Document must take place before the end of 2007 to comply with ISO rules. This leaves the committee two more meetings (in April and October 2007) to finalize the list of C++09 features. C++09 is much closer than it seems!
extern templates (不要和exported templates混淆),它是抑制模板implicit instantiation的 一种机制。
ISO章程要求最终候选文档将在2007年末前要被推选出来。这样子的话留给委员会的还有两个或更多的 会议(在2007的四月和九月)来确定c++09的那些特性。c++09马上就将来临。
后记:译了一天,好累。
本文来源:http://blog.csdn.net/hziee_/archive/2007/08/23/1756638.aspx
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|