Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling C++ code without macros

Tags:

c++

macros

aop

I hate macros. I'm trying to avoid using them as much as I can, but I occasionally need them to enable / disable features in my code. Typically:

#ifdef THREAD_SAFE
  typedef boost::mutex Mutex;
  typedef boost::mutex::scoped_lock ScopedLock;
#else
  typedef struct M {            } Mutex;
  typedef struct S { S(M m) { } } ScopedLock;
#endif

This way I can leave my actual code unchanged. I'm trusting the compiler to remove the placebo code when the macro is undefined.

I'm aware that template specialization could be a solution, but that would involve a lot of rewriting / code duplicating.

No need to be a C++ expert to guess there's something wrong with the way I'm cheating on the compiler. I'm looking for a better solution.

like image 331
Warren Seine Avatar asked Apr 20 '26 22:04

Warren Seine


2 Answers

What you are using aren't macros, but normal preprocessor capabilities. Also, you're not relying on the compiler, but the preprocessor.
The compiler will only ever see one of the two versions, the other gets eliminated before the compilation step. Nothing wrong with using the preprocessor to do (conditional) inclusion/exclusion of code. It isn't any kind of "cheating", that's totally what the preprocessor is there for.

like image 159
Xeo Avatar answered Apr 23 '26 10:04

Xeo


Macros are the only good way to get information from the build system into the program. The other alternative is writing your own code-generation scripts, or tools like SWIG.

The problem I see here is the unnecessary use of typedef. I think this is better because it limits the introduction of new symbols (single-letter ones!), and keeps code looking more canonical.

#ifdef THREAD_SAFE
  using boost::mutex;
#else
  struct mutex {
      struct scoped_lock {
          scoped_lock(mutex const &m) { }
      };
  };
#endif
like image 38
Potatoswatter Avatar answered Apr 23 '26 11:04

Potatoswatter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!