According here in C ++ 11:
For any object z type complex<T> , reinterpret_cast<T(&)[2]>(z)[0] is the real part of z and reinterpret_cast<T(&)[2]>(z)[1] is the imaginary part z .
For any pointer to an array element complex<T> named p and any valid array index i , reinterpret_cast<T*>(p)[2*i] is the real part of the complex number p[i] , and reinterpret_cast<T*>(p)[2*i + 1] is the imaginary part of the complex number p[i]
You cannot do this with std::complex<T> , but if you really need to reinterpret_cast on T* or T(&)[2] and use operator[] on this.
If possible, I would suggest creating an access function:
template <class T> T & get(std::complex<T> & c, int index) { if(index == 0) return c.real(); else return c.imag(); }
and then use get(c, 0) and get(c, 1) if necessary.
SirGuy
source share