转:C++11中值得关注的几大变化

        C++11中值得关注的几大变化       

        分类:            C++ 54832人阅读 评论(103) 收藏 举报

赖勇浩(http://laiyonghao.com) 声明:本文源自 Danny Kalev 在 2011 年 6 月 21 日发表的《The Biggest Changes in C++11(and Why You Should Care)》一文,几乎所有内容都搬了过来,但不是全文照译,有困惑之处,请参详原文(http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-why-you-should-care/)。 注:作者 Danny Kalev 曾是 C++ 标准委员会成员。

Lambda 表达式

Lambda 表达式的形式是这样的:

  1. [capture](parameters)->return-type {body} 

[capture](parameters)->return-type {body}来看个计数某个字符序列中有几个大写字母的例子:

  1. int main() 
  2.    char s[]="Hello World!"
  3.    int Uppercase = 0; //modified by the lambda 
  4.    for_each(s, s+sizeof(s), [&Uppercase] (char c) { 
  5.     if (isupper(c)) 
  6.      Uppercase++; 
  7.     }); 
  8. cout<< Uppercase<<" uppercase letters in: "<< s<<endl; 

int main() { char s[]="Hello World!"; int Uppercase = 0; //modified by the lambda for_each(s, s+sizeof(s), [&Uppercase] (char c) { if (isupper(c)) Uppercase++; }); cout<< Uppercase<<" uppercase letters in: "<< s<<endl; }其中 [&Uppercase] 中的 & 的意义是 lambda 函数体要获取一个 Uppercase 引用,以便能够改变它的值,如果没有 &,那就 Uppercase 将以传值的形式传递过去。

 

自动类型推导和 decltype

在 C++03 中,声明对象的同时必须指明其类型,其实大多数情况下,声明对象的同时也会包括一个初始值,C++11 在这种情况下就能够让你声明对象时不再指定类型了:

[c-sharp] view plain copy print ?
  1. auto x=0; //0 是 int 类型,所以 x 也是 int 类型 
  2. auto c='a'; //char 
  3. auto d=0.5; //double 
  4. auto national_debt=14400000000000LL;//long long 

auto x=0; //0 是 int 类型,所以 x 也是 int 类型 auto c='a'; //char auto d=0.5; //double auto national_debt=14400000000000LL;//long long这个特性在对象的类型很大很长的时候很有用,如:

  1. void func(const vector<int> &vi) 
  2.   vector<int>::const_iterator ci=vi.begin(); 

void func(const vector<int> &vi) { vector<int>::const_iterator ci=vi.begin(); }那个迭代器可以声明为:

  1. auto ci=vi.begin(); 

auto ci=vi.begin();C++11 也提供了从对象或表达式中“俘获”类型的机制,新的操作符 decltype 可以从一个表达式中“俘获”其结果的类型并“返回”:

[c-sharp] view plain copy print ?
  1. const vector<int> vi; 
  2. typedef decltype (vi.begin()) CIT; 
  3. CIT another_const_iterator; 

const vector<int> vi; typedef decltype (vi.begin()) CIT; CIT another_const_iterator;

 

统一的初始化语法

C++ 最少有 4 种不同的初始化形式,如括号内初始化,见:

  1. std::string s("hello"); 
  2. int m=int(); //default initialization 

std::string s("hello"); int m=int(); //default initialization还有等号形式的:

  1. std::string s="hello"
  2. int x=5; 

std::string s="hello"; int x=5;对于 POD 集合,又可以用大括号:

  1. int arr[4]={0,1,2,3}; 
  2. structtm today={0}; 

int arr[4]={0,1,2,3}; struct tm today={0};最后还有构造函数的成员初始化:

  1. struct S { 
  2. int x; 
  3. S(): x(0) {} }; 

struct S { int x; S(): x(0) {} };这么多初始化形式,不仅菜鸟会搞得很头大,高手也吃不消。更惨的是 C++03 中居然不能初始化 POD 数组的类成员,也不能在使用 new[] 的时候初始 POD 数组,操蛋啊!C++11 就用大括号一统天下了:

  1. class
  2. int a; 
  3. int b; 
  4. public
  5. C(int i, int j); 
  6. }; 
  7. C c {0,0}; //C++11 only. 相当于 C c(0,0); 
  8. int* a = newint[3] { 1, 2, 0 }; /C++11 only 
  9. class X { 
  10.   int a[4]; 
  11. public
  12.   X() : a{1,2,3,4} {} //C++11, 初始化数组成员 
  13. }; 

class C { int a; int b; public: C(int i, int j); }; C c {0,0}; //C++11 only. 相当于 C c(0,0); int* a = new int[3] { 1, 2, 0 }; /C++11 only class X { int a[4]; public: X() : a{1,2,3,4} {} //C++11, 初始化数组成员 };还有一大好事就是对于容器来说,终于可以摆脱 push_back() 调用了,C++11中可以直观地初始化容器了:

  1. // C++11 container initializer 
  2. vector vs<string>={ "first", "second", "third"}; 
  3. map singers = 
  4.   { {"Lady Gaga", "+1 (212) 555-7890"}, 
  5.     {"Beyonce Knowles", "+1 (212) 555-0987"}}; 

// C++11 container initializer vector vs<string>={ "first", "second", "third"}; map singers = { {"Lady Gaga", "+1 (212) 555-7890"}, {"Beyonce Knowles", "+1 (212) 555-0987"}};而类中的数据成员初始化也得到了支持:

  1. class
  2. int a=7; //C++11 only 
  3. public
  4. C(); 
  5. }; 

