C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Integer overflow arithmetic

From: Jan Schultke <janschultke_at_[hidden]>
Date: Sat, 17 Feb 2024 11:52:35 +0100
I don't really get the following case in your code:

> if constexpr (sizeof(T) >= sizeof(U)) return false;

Why would T not be able to represent x in the event that T is larger.
Doesn't this imply that "would_cast_modify<long long>(0)" is false?

Also, if you're checking for specific values x, then this kind of test
doesn't make much sense to me. It doesn't matter which of the types is
larger; they can both represent x if x is zero.

In the case where the signedness is left unchanged by the cast, you
can probably do:

> static_cast<From>(static_cast<To>(x)) == x

This simply checks whether the roundtrip conversion preserves the value.

Received on 2024-02-17 10:52:47