Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • 1
    Would the implementation-defined result differ if i run it on a different compiler or on the same one again? Does it mean the code would be non-portable? Commented Oct 20, 2011 at 10:16
  • Implementation-defined means that the implementation (the compiler, basically) can choose how to behave, but it must document the behavior. So typically, that means that a single compiler will do the same thing consistently if you recompile, or if you run the program again. But if you compile using a different compiler, or targeting a difference CPU, it might do something else (which, again, has to be documented). So yes, if you rely on the intermediate value, your code is non-portable. If you want to be portable, you have to do the full round-trip Commented Oct 20, 2011 at 11:36
  • I see some conversions you showed are guaranteed, does it depend on the type of objects we are interpreting e.g float* to int* or is it because reinterpret_cast works that way. how about some abstract types? Commented Oct 22, 2011 at 7:29
  • 1
    @user974191 That's what I tried to say above the example. reinterpret_cast from A to B yields an implementation-defined result, but A to B to A is guaranteed to give the same value as you started with. Commented Oct 22, 2011 at 7:50
  • 4
    @jalf Great answer, and best I found in an hour's googling. Have a nitpick o share: Stroustrup mentions in "The C++ Programming Language" that the size of A needs to fit into the size of B for the guaranteed A to B to A conversion sequence to work. Casting a pointer to a non-pointer char and back will obviously lead to tears. Less obvious is that the storage size of pointers on some obscure hardware platforms can vary depending on the type pointed to, with the only guarantee being a void* needs to be able to fit any of them. Commented Nov 5, 2018 at 20:48

lang-cpp