
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 23 September 2010, David Abrahams wrote:
On Sep 22, 2010, at 4:01 PM, Mathias Gaunard wrote:
On 22/09/2010 20:43, David Abrahams wrote:
static_cast through void*, please. That has defined behavior
AFAIK, not if you don't cast back to exactly the same type that was converted to void*.
Yes, in many cases, even if you don't. For example, if you want to access the raw bytes of an object, you can do
static_cast<char*>(static_cast<void*>(&x))
Also, if you happen to know that X is a POD struct beginning with an int, you can
*static_cast<int*>(static_cast<void*>(&x)) = 0;
What is this based on? There is a strict aliasing exception for char*, but all I see in the standard about static_casting through void is (paragraph 10 of 5.2.9): "A value of type pointer to object converted to “pointer to cv void” and back to the original pointer type will have its original value." -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkybdDgACgkQ5vihyNWuA4WVIACfSYvqL5w1Os5RyWQI0gJOH4Rf idQAoLyoBZHhwEbSegc65JY+Cx4EyPWo =UIov -----END PGP SIGNATURE-----