Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to cast float* to std::complex<float>*

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 expression a[i] is well-defined for an integer expression i, then:
- reinterpret_cast<cv T*>(a)[2*i] shall designate the real part of a[i], and
- reinterpret_cast<cv T*>(a)[2*i + 1] shall designate the imaginary part of a[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.

like image 276
SirGuy Avatar asked Apr 21 '14 13:04

SirGuy


2 Answers

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.

like image 52
Yakk - Adam Nevraumont Avatar answered Nov 15 '22 01:11

Yakk - Adam Nevraumont


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.

like image 30
Daniel Frey Avatar answered Nov 15 '22 00:11

Daniel Frey