class C { int a=7; //C++11 only public: C(); };

 

deleted 函数和 defaulted 函数

像以下形式的函数:

  1. struct
  2. A()=default; //C++11 
  3. virtual ~A()=default; //C++11 
  4. }; 

struct A { A()=default; //C++11 virtual ~A()=default; //C++11 };叫做 defaulted 函数,=default; 指示编译器生成该函数的默认实现。这有两个好处:一是让程序员轻松了,少敲键盘,二是有更好的性能。 与 defaulted 函数相对的就是 deleted 函数:

  1. int func()=delete

int func()=delete;这货有一大用途就是实现 noncopyabe 防止对象拷贝,要想禁止拷贝,用 =deleted 声明一下两个关键的成员函数就可以了:

  1. struct NoCopy 
  2.     NoCopy & operator =( const NoCopy & ) = delete
  3.     NoCopy ( const NoCopy & ) = delete
  4. }; 
  5. NoCopy a; 
  6. NoCopy b(a); //编译错误,拷贝构造函数是 deleted 函数 

struct NoCopy { NoCopy & operator =( const NoCopy & ) = delete; NoCopy ( const NoCopy & ) = delete; }; NoCopy a; NoCopy b(a); //编译错误,拷贝构造函数是 deleted 函数

 

nullptr

nullptr 是一个新的 C++ 关键字,它是空指针常量,它是用来替代高风险的 NULL 宏和 0 字面量的。nullptr 是强类型的:

  1. void f(int); //#1 
  2. void f(char *);//#2 
  3. //C++03 
  4. f(0); //调用的是哪个 f? 
  5. //C++11 
  6. f(nullptr) //毫无疑问,调用的是 #2 

void f(int); //#1 void f(char *);//#2 //C++03 f(0); //调用的是哪个 f? //C++11 f(nullptr) //毫无疑问,调用的是 #2所有跟指针有关的地方都可以用 nullptr,包括函数指针和成员指针:

  1. constchar *pc=str.c_str(); //data pointers 
  2. if (pc!=nullptr) 
  3.   cout<<pc<<endl; 
  4. int (A::*pmf)()=nullptr; //指向成员函数的指针 
  5. void (*pmf)()=nullptr; //指向函数的指针 

const char *pc=str.c_str(); //data pointers if (pc!=nullptr) cout<<pc<<endl; int (A::*pmf)()=nullptr; //指向成员函数的指针 void (*pmf)()=nullptr; //指向函数的指针

 

委托构造函数

C++11 中构造函数可以调用同一个类的另一个构造函数:

  1. class M //C++11 delegating constructors 
  2. int x, y; 
  3. char *p; 
  4. public
  5. M(int v) : x(v), y(0),  p(newchar [MAX])  {} //#1 target 
  6. M(): M(0) {cout<<"delegating ctor"<<end;} //#2 delegating 

class M //C++11 delegating constructors { int x, y; char *p; public: M(int v) : x(v), y(0), p(new char [MAX]) {} //#1 target M(): M(0) {cout<<"delegating ctor"<<end;} //#2 delegating#2 就是所谓的委托构造函数,调用了真正的构造函数 #1。

 

右值引用

在 C++03 中的引用类型是只绑定左值的,C++11 引用一个新的引用类型叫右值引用类型,它是绑定到右值的,如临时对象或字面量。 增加右值引用的主要原因是为了实现 move 语义。与传统的拷贝不同,move 的意思是目标对象“窃取”原对象的资源,并将源置于“空”状态。当拷贝一个对象时,其实代价昂贵且无必要,move 操作就可以替代它。如在 string 交换的时候,使用 move 意义就有巨大的性能提升,如原方案是这样的:

  1. void naiveswap(string &a, string & b) 
  2. string temp = a; 
  3. a=b; 
  4. b=temp; 

