-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmacro.cpp
More file actions
executable file
·74 lines (63 loc) · 2.06 KB
/
macro.cpp
File metadata and controls
executable file
·74 lines (63 loc) · 2.06 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
// Demos for docs/macros.md
// Build: cmake --build build --target macro -j 4
// Run: ./build/Debug/macro
//
// CMakeLists.txt sets: target_compile_definitions(macro PRIVATE LOGING=0)
#include <iostream>
// 1) Defining constants with #define
#define PI 3.14159
// 2) Function-like macro. Parenthesize every argument and the whole body
// so operator precedence does not bite you.
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
// 3) Conditional logging macro driven by -DLOGING=0/1 from CMake.
#if LOGING == 1
#define LOG(msg) std::cout << "[LOG] " << msg << std::endl
#else
#define LOG(msg)
#endif
void constants_demo() {
std::cout << "--- constants ---\n";
std::cout << "PI = " << PI << "\n";
}
void function_like_demo() {
std::cout << "--- function-like macros ---\n";
int x = 5, y = 10;
std::cout << "SQUARE(4) = " << SQUARE(4) << "\n";
std::cout << "MAX(" << x << ", " << y << ") = " << MAX(x, y) << "\n";
}
void parenthesize_demo() {
std::cout << "--- parenthesize arguments ---\n";
// SQUARE(1 + 2) expands to ((1 + 2) * (1 + 2)) = 9.
// Without the inner parens it would be 1 + 2 * 1 + 2 = 5.
std::cout << "SQUARE(1 + 2) = " << SQUARE(1 + 2) << "\n";
}
void conditional_compile_demo() {
std::cout << "--- conditional compilation ---\n";
#ifdef __linux__
std::cout << "compiled on linux\n";
#elif defined(_WIN32)
std::cout << "compiled on windows\n";
#elif defined(__APPLE__)
std::cout << "compiled on apple\n";
#else
std::cout << "compiled on unknown platform\n";
#endif
std::cout << "LOGING = " << LOGING << "\n";
LOG("this message only prints when LOGING == 1");
}
void type_safety_pitfall_demo() {
std::cout << "--- type-safety pitfall ---\n";
// Macros do no type checking. SQUARE works for any expression, even one
// that mixes types in a way a templated/inline function would catch.
double d = 1.5;
std::cout << "SQUARE(1.5) = " << SQUARE(d) << "\n";
}
int main() {
constants_demo();
function_like_demo();
parenthesize_demo();
conditional_compile_demo();
type_safety_pitfall_demo();
return 0;
}