mardi 10 août 2021

C++ compile time checking of type equality in a preprocessor directive

I'd like to check if two POD types are equal at compile time in C++11. I'm trying to #define function names that are appropriate for the Float_T type. Here is what I tried:

#include <type_traits>

using Float_T = double;
// using Float_T = float;

#if is_same<Float_T, double>::value
    #define LOG2  log2
#else
    #define LOG2  log2f
#endif

 g++ complains:  error: extra text after expected end of preprocessing directive
  #if is_same<Float_T, double>::value
                     ^

Can anyone suggest a way to do this?

Aucun commentaire:

Enregistrer un commentaire