N3797 26.4 [complex.numbers] says this about casting std::complex<T>* to T*
4 Moreover, if a is an expression of type
cv std::complex<T>*and the expressiona[i]is well-defined for an integer expressioni, then:
-reinterpret_cast<cv T*>(a)[2*i]shall designate the real part ofa[i], and
-reinterpret_cast<cv T*>(a)[2*i + 1]shall designate the imaginary part ofa[i].
Does this (or some other wording of the standard) imply that I can reinterpret_cast the other way? Can I do this:
float * pf;
std::complex<float>* pc = reinterpret_cast<std::complex<float>*>(pf);
pc[i].real();
As n.m. pointed out below, I would have to make sure the alignment of pf is suitable for a std::complex<float>. This can be assumed to be taken care of.
No, that clause makes no such guarantee.
Now, in practice, the most common issue would be alignment: but even that may be rare.
The second problem would involve strict aliasing, where memory allocated as a double can be assumed by the compiler to not be modified by any operation involving pointers to other types (except char). The above places a restricition going the other way (a complex allocated pointer may not assume thay double*s do not point to its data), but not in the direction you want. Again this is relatively obscure, but a compiler could use this to reorder writes in your code.
It will, however, usually work. More often if you align it, and your compiler does not use strict aliasing assumptions: even then it is undefined behaviour by the standard, however.
It doesn't work the other way round. A std::complex<float> is two consecutive float's in memory as proven by what the standard does allow you to do, but you have a pointer to a single float value and turn it into a pointer to a structure which should contain two floats. Even if you would have two floats, the standard doesn't guarantee it and it would therefore be illegal to reinterpret_cast to pointers in that direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With