standards - C++ Converting unsigned to signed integer portability -


i know in c conversion of unsigned signed integers implementation defined, c++? figured have asked already, , searched couldn't find it.

i have function operates on unsigned integer , returns related unsigned integer. passing function signed integer casting unsigned similar int num = -6; unsigned ret = func((unsigned)num); int ret_as_signed = (int)ret;. in visual studio works fine, wonder how portable is.

is there portable way convert unsigned integers signed integers? it possible reverse how signed integers converted unsigned via wraparound? thanks

conversion of unsigned integer signed integer unsigned value outside of range of signed type implementation-defined. cannot count on being able round-trip negative integer unsigned , signed. [1]

c++ standard, [conv.integral], § 4.7/3:

if destination type signed, value unchanged if can represented in destination type (and bit-field width); otherwise, value implementation-defined.

[1] seems work, there no guarantees.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -