On 26/11/2018 15:50, Emil Dotchevski wrote:
auto xu = std::make_unsigned_t
(x); if (xu < f()) This expresses your intent of using the unsigned version of the parameter.
You probably mean
auto xu = std::make_unsigned
::type(x);
No, I meant what I wrote.
This is similar to the implicit conversion, except it doesn't eliminate the other implicit conversion which will occur if the return type of f is of size greater than the size of x, which will likely earn you another warning.
That was intentional, because that's another decision that has to be made explicitly, with evaluation of the range constraints on f()'s return value.
Therefore you probably want
auto xu = std::make_unsigned
::type(x);
No, that's pointless. f() is already known to return an unsigned type or you wouldn't have received the original warning in the first place. You could use decltype(f()) by itself, but that doesn't really help unless you know that it is always wider then x anyway.
Fine. Do you think programmers actually write this to silence such warnings, rather than cast? Are you arguing that they should?
I don't think many currently do this, no. I wonder if they should, given your complaint, but it's probably more generally useful to encourage use of safe_numerics or similar instead.
Ironically, in my experience the warning police doesn't care about the assert, even though it will actually catch the bugs they're trying to defend against, while the warning, silenced with a cast, won't.
True. The assert is more explicit and more useful (if actually checked), but people seem unreasonably afraid of adding them for some reason.