At first I was a bit confused about what std::copysign
is actually good for, but the note on cppreference gives a reasonable motivation:
std::copysign
is the only portable way to manipulate the sign of a NaN
value (to examine the sign of a NaN
, signbit
may also be used)
Take this plus the fact that integers cannot be NaN
and it makes sense that there is no std::copysign
for int
. On the other hand, a std::copysign<int,int>
returning an int
would come in handy in generic code, where you want it to work for double
as well as for int
. For this I would perhaps write my own wrapper, starting from a
template <typename T>
T my_copy_sign(const T& t, const T& s);
and then specialize it accordingly.
PS: If you are only working on int
, you probably dont need std::copysign
in the first place.