I need to write to individual bytes of some integer types. Should I used reinterpret_cast, or should I use static_cast via void*?
(a)
unsigned short v16;
char* p = static_cast<char*>(static_cast<void*>(&v16));
p[1] = ... some char value
p[0] = ... some char value
or (b)
unsigned short v16;
char* p = reinterpret_cast<char*>(&v16);
p[1] = ... some char value
p[0] = ... some char value
According to static_cast and reinterpret_cast for std::aligned_storage 's answer both should be equivalent --
-- if both T1 and T2 are standard-layout types and the alignment requirements of T2 are no stricter than those of T1
I'm leaning towards reinterpret_cast as that is essentially what I'm doing, isn't it?
Are there any other things to consider, specifically looking at Visual-C++ and VC8, the version we're currently compiling on? (x86 only atm.)
In this case (converting object pointers),
reinterpret_castis identical to the two nestedstatic_castviavoid*5.2.10 Reinterpret cast [expr.reinterpret.cast]
It is better to use
reinterpret_castto signal your intent here.UPDATE: as mentioned in the comments, this was apparently added in C++11, although most C++98 compilers already supported it (see also this Q&A)