I want to declare a struct where one of my type is either float or double depending on pointer size.
#if size of pointer is 4
# define Real float
#else
# define Real double
#endif
struct mydata {
//...
Real speed;
//...
};
#undefine Real
without using macro?
You can use std::conditional
(or conditional_t
) to choose a type based on a compile-time condition:
#include <type_traits>
using Real = std::conditional_t<sizeof(void*) == 4, float, double>;
If you use C++11 but not C++14, you'd need:
using Real = std::conditional<sizeof(void*) == 4, float, double>::type;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With