Is it possible to set pre-processor conditions in a macro?

I am in a position where this design will greatly improve the clarity and maintenance requirements of my code.

I am looking for something like this:

#define MY_MACRO(arg) #if (arg)>0 cout<<((arg)*5.0)<<endl; #else cout<<((arg)/5.0)<<endl; #endif

The idea here: A
preprocessor replaces different lines of code depending on the compilation time value of the (constant) macro argument. Of course, I know that this syntax does not work, since it is #considered as a string-ize operator instead of the standard one #if, but I think this demonstrates the function of the preliminary processor I'm trying to achieve.

I know that I could just put the standard ifoperator there, and the compiler / runtime is left to check the values. But this is unnecessary work for the application, when a argconstant value, for example, 10.8or -12.5, which needs to be evaluated only at compile time , will always be transmitted .

The performance requirements for this crunchy application require that all unnecessary execution conditions be eliminated when possible, and many constant values ​​and macros must be used (instead of variables and functions) to make this happen. The ability to continue this trend without having to combine pre-processor code with real conditions ifwill make it much cleaner - and, of course, code cleanliness is one of the biggest problems when using macros, especially at this level.

+3
source share
2 answers

, #if ( - ) . , , if. ( , ).

"" "

+7

, ++:

template <bool B> void foo_impl       (int arg) { cout << arg*5.0 << endl; }
template < >      void foo_impl<false>(int arg) { cout << arg/5.0 << endl; }
template <int I>  void foo            ( )       { foo_impl< (I>0) >(I); }  
+5

Source: https://habr.com/ru/post/1795079/


All Articles