The answer to “What exactly nullptr is in C++?” would be a piece of cake for experienced C++ eyes & for those who are aware of Modern C++ i.e. keyword. But nullptr is more than just a keyword in C++ & to explain that, I have written this article. But before jump-into it, we will see issues with NULL & then we’ll dive into the unsophisticated implementation of nullptr & some use-cases of nullptr.
Why do we need nullptr?
To distinguish between an integer 0(zero) i.e. NULL & actual null of type pointer.
nullptr vs NULL
NULLis0(zero) i.e. integer constant zero with C-style typecast tovoid*, whilenullptris prvalue of typenullptr_twhich is integer literal evaluates to zero.- For those of you who believe that
NULLis same i.e.(void*)0in C & C++. I would like to clarify that no it’s not:
NULL - cppreference.com (C++)
- C++ requires that macro
NULLto be defined as an integral constant expression having the value of0. So unlike in C,NULLcannot be defined as(void *)0in the C++ standard library.
Issues with NULL
Implicit conversion
Function calling ambiguity
Compilation produces the following error:
Constructor overload
- In such cases, you need explicit cast (i.e., `String s((char*)0)).
Implementation of unsophisticated nullptr
nullptris a subtle example of Return Type Resolver idiom to automatically deduce a null pointer of the correct type depending upon the type of the instance it is assigning to.- Consider the following simplest & unsophisticated
nullptrimplementation:
- If the above code seems strange & weird to you(although it should not), then I would suggest you go through my earlier article on advanced C++ concepts. The magic here is just the templatized conversion operator.
- If you are into a more authoritative source, then, here is a concrete implementation of nullptr from LLVM header.
Use-cases of nullptr
- As shown in the above example, when
nullptris being assigned to an integer pointer, ainttype instantiation of the templatized conversion function is created. And same goes for method pointers too. - This way by leveraging C++ template functionality, we are actually creating the appropriate type of null pointer every time we do, a new type assignment.
- As
nullptris an integer literal with value zero, you can not able to use its address which we accomplished by deleting & operator.
Function calling clarity with nullptr
- Now,
func( int* ) will be called asnullptrwill implicitly be deduced toint*`.
Typecasting on nullptr_t
- A cast of
nullptr_tto an integral type needs areinterpret_cast, and has the same semantics as a cast of(void*)0to an integral type. - Casting
nullptr_tto an integral type holds true as long as destination type is large enough. Consider this:
- A
reinterpret_castcannot convertnullptr_tto any pointer type. Usestatic_castinstead.
nullptris implicitly convertible to any pointer type so explicit conversion withstatic_castis only valid.
nullptr_t is comparable
From Wikipedia article:
- …null pointer constant:
nullptr. It is of typenullptr_t, which is implicitly convertible and comparable to any pointer type or pointer-to-member type. - It is not implicitly convertible or comparable to integral types, except for
bool.
Template-argument is of type std::nullptr_t
- As discussed earlier, Return Type Resolver needs an assignee to deduce the type.
Conversion to bool from nullptr_t
From cppreference :
In the context of a direct-initialization, a
boolobject may be initialized from a prvalue of typestd::nullptr_t, includingnullptr. The resulting value is false. However, this is not considered to be an implicit conversion.The conversion is only allowed for direct-initialization, but not copy-intialization, which including the case for passing an argument to a function by value. e.g.
Misc
Summary by FAQs
When was nullptr introduced?
C++11
Is nullptr a keyword or an instance of a type std::nullptr_t?
Both true and false are keywords & literals, as they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, & it’s a prvalue (i.e. pure rvalue, you cannot take the address of it using &). For more.
What are the advantages of using nullptr?
- No function calling ambiguity between overload sets.
- You can do template specialization with
nullptr_t. - Code will become more safe, intuitive & expressive.
if (ptr == nullptr);rather thanif (ptr == 0);.
Is NULL in C++ equal to nullptr from C++11?
Not at all. The following line does not even compile:
cout<<is_same_v<nullptr, NULL><<endl;
Can I convert nullptr to bool?
Yes. But only if you direct-initialization. i.e. bool is_false{nullptr};. Else need to use static_cast.
How is nullptr defined?
It’s just the templatized conversion operator known as Return Type Resolver.
What exactly nullptr is in C++?
References
You can find similar resources here, here, and in nullptr proposal(N2431); however, this post will walk you through the ins and outs of the spec step-by-step in a more friendly way so that you come away with a full understanding of the concept without any needless confusion
