Before C++11, people were using the NULL macro, baaack from C. In most compilers, this macro definition is:

#define NULL (void*)0

Duuude, isn’t this problematic? Sure, it is a pointer, but it is pretty obvious that it can be cast to integral types. This can work:

int a = (int)NULL;

What about another problem? Yeah, there is one more. Since NULL is easily cast to integral types, using NULL as an argument for a function may lead to unambiguous calls, and compilation problems.

void foo(char*) {
//Stuff
}

void foo(int) {
//Stuff
}

foo(NULL);//This can call both, so it won't compile.

Sutter and Stroustrup suggested std::nullptr 14 years ago. Their goals were:

  1. nullptr should be a reserved word.
  2. nullptr should not be usable in an arithmetic expression by conversion to integral types.
  3. nullptr can be converted to any pointer type, but no integral type.
  4. 0 does not implicitly convert to any pointer type. Thus, nullptr, a pointer in the type of std::nullptr_t born.

An Observation

Although it is 14 years old, I see that std::nullptr is not fully adopted. People are still using NULL, even if it is not defined, they #define it. This is probably because people are reluctant to change their habits. When they see a new property, they:

  1. Either think it is not needed, old thing is “enough”, whatever that is. These people are so reluctant, they don’t even read what it is. They oppose change and are probably the biggest obstacle in front of C++’s improvement.
  2. Read what it is, understand what it is, and judge it is not worth changing old codebases or habit. These people are hard to change, but at least they are more reasonable.
  3. Read it, understand it, and change their codebases and habits. These people are adopters.

Also, there are people who are new to C++. I hardly suggest them to keep up with new content instead. Learning what NULL is will not improve your performance anyways. Learn the new stuff, and best practices instead. C++ is so wide, learning best practices will consume your life. Don’t waste your time learning old shit.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *