-
- 1.1. auto 关键字
- 1.2. decltype 关键字
- 1.3. template default value 模板默认值
- 类型推导
auto i = 0;
- 对于STL中迭代器的类型书写简化
map<int,int> m;
auto it = m.begin();
std::unordered_multimap<int, int> um;
// before
std::pair<
std::unordered_multimap<int, int>::iterator,
std::unordered_multimap<int, int>::iterator> range = um.equal_range(key);
// after
auto range2 = um.equal_range(key);
- 获知表达式类型
int x = 0;
decltype(x) y = 1;// y -> int
template <typename R = int, typename U>
R func(U val){
return val;
}
int main(){
auto ret1 = func(123);// return `int`
printType(ret1);// i
auto ret2 = func<long>(123);// return `long`
printType(ret2);// l
}