Why is there no std::copysign specialization for integers in c++?

The*_*moo 5 c++

I tried using std::copysign to set an int with the sign of another.

int translate = getTranslate();
int offset = getOffset();
translate = std::copysign(offset, translate)
Run Code Online (Sandbox Code Playgroud)

But copysign<int, int> returns a double. How should I use it better? Or what should I use instead?

for*_*818 5

起初我对什么std::copysign是真正有用的感到有点困惑,但是cppreference 上的注释给出了合理的动机:

std::copysign是操作值符号的唯一可移植方法(也可以使用NaN来检查 a 的符号)NaNsignbit

考虑到这一点,再加上整数不可能的NaN事实,所以没有std::copysignfor是有道理的int。另一方面,std::copysign<int,int>返回 anint在通用代码中会派上用场,您希望它既适用于double又适用于int。为此,我也许会编写自己的包装器,从

template <typename T> 
T my_copy_sign(const T& t, const T& s);
Run Code Online (Sandbox Code Playgroud)

然后相应地专门化它。

PS:如果您只致力于,那么您可能一开始就int不需要。std::copysign