Skip to content

Commit 724d079

Browse files
committed
cpp11: add nullptr
1 parent 4ddcf95 commit 724d079

19 files changed

+810
-56
lines changed

book/en/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [List Initialization](./cpp11/09-list-initialization.md)
1515
- [Delegating Constructors](./cpp11/10-delegating-constructors.md)
1616
- [Inherited Constructors](./cpp11/11-inherited-constructors.md)
17+
- [nullptr - Pointer Literal](./cpp11/12-nullptr.md)
1718

1819
# C++14 Core Language Features
1920

book/en/src/changelog.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# mcpp-standard Changelog
22

3+
## 2025/11
4+
5+
---
6+
7+
**C++11 - 12 - nullptr - Pointer Literal**
8+
9+
- `Book`: [zh](https://sunrisepeak.github.io/mcpp-standard/cpp11/12-nullptr.html) / [en](https://sunrisepeak.github.io/mcpp-standard/en/cpp11/12-nullptr.html) - 2025/11/02
10+
- `Code`: [zh](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/12-nullptr-0.cpp) / [en](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-0.cpp) - 2025/11/02
11+
312
## 2025/09
413

514
---

book/en/src/cpp11/12-nullptr.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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)

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [列表初始化](./cpp11/09-list-initialization.md)
1515
- [委托构造函数](./cpp11/10-delegating-constructors.md)
1616
- [继承构造函数](./cpp11/11-inherited-constructors.md)
17+
- [nullptr - 指针字面量](./cpp11/12-nullptr.md)
1718

1819
# C++14核心语言特性
1920

book/src/changelog.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# mcpp-standard更新日志
22

3+
## 2025/11
4+
5+
---
6+
7+
**C++11 - 12 - nullptr - 指针字面量**
8+
9+
- `Book`: [zh](https://sunrisepeak.github.io/mcpp-standard/cpp11/12-nullptr.html) / [en](https://sunrisepeak.github.io/mcpp-standard/en/cpp11/12-nullptr.html) - 2025/11/02
10+
- `Code`: [zh](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/12-nullptr-0.cpp) / [en](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/en/cpp11/12-nullptr-0.cpp) - 2025/11/02
11+
312
## 2025/09
413

514
---
6-
**C++11 - 11 - 继承造函数**
15+
**C++11 - 11 - 继承构造造函数**
716

817
- **video:** [Bili](https://www.bilibili.com/video/BV1bspBzFEEC) / [Youtube](https://youtu.be/p7vbY8XUKnY?si=GZUn9GSW68aU94A6) / [Code](https://github.com/Sunrisepeak/mcpp-standard/blob/main/videos/cpp11/11-inherited-constructors.py)
918

1019
## 2025/08
1120

1221
---
13-
**C++11 - 11 - 继承造函数**
22+
**C++11 - 11 - 继承构造造函数**
1423

1524
- **[code-0](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/11-inherited-constructors-0.cpp) / [code-1](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/11-inherited-constructors-1.cpp) / [code-2](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/11-inherited-constructors-2.cpp)** - 2025/08/29
1625
- **[book](https://sunrisepeak.github.io/mcpp-standard/cpp11/11-inherited-constructors.html)** - 2025/08/28
@@ -27,4 +36,4 @@
2736

2837
```bash
2938
d2x checker delegating-constructors
30-
```
39+
```

0 commit comments

Comments
 (0)