Ask HN: Is there a C++2x way to do ifdef endif without pre-processor?

1 points by scrubs ↗ HN
Context:

For release builds I want all verbose and lower log calls omitted from code Ex:

LOG_VERBOSE(...) // macro

LOG_DEBUG(...); // macro

where the macros are wrapped in a #ifdef such that it is removed depending on what -D is built with?

ex. Building with -DLOG_LEVEL=6

enables LOG levels 0..6 for debug releases but -DLOG_LEVEL=1 only allows error, warnings stripping everything else out of the .o file.

2 comments

[ 3.1 ms ] story [ 17.2 ms ] thread
The "only C++" way would be std::enable_if, but it's a lot less ergonomic for a debug library (and harder to read) than a simple preprocessor macro. There's a reason logging libraries use the preprocessor macro approach here. If you really want to avoid any manual preprocessor magic, maybe consider Boost PP?
Could you rely on optimisation to strip out the code i.e. would a call to the following function be optimised out by the optimiser?

void log_debug( … ) { if ( LOG_LEVEL >= 6 ) { // logging stuff here. } }