-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathpreprocess.cpp
134 lines (104 loc) · 2.35 KB
/
preprocess.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
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
// Copyright (c) 2020 by Chrono
//
// pre-process source:
// g++ preprocess.cpp -E -o a.cxx
//
// g++ preprocess.cpp -std=c++98 -o a.out;./a.out
// g++ preprocess.cpp -std=c++11 -o a.out;./a.out
// g++ preprocess.cpp -std=c++14 -o a.out;./a.out
//
// g++ preprocess.cpp -DNDEBUG -std=c++11 -o a.out;./a.out
//
// show all predefined macro:
// g++ -E -dM - < /dev/null
// g++ -E -dM - < /dev/null > predef.cxx
// comment it for better pre-process output
#include <iostream>
using namespace std;
// you can try this
//#include "a.out"
void case1()
{
#
#if __linux__
# define HAS_LINUX 1
#else
# define HAS_LINUX 0
#endif
#
cout << "linux is " << HAS_LINUX << endl;
}
void case2()
{
#define CUBE(a) (a) * (a) * (a)
cout << CUBE(10) << endl;
cout << CUBE(15) << endl;
#undef CUBE
#ifdef AUTH_PWD
# undef AUTH_PWD
#endif
#define AUTH_PWD "xxx"
cout << AUTH_PWD << endl;
#define MAX_BUF_LEN 65535
#define VERSION "1.0.18"
}
// macro for convienient namespace
#define BEGIN_NAMESPACE(x) namespace x {
#define END_NAMESPACE(x) }
BEGIN_NAMESPACE(my_own)
class MyClass final
{
};
void case3()
{
cout << "working in own namespace" << endl;
}
END_NAMESPACE(my_own)
#ifdef __cplusplus
extern "C" {
#endif
void a_c_function(int a);
#ifdef __cplusplus
}
#endif
void case4()
{
#if __cplusplus >= 201402
cout << "c++14 or later" << endl;
#elif __cplusplus >= 201103
cout << "c++11 or before" << endl;
#else // __cplusplus < 201103
# error "c++ is too old"
#endif // __cplusplus >= 201402
#if __GNUC__ <= 4
cout << "gcc is too old" << endl;
#else // __GNUC__ > 4
cout << "gcc is good enough" << endl;
#endif // __GNUC__ <= 4
#if defined(__SSE4_2__) && defined(__x86_64)
cout << "we can do more optimization" << endl;
#endif // defined(__SSE4_2__) && defined(__x86_64)
#if __linux__
cout << "running on linux" << endl;
#else // not linux
cout << "running on others" << endl;
#endif //__linux__
#if defined(__cpp_decltype_auto)
cout << "decltype(auto) enable" << endl;
#else
cout << "decltype(auto) disable" << endl;
#endif //__cpp_decltype_auto
#ifndef NDEBUG
cout << "debug mode" << endl;
#endif // NDEBUG
}
int main()
{
case1();
case2();
my_own::case3();
[[gnu::unused]] // ignore warning
my_own::MyClass obj;
case4();
cout << "pre-process demo" << endl;
}