1개월 전 · Jul 18, 2026 3:01 AM
Why preprocessor #define macros are error-prone and what to replace them with:
- Problem: Macros ignore scope, pollute global namespace, cause multiple evaluation side-effects (
#define SQUARE(x) (x*x)withSQUARE(i++)), and produce cryptic compiler errors.
Modern C++ Replacements:
- Constants:
constexpr int MaxRetries = 5; - Global flags:
inline constexpr std::string_view Version = "2.4.0"; - Utility functions:
template<typename T> constexpr T Square(T x) { return x * x; } - Compile-time conditions:
if constexpr (sizeof(T) == 8)instead of#ifdef _WIN64.