什么时候std :: is_convertible考虑原始类型可转换?

Sau*_*ahu 2 c++ primitive-types

使用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
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么输出必须true适用于所有上述情况,当它们看起来是可转换的(类型转换可能导致精度损失).或者我们不应该使用原始类型进行比较std::is_convertible

Fan*_*Fox 6

页面链接状态:

如果虚函数定义中的return语句{ return std::declval<From>(); }格式正确(即,如果std::declval<From>()可以使用隐式转换转换),则提供成员常量值等于true.

提到的所有类型都是隐式可转换的(尽管编译器可能会发出警告),即:

float f = 0.8f
int i = f; // Legal implicit conversion.
Run Code Online (Sandbox Code Playgroud)

完全合法且形式良好.那std::is_convertible<float, int>::value将是真的.对于列出的比较的其余部分也是如此.