|
| 1 | +# nullptr - Pointer Literal |
| 2 | + |
| 3 | +`nullptr` is a **pointer literal** introduced in C++11, used to represent null pointers. It addresses the shortcomings of traditional null pointer representations (such as `NULL` and `0`) in terms of type safety and overload resolution. |
| 4 | + |
| 5 | +| Book | Video | Code | X | |
| 6 | +| --- | --- | --- | --- | |
| 7 | +| [cppreference](https://en.cppreference.com/w/cpp/language/nullptr) / [markdown](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/src/cpp11/12-nullptr.md) | [Video Explanation]() | [Practice Code](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-0.cpp) | | |
| 8 | + |
| 9 | +**Why was it introduced?** |
| 10 | + |
| 11 | +- Resolve ambiguity issues with `NULL` macro and integer `0` in overload resolution |
| 12 | +- Provide type-safe null pointer representation |
| 13 | +- Clearly distinguish between pointer and integer types |
| 14 | +- Support type deduction in template programming |
| 15 | + |
| 16 | +**What's the difference between nullptr and NULL?** |
| 17 | + |
| 18 | +- `nullptr` is a keyword introduced in C++11, with type `std::nullptr_t` |
| 19 | +- `NULL` is a preprocessor macro, typically defined as integer `0` or `(void*)0` |
| 20 | +- `nullptr` is more precise in overload resolution and won't be confused with integer types |
| 21 | + |
| 22 | +## I. Basic Usage and Scenarios |
| 23 | + |
| 24 | +### Replacing NULL and 0 |
| 25 | + |
| 26 | +> Used for pointer variable initialization and assignment, replacing traditional `NULL` and `0` |
| 27 | +
|
| 28 | +```cpp |
| 29 | +int* ptr1 = nullptr; // Recommended usage |
| 30 | +int* ptr2 = NULL; // Traditional usage |
| 31 | +int* ptr3 = 0; // Not recommended |
| 32 | + |
| 33 | +// Check if pointer is null |
| 34 | +if (ptr1 == nullptr) { |
| 35 | + // Handle null pointer case |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Resolving Overload Ambiguity |
| 40 | + |
| 41 | +> Explicitly passing null pointers in function calls, `nullptr` can avoid overload ambiguity issues and prevent confusion with integer types |
| 42 | +
|
| 43 | +```cpp |
| 44 | +void func(int* ptr) { |
| 45 | + if (ptr != nullptr) { |
| 46 | + *ptr = 42; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void func(int value) { |
| 51 | + // Handle integer parameter |
| 52 | +} |
| 53 | + |
| 54 | +int main() { |
| 55 | + func(nullptr); // Explicitly call pointer version |
| 56 | + func(0); // May call integer version, causing ambiguity |
| 57 | + func(NULL); // May call integer version, causing ambiguity |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +For example, in the code above, calling `func(NULL)` will report an overload ambiguity error |
| 62 | +
|
| 63 | +```bash |
| 64 | +main.cpp: In function 'int main()': |
| 65 | +main.cpp:16:9: error: call of overloaded 'func(NULL)' is ambiguous |
| 66 | + 16 | func(NULL); // May call integer version, causing ambiguity |
| 67 | + | ~~~~^~~~~~ |
| 68 | +``` |
| 69 | + |
| 70 | +### Ensuring Type Safety in Template Programming |
| 71 | + |
| 72 | +> In template functions and classes, `nullptr` provides better type deduction and safety |
| 73 | +
|
| 74 | +```cpp |
| 75 | +// https://en.cppreference.com/w/cpp/language/nullptr.html |
| 76 | + |
| 77 | +template<class T> |
| 78 | +constexpr T clone(const T& t) { |
| 79 | + return t; |
| 80 | +} |
| 81 | + |
| 82 | +void g(int*) { |
| 83 | + std::cout << "Function g called\n"; |
| 84 | +} |
| 85 | + |
| 86 | +int main() { |
| 87 | + g(nullptr); // ok |
| 88 | + g(NULL); // ok |
| 89 | + g(0); // ok |
| 90 | + |
| 91 | + g(clone(nullptr)); // ok |
| 92 | + g(clone(NULL)); // ERROR: NULL might be deduced to non-"pointer" type |
| 93 | + g(clone(0)); // ERROR: 0 will be deduced to non-"pointer" type |
| 94 | +} |
| 95 | +``` |
| 96 | +
|
| 97 | +When using function templates, `NULL` and `0` are usually deduced to non-"pointer" types, while `nullptr` can avoid this problem |
| 98 | +
|
| 99 | +```bash |
| 100 | +main.cpp:19:12: error: invalid conversion from 'int' to 'int*' [-fpermissive] |
| 101 | + 19 | g(clone(0)); // ERROR: 0 will be deduced to non-"pointer" type |
| 102 | + | ~~~~~^~~ |
| 103 | + | | |
| 104 | + | int |
| 105 | +``` |
| 106 | + |
| 107 | +### Smart Pointers and Containers |
| 108 | + |
| 109 | +> Used with modern C++ features (such as smart pointers, STL containers) |
| 110 | +
|
| 111 | +```cpp |
| 112 | +#include <memory> |
| 113 | +#include <vector> |
| 114 | + |
| 115 | +int main() { |
| 116 | + std::shared_ptr<int> sp1 = nullptr; |
| 117 | + std::unique_ptr<int> up1 = nullptr; |
| 118 | + |
| 119 | + std::vector<int*> vec; |
| 120 | + vec.push_back(nullptr); |
| 121 | + |
| 122 | + // Check if smart pointer is null |
| 123 | + if (sp1 == nullptr) { |
| 124 | + sp1 = std::make_shared<int>(42); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## II. Important Notes |
| 130 | + |
| 131 | +### Type Deduction and std::nullptr_t |
| 132 | + |
| 133 | +The type of `nullptr` is `std::nullptr_t`, which is a special type that can be **implicitly** converted to any pointer type: |
| 134 | + |
| 135 | +```cpp |
| 136 | +#include <cstddef> // Contains definition of std::nullptr_t |
| 137 | + |
| 138 | +void func(int*) {} |
| 139 | +void func(double*) {} |
| 140 | +void func(std::nullptr_t) {} |
| 141 | + |
| 142 | +int main() { |
| 143 | + auto ptr = nullptr; // ptr's type is std::nullptr_t |
| 144 | + |
| 145 | + func(nullptr); // Call std::nullptr_t version |
| 146 | + func(ptr); // Call std::nullptr_t version |
| 147 | + |
| 148 | + int* intPtr = nullptr; |
| 149 | + func(intPtr); // Call int* version |
| 150 | +} |
| 151 | +``` |
| 152 | +
|
| 153 | +### Implicit Conversion to Boolean Type |
| 154 | +
|
| 155 | +`nullptr` can be implicitly converted to `bool` type, which is very convenient in conditional checks: |
| 156 | +
|
| 157 | +```cpp |
| 158 | +int* ptr = nullptr; |
| 159 | +
|
| 160 | +if (ptr) { // Equivalent to if (ptr != nullptr) |
| 161 | + // Pointer is not null |
| 162 | +} else { |
| 163 | + // Pointer is null |
| 164 | +} |
| 165 | +
|
| 166 | +bool isEmpty = (ptr == nullptr); // true |
| 167 | +``` |
| 168 | + |
| 169 | +## III. Practice Code |
| 170 | + |
| 171 | +Practice Code Topics |
| 172 | + |
| 173 | +- 0 - [nullptr Basic Usage](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-0.cpp) |
| 174 | +- 1 - [nullptr Function Overloading](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-1.cpp) |
| 175 | +- 2 - [nullptr Advantages in Template Programming](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-2.cpp) |
| 176 | + |
| 177 | +Auto-Checker Command |
| 178 | + |
| 179 | +```bash |
| 180 | +d2x checker nullptr |
| 181 | +``` |
| 182 | + |
| 183 | +## IV. Additional Resources |
| 184 | + |
| 185 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 186 | +- [mcpp-standard Tutorial Repository](https://github.com/Sunrisepeak/mcpp-standard) |
| 187 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 188 | +- [Tutorial Support Tool - xlings](https://xlings.d2learn.org) |
0 commit comments