forked from chronolaw/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.cpp
More file actions
147 lines (115 loc) · 2.38 KB
/
compile.cpp
File metadata and controls
147 lines (115 loc) · 2.38 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2020 by Chrono
//
// g++ compile.cpp -Wall -Werror -std=c++11 -o a.out;./a.out
// g++ compile.cpp -Wall -Werror -std=c++14 -o a.out;./a.out
//
// g++ compile.cpp -DNDEBUG -std=c++11 -o a.out;./a.out
//
// gcc -E -dM - < /dev/null
#include <cassert>
#include <typeinfo>
#include <string>
#include <iostream>
#include <stdexcept>
#include <type_traits>
template<int N>
struct fib
{
static_assert(N >= 0, "N must be postive");
static const int value =
fib<N - 1>::value + fib<N - 2>::value;
};
template<>
struct fib<0>
{
static const int value = 1;
};
template<>
struct fib<1>
{
static const int value = 1;
};
//[[deprecated("deadline:2020-12-31")]] // c++14 or later
[[gnu::deprecated]] // c+11 or later
int old_func()
{
//[[gnu::deprecated("I hate this")]]
int value = 0;
return value;
}
[[gnu::constructor]]
void first_func()
{
// can not use cout!
printf("before main()\n");
}
[[gnu::destructor]]
void last_func()
{
// can not use cout!
printf("after main()\n");
}
[[gnu::always_inline]] inline
int get_num()
{
return 42;
}
[[noreturn]]
int case1(bool flag)
{
throw std::runtime_error("XXX");
}
void case2()
{
using namespace std;
[[gnu::unused]]
int nouse;
cout << "case2" << endl;
}
[[gnu::hot]]
void case3()
{
using namespace std;
cout << "case3" << endl;
}
void case4()
{
static_assert(sizeof(int) == 4, "int must be 32bit");
static_assert(sizeof(long) >= 8, "must run on x64");
}
template<typename T>
void check_type(T v)
{
using namespace std;
static_assert(is_integral<T>::value, "int");
//static_assert(is_pointer<T>::value, "ptr");
//static_assert(is_default_constructible<T>::value, "is_default_constructible");
cout << "static_assert : " << typeid(v).name() << endl;
cout << is_void<void>::value << endl;
}
void case5()
{
int i = 10;
int *p = &i;
assert(i > 0 && "i must be greater than zero");
assert(p != nullptr);
std::string str = "hello";
assert(!str.empty());
}
int main()
{
using namespace std;
cout << fib<2>::value << endl;
cout << fib<3>::value << endl;
cout << fib<4>::value << endl;
cout << fib<5>::value << endl;
old_func();
get_num();
case2();
case3();
case4();
case5();
check_type(10);
//check_type((void*)0);
cout << "compile stage demo" << endl;
}