I have an unsigned value that needs to pass through a function as a signed value (it is not touched by the function). When it comes out I cast it back to unsigned. I know that the result of a cast to signed is implementation defined when overflowing, but can I at least guarantee that I end up with the same value when I cast it back (like with function pointers)?
Example:
int32_t function_with_default(int32_t a_Default)
{
// Try some stuff
// ...
// Fall back to default
return a_Default;
}
void main()
{
uint32_t input = UINT32_MAX;
uint32_t output = static_cast<uint32_t>(function_with_default(static_cast<int32_t>(input));
// Is is guarenteed to be true?
input == output;
}
I do have the guarentee that the signed integer is always bigger or equal than the unsigned integer in bytes, so no data should be lost due to lack of space.