且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有,浮点提升实际发生在什么时候?

更新时间:2023-11-14 23:53:04

There is such a thing as "floating point promotion" of float to double per [conv.fpprom].

A prvalue of type float can be converted to a prvalue of type double. The value is unchanged.

This conversion is called floating point promotion.

The answers to the linked question are correct. This promotion should not occur automatically when adding two floats since the usual arithmetic conversions do not promote floating-point operands.

Floating point promotion does occur when passing a float as an operand to an ellipsis, like in printf. That's why the %f format specifier prints either a float or a double: if you pass a float, the function actually receives a double, the result of promotion.

The existence of the floating point promotion is also important in overload resolution, because integral promotions and floating point promotions have better implicit conversion rank than integral conversions, floating point conversions, and floating-integral conversions.

Example 1:

void f(double);
void f(long double);
f(0.0f);

This calls void f(double) since the promotion to double is better than the conversion to long double. In contrast, consider this perhaps surprising example 2:

void f(long double);
void f(int);
f(0.0f);

This is ambiguous. The conversion from float to long double is no better than the conversion from float to int since they are both not promotions.

Example 3:

struct S {
    operator float();
    operator int();
};
double d = S();

This calls operator float and then promotes the resulting float value to double to initialize d.

相关阅读

推荐文章