Converting an integer to a signed type when the source value can't be represented in the destination type is according to cppreference
- implementation-defined (until C++20)
- the unique value of the destination type equal to the source value modulo 2^n where n is the number of bits used to represent the destination type (since C++20)
Also specified in GCC implementation-defined behavior, there is
For conversion to a type of width N, the value is reduced modulo 2^N to be within range of the type; no signal is raised.
I guess there are saying the same thing. My question is isn't the reduced/moduloed result still maybe out of range of the destination signed type? Say signed char c = 255, 255 modulo 2^8 is still 255, unchanged. How does this moduloed result fit into the destination type?
This answer shows a method to first invert the value and add 1, then prepend a signed bit. I'm not sure if that's what actually have been done.
What's the correct/standard way to interpret the emphasized part?
Neither of these quotes are meant to say that the original value is taken, the modulo operation applied, and the result used as result of the conversion.
Instead they are meant to say that out of all values
vrepresentable in the destination type, the (unique) one for which the mathematical equalityholds for some integer
m, withsthe source value, is chosen. It is said thatsandvare congruent modulo2^nif they satisfy this condition or sometimes also that they are equal modulo2^n.For
s = 255with signed target of width8,255is not representable, but-1is andv = -1satisfies the equation withm = -1.