void naiveswap(string &a, string & b) { string temp = a; a=b; b=temp; }这种方案很傻很天真,很慢,因为需要申请内存,然后拷贝字符,而 move 就只需要交换两个数据成员,无须申请、释放内存和拷贝字符数组:

  1. void moveswapstr(string& empty, string & filled) 
  2. //pseudo code, but you get the idea 
  3. size_t sz=empty.size(); 
  4. constchar *p= empty.data(); 
  5. //move filled's resources to empty 
  6. empty.setsize(filled.size()); 
  7. empty.setdata(filled.data()); 
  8. //filled becomes empty 
  9. filled.setsize(sz); 
  10. filled.setdata(p); 

void moveswapstr(string& empty, string & filled) { //pseudo code, but you get the idea size_t sz=empty.size(); const char *p= empty.data(); //move filled's resources to empty empty.setsize(filled.size()); empty.setdata(filled.data()); //filled becomes empty filled.setsize(sz); filled.setdata(p); }要实现支持 move 的类,需要声明 move 构造函数和 move 赋值操作符,如下:

  1. class Movable 
  2. Movable (Movable&&); //move constructor 
  3. Movable&& operator=(Movable&&); //move assignment operator 
  4. }; 

class Movable { Movable (Movable&&); //move constructor Movable&& operator=(Movable&&); //move assignment operator };C++11 的标准库广泛使用 move 语义,很多算法和容器都已经使用 move 语义优化过了。

 

C++11 的标准库

除 TR1 包含的新容器(unordered_set, unordered_map, unordered_multiset, 和unordered_multimap),还有一些新的库,如正则表达式,tuple,函数对象封装器等。下面介绍一些 C++11 的标准库新特性:

线程库

从程序员的角度来看,C++11 最重要的特性就是并发了。C++11 提供了 thread 类,也提供了 promise 和 future 用以并发环境中的同步,用 async() 函数模板执行并发任务,和 thread_local 存储声明为特定线程独占的数据,这里(http://www.devx.com/SpecialReports/Article/38883)有一个简单的 C++11 线程库教程(英文)。

新的智能指针类

C++98 定义的唯一的智能指针类 auto_ptr 已经被弃用,C++11 引入了新的智能针对类 shared_ptr 和 unique_ptr。它们都是标准库的其它组件兼容,可以安全地把智能指针存入标准容器,也可以安全地用标准算法“倒腾”它们。

新的算法

主要是 all_of()、any_of() 和 none_of(),下面是例子:

  1. #include <algorithm> 
  2. //C++11 code 
  3. //are all of the elements positive? 
  4. all_of(first, first+n, ispositive()); //false 
  5. //is there at least one positive element? 
  6. any_of(first, first+n, ispositive());//true 
  7. // are none of the elements positive? 
  8. none_of(first, first+n, ispositive()); //false 

#include <algorithm> //C++11 code //are all of the elements positive? all_of(first, first+n, ispositive()); //false //is there at least one positive element? any_of(first, first+n, ispositive());//true // are none of the elements positive? none_of(first, first+n, ispositive()); //false还有一个新的 copy_n:

  1. #include <algorithm> 
  2. int source[5]={0,12,34,50,80}; 
  3. int target[5]; 
  4. //从 source 拷贝 5 个元素到 target 
  5. copy_n(source,5,target); 

#include <algorithm> int source[5]={0,12,34,50,80}; int target[5]; //从 source 拷贝 5 个元素到 target copy_n(source,5,target);iota() 算法可以用来创建递增序列,它先把初值赋值给 *first,然后用前置 ++ 操作符增长初值并赋值到给下一个迭代器指向的元素,如下:

  1. #include <numeric> 
  2. int a[5]={0}; 
  3. char c[3]={0}; 
  4. iota(a, a+5, 10); //changes a to {10,11,12,13,14} 
  5. iota(c, c+3, 'a'); //{'a','b','c'} 

#include <numeric> int a[5]={0}; char c[3]={0}; iota(a, a+5, 10); //changes a to {10,11,12,13,14} iota(c, c+3, 'a'); //{'a','b','c'} 是的,C++11 仍然缺少一些很有用的库如 XML API,socket,GUI、反射——以及自动垃圾收集。然而现有特性已经让 C++ 更安全、高效(是的,效率更高了,可以参见 Google 的 基准测试结果http://www.itproportal.com/2011/06/07/googles-rates-c-most-complex-highest-performing-language/)以及更加易于学习和使用。 如果觉得 C++ 变化太大了,不必惊恐,花点时间来学习就好了。可能在你融会贯通新特性以后,你会同意 Stroustrup 的观点:C++11 是一门新的语言——一个更好的 C++。

转载于:https://www.cnblogs.com/by1990/archive/2012/09/02/2667279.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值