-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_ptr_impl.cpp
49 lines (37 loc) · 1.22 KB
/
null_ptr_impl.cpp
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
// A simple implementation of a nullptr-like pointer type
#include <iostream>
#include <memory>
struct null_pointer {
template <typename T>
operator T*() {
std::cout << "Converting to raw pointer" << std::endl;
return (T*)(0);
}
template <typename T>
operator std::shared_ptr<T>() {
std::cout << "Converting to std::shared_ptr<>" << std::endl;
return std::shared_ptr<T>{};
}
template <typename T>
operator std::unique_ptr<T>() {
std::cout << "Converting to std::unique_ptr<>" << std::endl;
return std::unique_ptr<T>{}; // invokes the move constructor
}
};
void f(void*) { std::cout << "void f(void*)" << std::endl; }
void f(int) { std::cout << "void f(int)" << std::endl; }
void g(int) { std::cout << "void g(int)" << std::endl; }
int* h() { return null_pointer{}; }
int main() {
null_pointer np;
// Calls, distinguish int vs null_pointer
f(np); // calls f(void*), via implicit conversion to raw pointer
f(0); // calls f(int)
// g(np); // does not compile
// Explicit conversions to raw pointers
(void*)np;
h();
// Conversions to smart pointers
std::shared_ptr<int> spint = np;
std::unique_ptr<int> upint = np;
}