Article 8 of "Effective Modern C++" study notes: nullptr is preferred instead of 0 or NULL

0 is the int type. In some cases, it can barely be interpreted as a null pointer, but remember that this is a last resort scenario. There are different types of NULL in different systems, some are int, some are long, and their essence is also an integer. But nullptr is different, although its essence is std::nullptr_t, but because it can be implicitly converted to all types of raw pointer types. So when the function is overloaded, nullptr can perfectly match the overloaded function of the pointer type:

void f(int);
void f(bool);
void f(void*);

f(0);   //匹配到void f(int)

f(NULL);   //匹配到void f(int)

f(nullptr);   //匹配到void f(void*)

Shorthand

  • Compared with 0 or NULL, nullptr is preferred
  • Avoid overloading between integer and pointer types

 

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/114187946