为什么 float 和 int 的 std::pow 会调用此重载?
Why does std::pow of a float and int invoke this overload?
以下:
#include <cmath>
int main()
{
float base = 2.0f;
float result = std::pow(base, 2);
return 0;
}
在打开时触发 -Wconversion
警告。 Wandbox
似乎调用了 std::pow
的 double
重载,我希望选择 float
重载(将 int
指数转换为float
)。知道他超载的人能解释一下为什么吗?
来自 cpp reference
7) A set of overloads or a function template for all combinations of
arguments of arithmetic type not covered by 1-3). If any argument has
integral type, it is cast to double. If any argument is long double,
then the return type Promoted is also long double, otherwise the
return type is always double.
"If any argument has integral type, it is cast to double"
然后它会调用,将你的双精度转换为浮点数。
float pow( float base, float exp );
自 C++11 起,混合参数 pow
将任何整型参数转换为双精度。混合参数函数的 return 类型总是 double
除非一个参数是 long double
那么结果是 long double
.
[c.math]
In addition to the double versions of the math functions in , C++ adds float and long double overloaded versions of these functions, with the same semantics.
Moreover, there shall be additional overloads sufficient to ensure:
If any argument corresponding to a double parameter has type long double, then all arguments corresponding to double parameters are effectively cast to long double.
Otherwise, if any argument corresponding to a double parameter has type double or an integer type, then all arguments corresponding to double parameters are effectively cast to double.
Otherwise, all arguments corresponding to double parameters are effectively cast to float.
所以总结一下:
- 一个参数
long double
>> long double
- 两个参数
float
>> float
- 否则>>
double
以下:
#include <cmath>
int main()
{
float base = 2.0f;
float result = std::pow(base, 2);
return 0;
}
在打开时触发 -Wconversion
警告。 Wandbox
似乎调用了 std::pow
的 double
重载,我希望选择 float
重载(将 int
指数转换为float
)。知道他超载的人能解释一下为什么吗?
来自 cpp reference
7) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has integral type, it is cast to double. If any argument is long double, then the return type Promoted is also long double, otherwise the return type is always double.
"If any argument has integral type, it is cast to double" 然后它会调用,将你的双精度转换为浮点数。
float pow( float base, float exp );
自 C++11 起,混合参数 pow
将任何整型参数转换为双精度。混合参数函数的 return 类型总是 double
除非一个参数是 long double
那么结果是 long double
.
[c.math]
In addition to the double versions of the math functions in , C++ adds float and long double overloaded versions of these functions, with the same semantics.
Moreover, there shall be additional overloads sufficient to ensure:
If any argument corresponding to a double parameter has type long double, then all arguments corresponding to double parameters are effectively cast to long double.
Otherwise, if any argument corresponding to a double parameter has type double or an integer type, then all arguments corresponding to double parameters are effectively cast to double.
Otherwise, all arguments corresponding to double parameters are effectively cast to float.
所以总结一下:
- 一个参数
long double
>>long double
- 两个参数
float
>>float
- 否则>>
double