-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-1-nullptr.cpp
More file actions
73 lines (56 loc) · 1.62 KB
/
Copy path02-1-nullptr.cpp
File metadata and controls
73 lines (56 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <type_traits>
using namespace std;
/*
Historically, C++ allowed:
int* p = 0;
But 0 itself is still an integer. That's the problem.
NULL is not a C++ keyword.
It is a macro provided by the implementation/library.
Historically, it might be something like: #define NULL 0
or in C: define NULL ((void*)0)
C++11 introduced: nullptr is specifically designed to represent a null pointer.
And unlike 0, it is not an integer.
decltype(expression) asks: What is the type of this expression?
*/
void foo(int) {
cout << "int" << endl;
}
void foo(char*) {
cout << "pointer" << endl;
}
/*
0
│
└── integer
├── foo(int) ✅
└── foo(char*) conversion
nullptr
│
└── nullptr_t
└── foo(char*) ✅ pointer conversion
*/
/*
What does ((void*)0) actually mean?
- There are two things happening.
- Take the integer 0 and explicitly cast it to void*.
- The resulting pointer is a null pointer value.
- void* means: A pointer to an object of an unspecified type.
*/
double x = 3.4;
void* ptr = &x;
int main() {
system("clear");
char *ch1 = NULL;
char *ch2 = nullptr;
cout << NULL << " " << (size_t)(nullptr) << endl;
cout << std::is_same<decltype(NULL), decltype(0)>::value << endl;
cout << std::is_same<decltype(NULL), decltype(nullptr)>::value << endl;
int a = 20;
decltype(a) b = 22;
/*
Can we print decltype? Not directly.
It's like asking: "cout << int" You can't print a type directly.
*/
return 0;
}