|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// ============================================================================ |
| 6 | +// (1) Object-like macros: #define identifier replacement-list |
| 7 | +// ============================================================================ |
| 8 | + |
| 9 | +#define PI 3.14159 |
| 10 | +#define MAX_SIZE 100 |
| 11 | +#define VERSION "1.0.0" |
| 12 | +#define EMPTY // replacement-list is optional (empty) |
| 13 | + |
| 14 | +void example1_object_like() { |
| 15 | + cout << "\n=== (1) Object-like macros ===\n"; |
| 16 | + cout << "PI = " << PI << "\n"; |
| 17 | + cout << "MAX_SIZE = " << MAX_SIZE << "\n"; |
| 18 | + cout << "VERSION = " << VERSION << "\n"; |
| 19 | + // EMPTY expands to nothing |
| 20 | +} |
| 21 | + |
| 22 | +// ============================================================================ |
| 23 | +// (2) Function-like macros: #define identifier(parameters) replacement-list |
| 24 | +// ============================================================================ |
| 25 | + |
| 26 | +#define SQUARE(x) ((x) * (x)) |
| 27 | +#define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 28 | +#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 29 | + |
| 30 | +void example2_function_like() { |
| 31 | + cout << "\n=== (2) Function-like macros ===\n"; |
| 32 | + cout << "SQUARE(5) = " << SQUARE(5) << "\n"; // 25 |
| 33 | + cout << "MAX(10, 20) = " << MAX(10, 20) << "\n"; // 20 |
| 34 | + cout << "MIN(10, 20) = " << MIN(10, 20) << "\n"; // 10 |
| 35 | +} |
| 36 | + |
| 37 | +// ============================================================================ |
| 38 | +// (3) Variadic macros: #define identifier(parameters, ...) replacement-list |
| 39 | +// C++11 - variable number of arguments |
| 40 | +// ============================================================================ |
| 41 | + |
| 42 | +#define PRINT_NUMS(first, ...) \ |
| 43 | + cout << "First: " << first << ", Rest: " << #__VA_ARGS__ << "\n" |
| 44 | + |
| 45 | +#define LOG(level, msg) \ |
| 46 | + cout << "[" << level << "] " << msg << "\n" |
| 47 | + |
| 48 | +void example3_variadic_with_params() { |
| 49 | + cout << "\n=== (3) Variadic macros (parameters, ...) ===\n"; |
| 50 | + PRINT_NUMS(1, 2, 3, 4); |
| 51 | + LOG("INFO", "Server started"); |
| 52 | + LOG("ERROR", "Connection failed"); |
| 53 | +} |
| 54 | + |
| 55 | +// ============================================================================ |
| 56 | +// (4) Variadic macros: #define identifier(...) replacement-list |
| 57 | +// C++11 - only variadic arguments, no fixed parameters |
| 58 | +// ============================================================================ |
| 59 | + |
| 60 | +#define PRINT_ALL(...) \ |
| 61 | + cout << "Arguments: " << #__VA_ARGS__ << "\n" |
| 62 | + |
| 63 | +#define DEBUG(msg) \ |
| 64 | + cout << "[DEBUG] " << msg << "\n" |
| 65 | + |
| 66 | +void example4_variadic_only() { |
| 67 | + cout << "\n=== (4) Variadic macros (...) only ===\n"; |
| 68 | + PRINT_ALL(1, 2, 3); |
| 69 | + PRINT_ALL("hello", "world"); |
| 70 | + DEBUG("Some debug message"); |
| 71 | +} |
| 72 | + |
| 73 | +// ============================================================================ |
| 74 | +// (5) #undef directive |
| 75 | +// ============================================================================ |
| 76 | + |
| 77 | +#define TEMP_VALUE 999 |
| 78 | + |
| 79 | +void example5_undef() { |
| 80 | + cout << "\n=== (5) #undef directive ===\n"; |
| 81 | + cout << "TEMP_VALUE before undef: " << TEMP_VALUE << "\n"; |
| 82 | + |
| 83 | +#undef TEMP_VALUE |
| 84 | + // TEMP_VALUE is now undefined, cannot use it anymore |
| 85 | + // cout << TEMP_VALUE; // ERROR: TEMP_VALUE not defined |
| 86 | + |
| 87 | + cout << "TEMP_VALUE has been undefined\n"; |
| 88 | +} |
| 89 | + |
| 90 | +// ============================================================================ |
| 91 | +// OPERATORS: # (stringification) and ## (token pasting) |
| 92 | +// ============================================================================ |
| 93 | + |
| 94 | +// # operator: converts parameter to string |
| 95 | +#define STRINGIFY(x) #x |
| 96 | +#define TO_STRING(x) STRINGIFY(x) |
| 97 | + |
| 98 | +// ## operator: concatenates tokens |
| 99 | +#define CONCAT(a, b) a##b |
| 100 | +#define VAR_NAME(prefix, suffix) prefix##_##suffix |
| 101 | + |
| 102 | +void example6_operators() { |
| 103 | + cout << "\n=== # operator (stringification) ===\n"; |
| 104 | + cout << STRINGIFY(hello world) << "\n"; // "hello world" |
| 105 | + cout << TO_STRING(100) << "\n"; // "100" |
| 106 | + |
| 107 | + cout << "\n=== ## operator (token pasting) ===\n"; |
| 108 | + int value_123 = 456; |
| 109 | + cout << "CONCAT(value_, 123) = " << CONCAT(value_, 123) << "\n"; // 456 |
| 110 | + |
| 111 | + int my_variable = 789; |
| 112 | + cout << "VAR_NAME(my, variable) = " << VAR_NAME(my, variable) << "\n"; // 789 |
| 113 | +} |
| 114 | + |
| 115 | +// ============================================================================ |
| 116 | +// C++20: __VA_OPT__ |
| 117 | +// ============================================================================ |
| 118 | + |
| 119 | +#if __cplusplus >= 202002L |
| 120 | +#define LOG_WITH_COMMA(msg, ...) \ |
| 121 | + cout << msg __VA_OPT__(,) __VA_ARGS__ << "\n" |
| 122 | + |
| 123 | +void example7_va_opt() { |
| 124 | + cout << "\n=== C++20: __VA_OPT__ ===\n"; |
| 125 | + LOG_WITH_COMMA("Message"); // No comma if no args |
| 126 | + LOG_WITH_COMMA("Message", " extra"); // Comma if args present |
| 127 | +} |
| 128 | +#endif |
| 129 | + |
| 130 | +// ============================================================================ |
| 131 | +// Scanning and Replacement example |
| 132 | +// ============================================================================ |
| 133 | + |
| 134 | +#define EXAMPLE_SCAN 42 |
| 135 | +#define USE_EXAMPLE EXAMPLE_SCAN |
| 136 | + |
| 137 | +void example8_scanning() { |
| 138 | + cout << "\n=== Scanning and Replacement ===\n"; |
| 139 | + cout << "USE_EXAMPLE = " << USE_EXAMPLE << "\n"; // 42 |
| 140 | + // Macro expansion: USE_EXAMPLE -> EXAMPLE_SCAN -> 42 |
| 141 | +} |
| 142 | + |
| 143 | +// ============================================================================ |
| 144 | +// MAIN |
| 145 | +// ============================================================================ |
| 146 | + |
| 147 | +int main() { |
| 148 | + cout << "=================================================\n"; |
| 149 | + cout << " #define SYNTAX EXAMPLES\n"; |
| 150 | + cout << "=================================================\n"; |
| 151 | + |
| 152 | + example1_object_like(); |
| 153 | + example2_function_like(); |
| 154 | + example3_variadic_with_params(); |
| 155 | + example4_variadic_only(); |
| 156 | + example5_undef(); |
| 157 | + example6_operators(); |
| 158 | + |
| 159 | +#if __cplusplus >= 202002L |
| 160 | + example7_va_opt(); |
| 161 | +#else |
| 162 | + cout << "\n=== C++20: __VA_OPT__ ===\n"; |
| 163 | + cout << "(Requires C++20 or later)\n"; |
| 164 | +#endif |
| 165 | + |
| 166 | + example8_scanning(); |
| 167 | + |
| 168 | + cout << "\n=================================================\n"; |
| 169 | + cout << "Summary:\n"; |
| 170 | + cout << "(1) Object-like: #define NAME value\n"; |
| 171 | + cout << "(2) Function-like: #define NAME(params) replacement\n"; |
| 172 | + cout << "(3) Variadic: #define NAME(params, ...) __VA_ARGS__\n"; |
| 173 | + cout << "(4) Variadic only: #define NAME(...) __VA_ARGS__\n"; |
| 174 | + cout << "(5) Undefine: #undef NAME\n"; |
| 175 | + cout << "Operators: # (stringify), ## (concatenate)\n"; |
| 176 | + cout << "=================================================\n"; |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
0 commit comments