【C++ 2.0-侯捷老师】模板表达式中的空格,nullptr与auto

1.那个讨厌的小空格

vector<list<int> >;// OK in each C++ version,这样的写法每个版本都支持
vector<list<int>> ;// OK since C++ 11,C++11后的版本会支持

The requirement to put a space between two closing template expressions has gone.
自从十一版本发布,要求两个相连的模板类之间必须有一个空格的要求不复存在.
主要是在文件stl_bvector.h中实现了这一改变(GNU 4.9.2中可查看).

2.nullptr

C++11 lets you use nullptr instaed of 0 or NULL to specify that a pointer refers to no value(which differs ffrom having 
an undefined value).This new feature especially helps to avoid mistakes that occurred when a null pointer was interpreted as
 an intergral value.

For example:
void f(int);
void f(void*);
f(0);           //call f(int)
f(NULL);        //call f(int) if NULL is 0,ambigous otherwise 
f(nullptr);     //call f(void*)

nullptr is a new keyword. It automatically converts into each pointer type but not to any
interger type. 
It has type std::nullptr_t,defined in <cstddef>(since Section 5.8.1,page 161),so you can now even overload
 operations for the case that a null pointer is passed.Note that std::nullptr_t counts as a fundamental data type(see Section 5.4.2,page 127).

3.auto

With C++ 11,you can declare a variable or an object without specify its specific type by using auto. For example:

auto i = 42; // i has type int
double f();
auto d = f();// d has type double
auto b;      // 编译器不认识b,会编译失败,编译器无法知道这个b是什么类型



Using auto especially useful where the type is a pretty long and/or complicated expression.

For example:

[example1]
vector<string> v;
...
auto pos = v.begin;// pos has type vector<string>::iterator

[example2]
auto l = [](int x)->bool{...};//l has the type of lambda taking an int and returing a bool

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值