首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++09解决办法

2012-02-17 
c++09http://blog.csdn.net/hziee_c++09:一览未来在2006年10月在美国波特兰会议上,c++标注委员会把2009年

c++09
http://blog.csdn.net/hziee_    
                                         
                                                                    c++   09   :一览未来
 
        在2006年10月在美国波特兰会议上,c++标注委员会把2009年作为c++09--下一代的c++标准
的一个发布时间.   c++09包括两大核心特征:   左值引用(rvalue   references)和类型概念(   type  

conceptes).其他主要特色还包括,比如初始化时候的类型自动推演,代理构造器(delegating  

constructors),NULL指针,甚至会处理讨厌的右尖括号。那么接下来跟我一起来预览c++09的核心吧.
             
            c++:   一种艺术的状态。
今天的c++相比c++98,已经有很大的提升;这些包括微妙的技术勘正,比如vector的(内存)连续性问

题。   然而,c++98和c++03最大的改变是增加了TR1库。TR1库包括了hash容器,正则表达式,tuples,

new   smart   pointer类,和一个数组类模板。
            关于TR1库具体描述可以点击   这里。标准库的执性行早已被激活,TR1仅仅是瓶开胃酒,C++所驶向
将是一场革命的风暴。

          右值引用
c++09   引入了一种新的引用类型,叫做右值引用,因此,你们所使用了10年的传统意义上的引用类型现

在叫做左值引用。与左值引用不同,右值引用可以绑定在右值,甚至他们不具备const   特性。右值引用

可使得程序员:

        .   消除   当执行移动语义时的不必要和昂贵的对象拷贝开销。
        .   允许   用户实行完美的向前推导方法,从而解决通用库的主要的可用性问题。
        .   解决了一个可用性问题:绑定一个右值到non-const引用将不会是逻辑错误。(译者注:现在是编译错误)    

        在讨论“移动语义”的实行性问题前,让我们先来看看右值引用的语法。
        尽管T&   是表示左值应用,但是T&&   是代表指向T的一个右值引用。考虑下面情况:
void   g(int   &   n);
void   h(int&&   n);
g(5);   //error,   5   is   an   rvalue
g(int());   //error,   temporaries   are   rvalues
h(5);   //OK
h(int());   //OK
S   s;
S&   rs=s;   //ordindary   lvalue   reference
S&&   rrs=S();   //OK,   bind   a   temporary   to   an   rvalue   reference
S&   illegal   =   S();//error,   bind   a   temp   to   an   lvalue   reference

    The   Move   Movement(?)


自从C++诞生以来,它就被看作是一种   copy-semantics   语言。最基本的构造语言比如   拷贝构造,赋值

表达式,和基于把已存在对象拷贝到新的目标对象的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>
const   T&   min(const   T&   x,   const   T&   y)  
{
  return   x   <   y?   x   :   y;
}


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   expressed   as   follows:

用户需要察看min的实体去了解T到底需要那些东西。对这个例子,T必须是一个类型,而其包括以两个T
为参数和返回值是布尔型的的operator <函数。根据这个“概念”,有相应以下的需求:
auto   concept   LessThanComparable  
{
  bool   operator <(T,   T);
};
The   definition   of   min()   uses   this   concept   as   follows:
min()的定义用以上的给念:

template <LessThanComparable   T>   //T   must   be   a   LessThanComparable   type
const   T&   min(const   T&   x,   const   T&   y)  
{
  return   x   <   y?   x   :   y;
}

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.
至此以后,无论何时,当编译器遇到一个min的实例,他会先确认T是否具有   LessThanComparable的条件

,如果没有,就会发布一个错误!

Removing   Tiny   Annoyances   from   C++
把小烦恼从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
vector   <list   <int> >   vli;   //compilation   error;   > >   treated   as   a   right   shift   operator
vector   <list   <int>   >   vli;   //OK


The   rationale   behind   this   C++98   restriction   was   the   "maximal   munch "   rule.   However,   current  

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:


Null   指针是添加到c++的另外一个例子。很多编程语言用一个特殊的关键字来指定空指针或引用。c++  

采用用标志   0。   这就应起表达不清楚地问题,特别在重载决议时。空指针提议解决了这个问题:

void   f(char   *);
void   f(int);
f(nullptr);   //calls   func   (char*)   because   nullptr   isn 't   int
f(0);   //calls   func(int)  


Delegating   constructors   (also   known   as   forwarding   constructors)   will   make   C#   and   Java  

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
{
private:
  S()   {cout < < "target   ctor " < <endl;}   #1
public:
  S(bool   b):   S()   {cout < < "delegating   ctor " < <endl;}   //invokes   target   ctor   first
};
S   obj(false);

The   output   shows   the   constructor   execution   order:

target   ctor
delegating   ctor




[解决办法]
mark~ up

热点排行