nullptr和std::nullptr_t
C++11 let you use nullptr instead of 0 or NULL to specify that a pointer refers to no value.
For example:
void f(int);
void f(void*);
f(0); /*calls f(int)*/
f(NULL); /*calls f(int) if NULL is 0, ambiguous otherwise*/
f(nullptr); /*calls f(void*)*/
nullptr_t defined in <cstddef>
:
typedef decltype(nullptr) nullptr_t;
补充:
decltype
将变量的类型声明为表达式指定的类型。
decltype(x) y;
的含义是:让y的类型与x相同,其中x是一个表达式。示例:
double x; int n; decltype(x*n) q; /*q same type as x*n (double)*/ decltype(&x) pd; /*pd same type as &x (double)*/
这在定义模板的时候特别有用:
template<typename T, typename U> void ef(T t, U u) { decltype(T*U) tu; /*tu将为表达式T*U的类型*/ }
decltype
的工作原理比auto
复杂,根据使用的表达式,指定的类型可以为引用和const
:int j = 3; int &k = j; const int &n = j; decltype(n) i1; /*i1 type const int &*/ decltype(j) i2; /*i2 type int*/ decltype((j)) i3; /*i3 type int&*/ decltype(k + 1) i4; /*i4 type int*/
auto关键字
You can declare a variable or an object without specifying its specific type by using auto.
For example:
auto i = 42; /*i has type int*/
double f();
auto d = f(); /*d has type double*/
Using auto is especially useful where the type is a pretty long and/or complicated expression.
For example:
vector<string> v;
...
auto pos = v.begin(); /*pos has type vector<string>::iterator*/
auto I = [](int x) -> bool { /*I has type of a lambda*/
... /*taking an int and returning a bool*/
};