Using std::is_convertible:
bool i2c = std::is_convertible<int, char>::value;
bool c2i = std::is_convertible<char, int>::value;
bool f2i = std::is_convertible<float, int>::value;
std::cout << std::boolalpha;
std::cout << i2c << '\n'; //prints true
std::cout << c2i << '\n'; //prints true
std::cout << f2i << '\n'; //prints true
I couldn't get why the output had to be true
for all the above cases, when they appeared to be in-convertible (type conversions that can cause loss of precision). Or are we not supposed to compare the primitive types using std::is_convertible
?
The page linked states:
All of the types mentioned are implicitly convertible (although the compiler may issue a warning), ie:
Is perfectly legal and well formed. So
std::is_convertible<float, int>::value
will be true. The same holds for the remainder of the listed comparisons.