Search code examples
c++castingreinterpret-cast

Is std::complex stored in an interleaved fashion?


That is, on disk, if I have an array of std::complex, is it stored RIRIRIRI or RRRRIIII or something else?

My real question is - if I have a structure that I have defined that contains two numbers, can I do a reinterpret cast an array of my structure to use functions that would expect a std::complex array?

What about memcpy? If both of my structures are floats, that should be ok if they are stored the same?


Solution

  • A C++ class is little more than a struct with some adornments. As such, the members defined in a class are arranged sequentially in memory, contiguously for each element of that type. In other words, an array of std::complex, where each element contains RI, will be stored RIRIRIRI.

    You can probably get away with a reinterpret_cast, but you will be depending on the std::complex implementation if you do so -- and that will probably be just fine.