What is nullptr in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The nullptr is used to denote a null pointer introduced in C++11. Also, nullptr is a pointer that doesn't point toward anything. C++11 also introduced a typedef nullptr_t defined in the header <cstddef>. We can also say that nullptr is a prvalue of type nullptr_t.

prvalue ("pure" rvalue) simply means an expression used to compute the value of an operand (no result object of prvalue) or initializes an object (prvalue have a result object)

Nullptr vs NULL

  • For nullptr, C++ requires macro NULL to be defined as 0 the integral constant expression unlike in C, NULL is not defined as (void *)0 in the C++ library.
  • The nullptr is a prvalue of type nullptr_t that converts integral literal to 0 while the typecast of NULL is (void *)0 whose integer constant is 0.

Issues with NULL

Implicit Conversion

Function Calling Ambiguity

Ambiguity occurs when calling the function by passing NULL as an argument which makes it tough to decide which one to call. But using nullptr it becomes easy as it will directly call the pointer value.

Error

Constructor Overload

Example Code Showing Problems with NULL

Issue with NULL

NULL is defined as (void *)0 (null pointer constant) and in NULL conversion to integral types is allowed. This is the reason why the error says that the Number(NULL) is ambiguous.

As you can see when we call the function by passing NULL as an argument it gives an error saying the call of an overloaded function is ambiguous. Ideally, when calling the function by passing NULL it should return the pointer but instead, it gives an error.

But nullptr can be used at any place where NULL is expected because nullptr is implicitly convertible and comparable to any pointer type.

Why do We Need Nullptr?

We need nullptr to distinguish between NULL (integer 0(zero)) and actual null of type pointer.

How Does Nullptr Solve the Above Problems?

Using nullptr_t we can do template specialization. The error of function calling ambiguity due to overloading is resolved. The code becomes safer, more intuitive, and more expressive.

Implementation of unsophisticated Nullptr

The nullptr is an elusive example of a Return type Resolver idiom to deduce the correct type of null pointer which depend upon the type of the instance it is assigned to. The following code shows the unsophisticated implementation of nullptr.

Use-cases of Nullptr

The nullptr is an integral literal which cannot be used for its address as nullptr is accomplished by deleting the & operator.

1. Function Calling Clarity with Nullptr

Result In the result nullFunc(int *) will be called because here nullptr will implicitly deduce to int *.

2. Typecasting on Nullptr_t

  • The reinterpret_cast is needed in the case-to-case nullptr_t to an integral type and has the same definition as a cast of (void*)0 to an integral type.
  • If the destination is large enough then casting nullptr_t to an integral type holds.
  • But reinterpret_cast fails in converting nullptr_t to any other pointer type.
  • The nullptr can be easily converted to any pointer type therefore only explicit conversion with static_cast is coherent.

3. Nullptr_t is Comparable

Return type resolver is also known as Return type overloading which is used to deduce the type of the object being initialized or assign to and uses templatized conversion operator.

4. Template-Argument is of Type Std::Nullptr_t

The above program throws the following error

As we have already discussed Return Type resolver in the article, we need an assignee to deduce the type to resolve the above error.

Therefore, we can go with the following code:

We can use static_cast here but without using it also the code will give the same result.

Result

5. Conversion to Bool from Std::nullptr_t

The conversion to bool is not allowed for copy initialization which also includes the case of passing an argument to a function value but is only allowed for direct initialization.

6. Other Uses

We can use nullptr in the following ways as well: -

Examples

Use Nullptr and Zero Interchangeably

The nullptr and zero can be used interchangeably on native pointers.

Result

Interpret nullptr as a Handle

In case of function overloading, ambiguity, and error are generated therefore to avoid the error nullptr have to be explicitly cast to a type.

Cast Nullptr

The casting of nullptr is allowed and it returns a pointer or the handle to the cast type that contains nullptr.

Pass Nullptr as A Function Parameter

Default Initialization

We can initialize the value as nullptr while declaring the pointer.

Assign Nullptr to a Native Pointer

We can assign nullptr to a native pointer as we can see from the following code.

FAQs Regarding Nullptr

Is Nullptr a Keyword or an Instance of a Type Std::Nullptr_t?

The nullptr is prvalue i.e., pure rvalue which means you cannot take its address of it using &. The nullptr denotes pointer literals which are of type nullptr_t.

What are the Advantages of Using Nullptr?

  • There will be no ambiguity between overloaded sets while calling functions.
  • if(ptr == nullptr) rather than (ptr == 0), the code becomes safer, more expressive, and more insightful.
  • Using nullptr_t we can do Template Specialization.

FAQs

Q. Can Nullptr be Converted to Bool?

A. Yes, you can convert nullptr to bool only if you initialize an object from an explicit set of constructor arguments (direct-initialization:: bool is_false{nullptr}) otherwise you need to use static_cast.

Q. How Is Nullptr Defined?

A. The nullptr is just a templatized conversion operator which is known as a Return type resolver that is used to deduce the correct type of the object depending upon the pointer variable assigned to it.

Q. Is NULL in C++ Equal to Nullptr in C++11?

A. No, NULL is not equal to nullptr. The following code doesn't even compile.

Conclusion

  • Nullptr is introduced in C++11 which doesn't point towards any memory location.
  • The nullptr C++ is a templatized conversion operator that helps in distinguishing between NULL and actual null of type pointer.
  • Nullptr can be converted to bool only when the initialization of an object is done from an explicit set of constructor arguments.
  • There are a few use cases of nullptr: - function calling ambiguity is resolved, conversion of bool from nullptr_t can be done, nullptr is comparable.
  • Using NULL, we face different kinds of issues like function calling ambiguity because of overloaded functions, constructor overloading, etc therefore nullptr is used to overcoming the issues.