|
声明:本文源自 Danny Kalev 在 2011 年 6 月 21 日发表的《The Biggest Changes in C++(www.cppentry.com)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++(www.cppentry.com) 标准委员会成员。 Lambda 表达式Lambda 表达式的形式是这样的:
- [capture](parameters)->return-type {body}
来看个计数某个字符序列中有几个大写字母的例子:
- 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++(www.cppentry.com)03 中,声明对象的同时必须指明其类型,其实大多数情况下,声明对象的同时也会包括一个初始值,C++(www.cppentry.com)11 在这种情况下就能够让你声明对象时不再指定类型了:
- auto x=0; //0 是 int 类型,所以 x 也是 int 类型
- auto c='a'; //char
- auto d=0.5; //double
- auto national_debt=14400000000000LL;//long long
这个特性在对象的类型很大很长的时候很有用,如:
- void func(const vector<int> &vi)
- {
- vector<int>::const_iterator ci=vi.begin();
- }
那个迭代器可以声明为:
C++(www.cppentry.com)11 也提供了从对象或表达式中“俘获”类型的机制,新的操作符 decltype 可以从一个表达式中“俘获”其结果的类型并“返回”:
- const vector<int> vi;
- typedef decltype (vi.begin()) CIT;
- CIT another_const_iterator;
统一的初始化语法C++(www.cppentry.com) 最少有 4 种不同的初始化形式,如括号内初始化,见:
- std::string s("hello");
- int m=int(); //default initialization
还有等号形式的:
- std::string s="hello";
- int x=5;
对于 POD 集合,又可以用大括号:
- int arr[4]={0,1,2,3};
- struct tm today={0};
最后还有构造函数的成员初始化:
- struct S {
- int x;
- S(): x(0) {} };
这么多初始化形式,不仅菜鸟会搞得很头大,高手也吃不消。更惨的是 C++(www.cppentry.com)03 中居然不能初始化 POD 数组的类成员,也不能在使用 new[] 的时候初始 POD 数组,操蛋啊!C++(www.cppentry.com)11 就用大括号一统天下了:
- class C
- {
- int a;
- int b;
- public:
- C(int i, int j);
- };
- C c {0,0}; //C++(www.cppentry.com)11 only. 相当于 C c(0,0);
- int* a = new int[3] { 1, 2, 0 }; /C++(www.cppentry.com)11 only
- class X {
- int a[4];
- public:
- X() : a{1,2,3,4} {} //C++(www.cppentry.com)11, 初始化数组成员
- };
还有一大好事就是对于容器来说,终于可以摆脱 push_back() 调用了,C++(www.cppentry.com)11中可以直观地初始化容器了:
- // C++(www.cppentry.com)11 container initializer
- vector vs<string>={ "first", "second", "third"};
- map singers =
- { {"Lady Gaga", "+1 (212) 555-7890"},
- {"Beyonce Knowles", "+1 (212) 555-0987"}};
而类中的数据成员初始化也得到了支持:
- class C
- {
- int a=7; //C++(www.cppentry.com)11 only
- public:
- C();
- };
deleted 函数和 defaulted 函数像以下形式的函数:
- struct A
- {
- A()=default; //C++(www.cppentry.com)11
- virtual ~A()=default; //C++(www.cppentry.com)11
- };
叫做 defaulted 函数,=default; 指示编译器生成该函数的默认实现。这有两个好处:一是让程序员轻松了,少敲键盘,二是有更好的性能。 与 defaulted 函数相对的就是 deleted 函数:
这货有一大用途就是实现 noncopyabe 防止对象拷贝,要想禁止拷贝,用 =deleted 声明一下两个关键的成员函数就可以了:
- struct NoCopy
- {
- NoCopy & operator =( const NoCopy & ) = delete;
- NoCopy ( const NoCopy & ) = delete;
- };
- NoCopy a;
- NoCopy b(a); //编译错误,拷贝构造函数是 deleted 函数
|