Skip to content
amirroth edited this page Sep 22, 2021 · 2 revisions

This is going to be a short one. With the introduction of constexpr "variables" and more recently constexpr functions (see this page for how to use these), there is no more need to use the C-preprocessor to define compile-time constants or helper functions that inline arguments if those are compile-time constants. In fact, there are only two legitimate remaining reasons to use the C preprocessor:

  • Conditional compilation, i.e., you want to #ifdef some code based on definitions that are passed to the compiler via -D flags. Here is an example.
#ifdef EP_nocache_Psychrometrics
#undef EP_cache_PsyTwbFnTdbWPb
#undef EP_cache_PsyPsatFnTemp
#undef EP_cache_PsyTsatFnPb
#undef EP_cache_PsyTsatFnHPb
#else // !EP_nocache_Psychrometrics
#define EP_cache_PsyTwbFnTdbWPb
#define EP_cache_PsyPsatFnTemp
#define EP_cache_PsyTsatFnPb
#define EP_cache_PsyTsatFnHPb
#endif // EP_nocache_Psychrometrics
  • Porting compiler-specific features like #pragmas.
#if defined(_MSC_VER)
#define DISABLE_WARNING_PUSH __pragma(warning(push))
#define DISABLE_WARNING_POP __pragma(warning(pop))
#define DISABLE_WARNING(warningNumber) __pragma(warning(disable : warningNumber))
#define DISABLE_WARNING_STRICT_ALIASING // purposfully doing nothing here - does MSVC not have a strict-aliasing warning?
#elif defined(__GNUC__) || defined(__clang__) // !defined(_MSC_VER)
#define DO_PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)
#define DISABLE_WARNING_STRICT_ALIASING DISABLE_WARNING(-Wstrict-aliasing)
#endif // defined(_MSC_VER)
  • That's about it.
Clone this wiki locally