关于nullptr我的建议是:最好用nullptr代替NULL.
举例:
void f(int) {}
void f(bool) {}
void f(void*) {}
func(0);//调用f(int)
func(true);//调用f(bool)
func(NULL);//调用f(int) #define NULL 0
f(nullptr);//调用f(void*)
nullptr的优势在于它不再是一个整数类型.你可以理解为它是一个可以指向任意类型的指针.nullptr的类型实际上是std::nullptr_t.std::nullptr可以隐式转换为所有的原始指针,这使得nullptr表现的像可以指向任意类型的指针.
关于声明别名我的建议是:
尽量用using声明别名而不是用typedef
举例:
template<typename T>
using MyAllocList = std::list<T, MyAlloc<T> >
//使用MyAllocList<int> 等价于 使用std::list<int, MyAlloc<int> >
template<typename T>
struct MyAllocList {
typedef std::list<T, MyAlloc<T> > type;
};
//使用MyAllocList<int>::type 等价于 使用std::list<int, MyAlloc<int> >
很明显,上面的更简单一些.