- 模板的闭符之间不需要空格:vector<list<int> > 等同于 vector<list<int>>
- nullptr取代0或者NULL
- 以auto完成类型自动推导
- 一致性初始化与初值列;使用大括号进行初始化动作,如: int values[]{1,2,3} vector<int> v{1,2,3};初值列如:int j{}--j is initialized by 0; int*p{}--q is initialized by nullptr;
- range-based for ; 如 for(auto i: list/range/array/collection)
- move/rvalue reference
- 用以避免非必要拷贝(copy)和临时对象(temporary)
- std::move() 意味着“调用move()语义(若有提供的话),否则调用copy语义”
- c++标准库的class保证了,在一次move之后,对象处于有效但不确定的状态。也就是说,你可以在move之后对它赋予新值,但当前值是不确定的。STL容器则保证了,被搬移内容者,搬移后其值为空
- 新式的字符串字面常量。Raw string literal--R"\\\n"; 编码的(Encoded)string literal--u8"lsdkf" u“dd” U"kk" L"hello"
- noexcept--给编译器更大的优化空间。然而,并不是加上noexcept就能提高效率,步子迈大了也容易扯着蛋。防止异常扩散
- 用来指明莫哥函数无法或不打算抛出异常
- constexpr--让表达式核定于编译期
constexpr int square(int x) { return x*x; } float a[square(9)]; // OK since c++11 : a has 81 elements ------------ std::numeric_limits<int>::max() == INT_MAX
- 崭新的template特性
- 可以接受个数不定之template实参--variadic template;
void print(){ } template<typename T,typename... Types> void print(const T& first,const Types&... args) { cout<<first<<endl; print(...args); }
- 支持Alias Template/template typedef 通过using(带别名的模版);
- function template可拥有默认的template实参
- 可以接受个数不定之template实参--variadic template;
- lambda
- 功能定义式,可被定义于语句或者表达式内部。可以当作inline函数使用
- Lambda 的语法形式如下:
[函数对象参数] (操作符重载函数参数) mutable 或 exception 声明 -> 返回值类型 {函数体}
- decltype--可以让编译器找出表达式类型
- 其实就是常被要求的typeof的特性表现。只不过原有的typeof缺乏一致性又不安全,c++11才引入这么一个关键字
- 用途
- 声明返回值类型
template<typename T1, typename T2> decltype(x+y) add(T1 x,T2 y) --------------- template<typename T1, typename T2> auto add(T1 x,T2 y)->decltype(x+y)
- 在metaprogramming(超编程)或用来传递一个lambda类型
class Person{...} auto hash = [](const Person& p){...} auto eq = [](const Person& p1,const Person& p2){...} unordered_set<Person,decltype(hash),decltype(eq)> pset(10,hash,eq);
- 声明返回值类型
- 新的函数声明语法
template<typename T1,typename T2> decltype(x+y) add(T1 x,T2 y); template<typename T1,typename T2> auto add(T1 x,T2 y) -> decltype(x+y);
-