Can boolean operators be used with the preprocessor?

Wesley picture Wesley · Aug 2, 2010 · Viewed 45.8k times · Source

I wondering if it possible to have a preprocessor OR or AND statement? I have this code where I want to run under _DEBUG or _UNIT_TEST tags(?).

What I want is something like the following:

#if _DEBUG || _UNIT_TEST
  //Code here
#endif

If this is not possible, is there a workaround to achieve the same thing without having to duplicate the code using a #elseif?

Answer

Kirill V. Lyadvinsky picture Kirill V. Lyadvinsky · Aug 2, 2010
#if defined _DEBUG || defined _UNIT_TEST 
  //Code here 
#endif 

You could use AND and NOT operators as well. For instance:

#if !defined _DEBUG && defined _UNIT_TEST 
  //Code here 
#endif