-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain16.cpp
More file actions
67 lines (48 loc) · 1.34 KB
/
Copy pathmain16.cpp
File metadata and controls
67 lines (48 loc) · 1.34 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
// g++ -D__ENABLE_COMPILE_ASSERT__ -Wall -O3 -o main16 main16.cpp
#include "compile_assert.h"
#include <cstddef>
// demonstates a class that prevents construction with nullptr
template <typename T>
class never_null_ptr
{
public:
explicit never_null_ptr(T* ptr)
{
compile_assert(ptr != nullptr, "never_null_ptr: null pointer");
m_ptr = ptr;
}
// Never reaches here in this example
never_null_ptr(std::nullptr_t) = delete;
T* operator()() const noexcept { return m_ptr; }
private:
T* m_ptr;
};
void f(never_null_ptr<const char> ptr)
{
__builtin_printf("argv[0]: [%s]\n", ptr());
}
int main(int argc, char * const argv[])
{
if(nullptr == argv) return 1;
if(0 == argc) return 1;
compile_assert(argv != nullptr, "check main() argv not nullptr");
/*
// This approach doesn't work, because the constructor is checked before the optimizer
// realizes that a few lines above the argv pointer was already checked.
if(argv != nullptr)
{
never_null_ptr<const char> abc("hello");
}
else
{
never_null_ptr<const char> abc(nullptr);
}
*/
const char * str = argv[0];
// This line is the fix
if(str == nullptr) return 1;
// This line forces a check on the pointer by the calling code
never_null_ptr<const char> abc(str);
f(abc);
return 0;
}