Navid Mahmoudian Navid Mahmoudian - 18 days ago 5
C++ Question

c++ macro output type based on the macro function argument

I am trying to define my variable type based on a boolean variable comes into my macro function, but I get compiler error.

#define myImageType(depthImage) ((depthImage)==0 ? int : double)

int main()
{
bool inMilimeter = true;
myImageType(inMilimeter) pixelIntensity;
}

Answer

You will need to use std::conditional.

Since the type must be decided at compile-time, the boolean must also be a constant expression:

#include <type_traits>

#define myImageType(depthImage) \
std::conditional<depthImage, int, double>::type

int main() {
    constexpr bool inMilimeter = true;
    myImageType(inMilimeter) pixelIntensity;
}