What exactly nullptr is in C++?
Tags: cpp, programming, computerscience, tutorial
Published: 2020-04-13 02:45:52

<!-- wp:paragraph -->
<p>The answer to "What exactly nullptr is in C++?" would be a piece of cake for experienced C++ eyes &amp; for those who are aware of <a rel="noreferrer noopener" aria-label="Modern C++ (opens in a new tab)" href="http://www.vishalchovatiya.com/21-new-features-of-modern-cpp-to-use-in-your-project/" target="_blank"><span style="color:#0300a3" class="color">Modern C++</span></a> i.e. keyword. But <code>nullptr</code> is more than just a keyword in C++ &amp; to explain that, I have written this article. But before jump-into it, we will see issues with <code>NULL</code>&nbsp;&amp; then we'll dive into the unsophisticated implementation of &nbsp;<code>nullptr</code>&nbsp;&amp; some use-cases of&nbsp;<code>nullptr</code>.</p>
<!-- /wp:paragraph -->

> /!\\\: Originally published @ [www.vishalchovatiya.com](http://www.vishalchovatiya.com/).

<!-- wp:paragraph -->
<p><strong><em>Note:</em></strong> This article is more or less the same thing which you can find <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr" target="_blank"><span style="color:#0a1fd8" class="color">here</span></a>, <a rel="noreferrer noopener" aria-label="here (opens in a new tab)" href="https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr/" target="_blank"><span style="color:#200df3" class="color">here</span></a> &amp; in <a rel="noreferrer noopener" aria-label="nullptr proposal(N2431) (opens in a new tab)" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf" target="_blank"><span style="color:#0c10f1" class="color">nullptr proposal(N2431)</span></a> but in a bit organized &amp; simplified way. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Why do we need nullptr?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph {"align":"center","backgroundColor":"very-light-gray"} -->
<p class="has-background has-text-align-center has-very-light-gray-background-color"><strong><em>To distinguish between an integer 0(zero)  i.e. NULL &amp; actual null of type pointer. </em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>nullptr vs NULL </h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>NULL</code>&nbsp;is <code>0</code>(zero) i.e. <strong>integer constant zero</strong> with <a rel="noreferrer noopener" aria-label="C-style typecast (opens in a new tab)" href="http://www.vishalchovatiya.com/cpp-type-casting-with-example-for-c-developers/" target="_blank"><span style="color:#2600a3" class="color">C-style typecast</span></a> to <code>void*</code>, while&nbsp;<code>nullptr</code>&nbsp;is <a rel="noreferrer noopener" aria-label="prvalue (opens in a new tab)" href="http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/" target="_blank"><span style="color:#0300a3" class="color">prvalue</span></a> of type <code>nullptr_t</code>&nbsp;which is <strong><em><a rel="noreferrer noopener" href="https://en.cppreference.com/w/cpp/language/integer_literal" target="_blank"><span style="color:#0010a3" class="color">integer literal</span></a></em></strong> <strong><em>evaluates to zero</em></strong>. </li><li>For those of you who believe that <code>NULL</code> is same i.e. <code>(void*)0</code> in C &amp; C++. I would like to clarify that no it's not:  </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="http://en.cppreference.com/w/c/types/NULL" target="_blank"><span style="color:#1200a3" class="color">NULL - cppreference.com</span></a>&nbsp;(C)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="http://en.cppreference.com/w/cpp/types/NULL" target="_blank"><span style="color:#0800a3" class="color">NULL - cppreference.com</span></a>&nbsp;(C++)</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>C++ requires that macro <code>NULL</code>&nbsp;to be defined as an integral constant expression having the value of <code>0</code>. So unlike in C, <code>NULL</code>&nbsp;cannot be defined as <code>(void *)0</code> in the C++ standard library.  </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Issues with NULL</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>1️⃣ <strong>Implicit conversion</strong> </h3>
<!-- /wp:heading -->

```c++
char *str = NULL; // Implicit conversion from void * to char *
int i = NULL;     // OK, but `i` is not pointer type
```

<!-- wp:heading {"level":3} -->
<h3>2️⃣ <strong>Function calling ambiguity</strong></h3>
<!-- /wp:heading -->

```c++
void func(int) {}
void func(int*){}
void func(bool){}
 
func(NULL);     // Which one to call?
```

<!-- wp:paragraph -->
<p>Compilation produces the following error: </p>
<!-- /wp:paragraph -->

```c++
error: call to 'func' is ambiguous
    func(NULL);
    ^~~~
note: candidate function void func(bool){}
                              ^
note: candidate function void func(int*){}
                              ^
note: candidate function void func(int){}
                              ^
1 error generated.
compiler exit status 1
```

<!-- wp:heading {"level":3} -->
<h3>3️⃣ <strong>Constructor overload</strong></h3>
<!-- /wp:heading -->

```c++
struct String
{
    String(uint32_t)    {   /* size of string */    }
    String(const char*) {       /* string */        }
};

String s1( NULL );
String s2( 5 ); 
```

<!-- wp:list -->
<ul><li>In such cases, you need explicit cast (i.e., <code>String s((char*)0))</code>.   </li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Implementation of unsophisticated nullptr</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>nullptr</code> is a subtle example of <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver" target="_blank"><span style="color:#001aa3" class="color">Return Type Resolver</span></a>&nbsp;idiom to automatically deduce a null pointer of the correct type depending upon the type of the instance it is assigning to. </li><li>Consider the following simplest &amp; unsophisticated&nbsp;<code>nullptr</code> implementation:</li></ul>
<!-- /wp:list -->

```c++
struct nullptr_t 
{
    void operator&() const = delete;  // Can't take address of nullptr
    
    template<class T>
    inline operator T*() const { return 0; }

    template<class C, class T>
    inline operator T C::*() const { return 0; }
};
nullptr_t nullptr;
```

<!-- wp:list -->
<ul><li>If the above code seems strange &amp; weird to you(although it should not), then I would suggest you go through my earlier article on <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver" target="_blank"><span style="color:#0010a3" class="color">advanced C++ concepts</span></a>. The magic here is just the templatized conversion operator.</li><li>If you are into a more authoritative source, then, here is a <a rel="noreferrer noopener" aria-label="concrete implementation of nullptr from LLVM header (opens in a new tab)" href="https://github.com/llvm-mirror/libcxx/blob/master/include/__nullptr" target="_blank"><span style="color:#160dc4" class="color">concrete implementation of nullptr from LLVM header</span></a>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Use-cases of nullptr </h2>
<!-- /wp:heading -->

```c++
struct C { void func(); };

int main(void)
{
    int *ptr = nullptr;                // OK
    void (C::*method_ptr)() = nullptr; // OK
    nullptr_t n1, n2;
    n1 = n2;
    //nullptr_t *null = &n1;           // Address can't be taken.
}
```

<!-- wp:list -->
<ul><li>As shown in the above example, when <code>nullptr</code> is being assigned to an integer pointer, a <code>int</code> type instantiation of the templatized conversion function is created.  And same goes for method pointers too. </li><li>This way by leveraging template functionality, we are actually creating the appropriate type of null pointer every time we do, a new type assignment.</li><li>As&nbsp;<code>nullptr</code> is an&nbsp;<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.cppreference.com/w/cpp/language/integer_literal" target="_blank"><span style="color:#2e0ac0" class="color">integer literal</span></a>&nbsp;with value zero, you can not able to use its address which we accomplished by deleting &amp; operator.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3> 1️⃣ Function calling clarity with nullptr</h3>
<!-- /wp:heading -->

```c++
void func(int)   { /* ... */}
void func(int *) { /* ... */}
void func(bool)  { /* ... */}

func(nullptr);
```

<!-- wp:list -->
<ul><li>Now, <code>func( int* )</code> will be called as&nbsp;<code>nullptr</code>&nbsp;will implicitly be deduced to <code>int*</code>. </li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3> 2️⃣ Typecasting on nullptr_t</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>A cast of&nbsp;<code>nullptr_t</code>&nbsp;to an integral type needs a&nbsp;<code>reinterpret_cast</code>, and has the same semantics as a cast of&nbsp;<code>(void*)0</code>&nbsp;to an integral type. </li><li>Casting&nbsp;<code>nullptr_t</code>&nbsp;to an integral type holds true as long as destination type is large enough. Consider this:</li></ul>
<!-- /wp:list -->

```c++
// int ptr_not_ok = reinterpret_cast<int>(nullptr); // Not OK
long ptr_ok = reinterpret_cast<long long>(nullptr); // OK
```

<!-- wp:list -->
<ul><li> A&nbsp;<code>reinterpret_cast</code>&nbsp;cannot convert&nbsp;<code>nullptr_t</code>&nbsp;to any pointer type. Use&nbsp;<code>static_cast</code> instead.  </li></ul>
<!-- /wp:list -->

```c++
void func(int*)    { /*...*/ }
void func(double*) { /*...*/ }

func(nullptr);                            // compilation error, ambiguous call!

// func(reinterpret_cast<int*>(nullptr)); // error: invalid cast from type 'std::nullptr_t' to type 'int*'
func(static_cast<int*>(nullptr));         // OK
```

<!-- wp:list -->
<ul><li><code>nullptr</code>&nbsp;is implicitly convertible to any pointer type so explicit conversion with&nbsp;<code>static_cast</code>&nbsp;is only valid. </li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>  3️⃣ nullptr_t is comparable </h3>
<!-- /wp:heading -->

```c++
int *ptr = nullptr;
if (ptr == 0);          // OK
if (ptr <= nullptr);    // OK        

int a = 0;
if (a == nullptr);      // error: invalid operands of types 'int' and 'std::nullptr_t' to binary 'operator=='
```

>
<!-- wp:paragraph {"backgroundColor":"very-light-gray"} -->
<p class="has-background has-very-light-gray-background-color">From&nbsp;<a href="https://en.wikipedia.org/wiki/C%2B%2B11#Null_pointer_constant" target="_blank" rel="noreferrer noopener" aria-label="Wikipedia(opens in a new tab)"><span style="color:#0f1ae4" class="color">Wikipedia</span></a>:<be>
- …null pointer constant: <code>nullptr</code>. It is of type <code>nullptr_t</code>, which is implicitly convertible and comparable to any pointer type or pointer-to-member type. <br>- It is not implicitly convertible or comparable to integral types, except for <code>bool</code>.</p>
<!-- /wp:paragraph -->

```c++
const int a = 0;
if (a == nullptr); // OK

const int b = 5;
if (b == nullptr); // error: invalid operands of types 'const int' and 'std::nullptr_t' to binary 'operator=='
```

<!-- wp:heading {"level":3} -->
<h3> 4️⃣ Template-argument is of type std::nullptr_t</h3>
<!-- /wp:heading -->

```c++
template <typename T>
void ptr_func(T *t) {}

ptr_func(nullptr);         // Can not deduce T
```

<!-- wp:list -->
<ul><li>As discussed earlier, <a rel="noreferrer noopener" href="http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver" target="_blank"><span style="color:#1200a3" class="color">Return Type Resolver</span></a> needs an assignee to deduce the type.</li></ul>
<!-- /wp:list -->

```c++
template <typename T>
void val_func(T t) {}

val_func(nullptr);         // deduces T = nullptr_t
val_func((int*)nullptr);   // deduces T = int*, prefer static_cast though
```

<!-- wp:heading {"level":3} -->
<h3> 5️⃣ Conversion to bool from nullptr_t</h3>
<!-- /wp:heading -->

>
<!-- wp:paragraph {"backgroundColor":"very-light-gray"} -->
<p class="has-background has-very-light-gray-background-color">From <a rel="noreferrer noopener" aria-label="cppreference  (opens in a new tab)" href="https://en.cppreference.com/w/cpp/language/implicit_conversion#Boolean_conversions" target="_blank"><span style="color:#0007a3" class="color">cppreference </span></a>:<br>- In the context of a&nbsp;<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.cppreference.com/w/cpp/language/direct_initialization" target="_blank"><span style="color:#0800a3" class="color">direct-initialization</span></a>, a&nbsp;<code>bool</code>&nbsp;object may be initialized from a prvalue of type&nbsp;<a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.cppreference.com/w/cpp/types/nullptr_t" target="_blank"><span style="color:#2100a3" class="color"><code>std::nullptr_t</code></span></a>, including&nbsp;<code>nullptr</code>. The resulting value is&nbsp;false. However, this is not considered to be an implicit conversion.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>The conversion is only allowed for&nbsp;<a rel="noreferrer noopener" aria-label="direct-initialization (opens in a new tab)" href="http://en.cppreference.com/w/cpp/language/direct_initialization" target="_blank"><span style="color:#1700a3" class="color">direct-initialization</span></a>, but not&nbsp;<a rel="noreferrer noopener" aria-label="copy-intialization (opens in a new tab)" href="http://en.cppreference.com/w/cpp/language/copy_initialization" target="_blank"><span style="color:#0d00a3" class="color">copy-intialization</span></a>, which including the case for passing an argument to a function by value. e.g. </li></ul>
<!-- /wp:list -->

```c++
bool b1 = nullptr; // Not OK
bool b2 {nullptr}; // OK

void func(bool){}
 
func(nullptr);     // Not OK, need to do func(static_cast<bool>(nullptr));
```

<!-- wp:heading {"level":3} -->
<h3> 6️⃣ Misc</h3>
<!-- /wp:heading -->

```c++
typeid(nullptr);                            // OK
throw nullptr;                              // OK
char *ptr = expr ? nullptr : nullptr;       // OK
// char *ptr1 = expr ? 0 : nullptr;         // Not OK, types are not compatible
static_assert(sizeof(NULL) == sizeof(nullptr_t));
```

<!-- wp:heading -->
<h2>Summary by FAQs</h2>
<!-- /wp:heading -->

<!-- wp:yoast/faq-block {"questions":[{"id":"faq-question-1573541391140","question":["When was ",{"type":"code","props":{"children":["nullptr"]}}," introduced?"],"answer":["C++11"],"jsonQuestion":"When was \u003ccode\u003enullptr\u003c/code\u003e introduced?","jsonAnswer":"C++11"},{"id":"faq-question-1573541392995","question":["Is ",{"type":"code","props":{"children":["nullptr"]}}," a keyword or an instance of a type ",{"type":"code","props":{"children":["std::nullptr_t"]}},"? "],"answer":["Both ",{"type":"code","props":{"children":["true"]}}," and ",{"type":"code","props":{"children":["false"]}}," are keywords \u0026 literals, as they have a type ( ",{"type":"code","props":{"children":["bool"]}}," ). ",{"type":"code","props":{"children":["nullptr"]}}," is a ",{"type":"em","props":{"children":["pointer literal"]}}," of type ",{"type":"code","props":{"children":["std::nullptr_t"]}},", \u0026 it's a prvalue (i.e. pure ",{"type":"a","props":{"href":"http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/","target":"_blank","rel":"noreferrer noopener","aria-label":"rvalue (opens in a new tab)","children":[{"type":"span","props":{"style":"color:#0002a3","class":"color","children":["rvalue"]}}]}},", you cannot take the address of it using ",{"type":"code","props":{"children":["\u0026"]}},"). ",{"type":"a","props":{"rel":"noreferrer noopener","aria-label":"For more (opens in a new tab)","href":"https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr","target":"_blank","children":[{"type":"span","props":{"style":"color:#1e1af0","class":"color","children":["For more"]}}]}},"."],"jsonQuestion":"Is \u003ccode\u003enullptr\u003c/code\u003e a keyword or an instance of a type \u003ccode\u003estd::nullptr_t\u003c/code\u003e? ","jsonAnswer":"Both \u003ccode\u003etrue\u003c/code\u003e and \u003ccode\u003efalse\u003c/code\u003e are keywords \u0026amp; literals, as they have a type ( \u003ccode\u003ebool\u003c/code\u003e ). \u003ccode\u003enullptr\u003c/code\u003e is a \u003cem\u003epointer literal\u003c/em\u003e of type \u003ccode\u003estd::nullptr_t\u003c/code\u003e, \u0026amp; it's a prvalue (i.e. pure \u003ca href=\u0022http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/\u0022 target=\u0022_blank\u0022 rel=\u0022noreferrer noopener\u0022 aria-label=\u0022rvalue (opens in a new tab)\u0022\u003e\u003cspan style=\u0022color:#0002a3\u0022 class=\u0022color\u0022\u003ervalue\u003c/span\u003e\u003c/a\u003e, you cannot take the address of it using \u003ccode\u003e\u0026amp;\u003c/code\u003e). \u003ca rel=\u0022noreferrer noopener\u0022 aria-label=\u0022For more (opens in a new tab)\u0022 href=\u0022https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr\u0022 target=\u0022_blank\u0022\u003e\u003cspan style=\u0022color:#1e1af0\u0022 class=\u0022color\u0022\u003eFor more\u003c/span\u003e\u003c/a\u003e."},{"id":"faq-question-1574585833280","question":["What are the advantages of using nullptr?"],"answer":["- No function calling ambiguity between overload sets.",{"type":"br","props":{"children":[]}},"- You can do template specialization with ",{"type":"code","props":{"children":["nullptr_t"]}},".",{"type":"br","props":{"children":[]}},"- Code will become more safe, intuitive  \u0026 expressive.  ",{"type":"code","props":{"children":["if (ptr == nullptr);"]}}," rather than ",{"type":"code","props":{"children":["if (ptr == 0);"]}},"."],"jsonQuestion":"What are the advantages of using nullptr?","jsonAnswer":"- No function calling ambiguity between overload sets.\u003cbr/\u003e- You can do template specialization with \u003ccode\u003enullptr_t\u003c/code\u003e.\u003cbr/\u003e- Code will become more safe, intuitive  \u0026amp; expressive.  \u003ccode\u003eif (ptr == nullptr);\u003c/code\u003e rather than \u003ccode\u003eif (ptr == 0);\u003c/code\u003e."},{"id":"faq-question-1574586261065","question":["Is ",{"type":"code","props":{"children":["NULL"]}}," in C++ equal to ",{"type":"code","props":{"children":["nullptr"]}}," from C++11?"],"answer":["Not at all. The following line does not even compile:",{"type":"br","props":{"children":[]}},{"type":"br","props":{"children":[]}},{"type":"code","props":{"children":["cout\u003c\u003cis_same_v\u003cnullptr, NULL\u003e\u003c\u003cendl;"]}}],"jsonQuestion":"Is \u003ccode\u003eNULL\u003c/code\u003e in C++ equal to \u003ccode\u003enullptr\u003c/code\u003e from C++11?","jsonAnswer":"Not at all. The following line does not even compile:\u003cbr/\u003e\u003cbr/\u003e\u003ccode\u003ecout\u0026lt;\u0026lt;is_same_v\u0026lt;nullptr, NULL\u003e\u0026lt;\u0026lt;endl;\u003c/code\u003e"},{"id":"faq-question-1574586433936","question":["Can I convert ",{"type":"code","props":{"children":["nullptr"]}}," to bool?"],"answer":["Yes. But only if you ",{"type":"a","props":{"rel":"noreferrer noopener","href":"http://en.cppreference.com/w/cpp/language/direct_initialization","target":"_blank","children":[{"type":"span","props":{"style":"color:#2a00a3","class":"color","children":["direct-initialization"]}}]}},". i.e. ",{"type":"code","props":{"children":["bool is_false{nullptr};"]}},". Else need to use ",{"type":"code","props":{"children":["static_cast"]}},"."],"jsonQuestion":"Can I convert \u003ccode\u003enullptr\u003c/code\u003e to bool?","jsonAnswer":"Yes. But only if you \u003ca rel=\u0022noreferrer noopener\u0022 href=\u0022http://en.cppreference.com/w/cpp/language/direct_initialization\u0022 target=\u0022_blank\u0022\u003e\u003cspan style=\u0022color:#2a00a3\u0022 class=\u0022color\u0022\u003edirect-initialization\u003c/span\u003e\u003c/a\u003e. i.e. \u003ccode\u003ebool is_false{nullptr};\u003c/code\u003e. Else need to use \u003ccode\u003estatic_cast\u003c/code\u003e."},{"id":"faq-question-1575106390754","question":["How is ",{"type":"code","props":{"children":["nullptr"]}}," defined?"],"answer":["It's just the templatized conversion operator known as ",{"type":"a","props":{"href":"http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver","target":"_blank","rel":"noreferrer noopener","aria-label":"Return Type Resolver (opens in a new tab)","children":[{"type":"span","props":{"style":"color:#0d00a3","class":"color","children":["Return Type Resolver"]}}]}},"."],"jsonQuestion":"How is \u003ccode\u003enullptr\u003c/code\u003e defined?","jsonAnswer":"It's just the templatized conversion operator known as \u003ca href=\u0022http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver\u0022 target=\u0022_blank\u0022 rel=\u0022noreferrer noopener\u0022 aria-label=\u0022Return Type Resolver (opens in a new tab)\u0022\u003e\u003cspan style=\u0022color:#0d00a3\u0022 class=\u0022color\u0022\u003eReturn Type Resolver\u003c/span\u003e\u003c/a\u003e."}]} -->
<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section" id="faq-question-1573541391140"><strong class="schema-faq-question">When was <code>nullptr</code> introduced?</strong> <p class="schema-faq-answer">C++11</p> </div> <div class="schema-faq-section" id="faq-question-1573541392995"><strong class="schema-faq-question">Is <code>nullptr</code> a keyword or an instance of a type <code>std::nullptr_t</code>? </strong> <p class="schema-faq-answer">Both <code>true</code> and <code>false</code> are keywords &amp; literals, as they have a type ( <code>bool</code> ). <code>nullptr</code> is a <em>pointer literal</em> of type <code>std::nullptr_t</code>, &amp; it's a prvalue (i.e. pure <a href="http://www.vishalchovatiya.com/lvalue-rvalue-and-their-references-with-example-in-cpp/" target="_blank" rel="noreferrer noopener" aria-label="rvalue (opens in a new tab)"><span style="color:#0002a3" class="color">rvalue</span></a>, you cannot take the address of it using <code>&amp;</code>). <a rel="noreferrer noopener" aria-label="For more (opens in a new tab)" href="https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr" target="_blank"><span style="color:#1e1af0" class="color">For more</span></a>.</p> </div> <div class="schema-faq-section" id="faq-question-1574585833280"><strong class="schema-faq-question">What are the advantages of using nullptr?</strong> <p class="schema-faq-answer">- No function calling ambiguity between overload sets.<br/>- You can do template specialization with <code>nullptr_t</code>.<br/>- Code will become more safe, intuitive  &amp; expressive.  <code>if (ptr == nullptr);</code> rather than <code>if (ptr == 0);</code>.</p> </div> <div class="schema-faq-section" id="faq-question-1574586261065"><strong class="schema-faq-question">Is <code>NULL</code> in C++ equal to <code>nullptr</code> from C++11?</strong> <p class="schema-faq-answer">Not at all. The following line does not even compile:<br/><br/><code>cout&lt;&lt;is_same_v&lt;nullptr, NULL>&lt;&lt;endl;</code></p> </div> <div class="schema-faq-section" id="faq-question-1574586433936"><strong class="schema-faq-question">Can I convert <code>nullptr</code> to bool?</strong> <p class="schema-faq-answer">Yes. But only if you <a rel="noreferrer noopener" href="http://en.cppreference.com/w/cpp/language/direct_initialization" target="_blank"><span style="color:#2a00a3" class="color">direct-initialization</span></a>. i.e. <code>bool is_false{nullptr};</code>. Else need to use <code>static_cast</code>.</p> </div> <div class="schema-faq-section" id="faq-question-1575106390754"><strong class="schema-faq-question">How is <code>nullptr</code> defined?</strong> <p class="schema-faq-answer">It's just the templatized conversion operator known as <a href="http://www.vishalchovatiya.com/7-advanced-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver" target="_blank" rel="noreferrer noopener" aria-label="Return Type Resolver (opens in a new tab)"><span style="color:#0d00a3" class="color">Return Type Resolver</span></a>.</p> </div> </div>
<!-- /wp:yoast/faq-block -->

[Have any suggestions, query or wants to say `Hi`? Take the Pressure Off, you are just a click away.](http://www.vishalchovatiya.com/contact-2/)🖱️