Use any pointer to char poiner using static_cast
If, according to the strict rule of aliases, a char pointer can point to any pointer to type, then why can't I use a pointer of any type for a char pointer using static_cast?
char *ptr;
int *intPtr;
ptr = reinterpret_cast<char*>(intPtr); // ok
ptr = static_cast<char*>(intPtr); // error: invalid static_cast from type 'int*' to type 'char*'
source to share
How it works static_cast
has nothing to do with the strict anti-aliasing rule.
static_cast
won't let you choose between arbitrary pointer types, it can only be used to cast to 1 and from 2void*
(and casting to is void*
usually redundant since the conversion is already implicit 3 ).
You can do it
ptr = static_cast<char*>(static_cast<void*>(intPtr));
but there is no difference between them 4 and
ptr = reinterpret_cast<char*>(intPtr);
https://github.com/cplusplus/draft/blob/master/papers/n4140.pdf
1 [expr.static.cast] / 6
2 [expr.static.cast] / 13
3 [conv.ptr] / 2
4 [expr.reinterpret.cast] / 7
source to share