Try as I might, the closest answer I've seen is this, with two completely opposing answers(!)
The question is simple, is this legal?
auto p = reinterpret_cast<int*>(0xbadface);
*p; // legal?
My take on the matter
- Casting integer to pointer: no restrictions on what may be casted
- Indirection: only states the result is a lvalue.
- Lifetimes: only states what can't be done on objects, there is no object here
- Expression statements:
*p
is a discarded value expression - Discarded value expressions: no lvalue-to-rvalue conversion occurs
- Undefined-ness of lvalues: aka strict aliasing rule, only if the lvalue is converted to a rvalue
So I conclude there is nothing explicitly saying this is undefined behaviour. Yet I distinctively remember that some platforms trap on indirection for invalid pointers. What went wrong with my reasoning?