Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c++ - C++11 complex<float>* to C float complex*

I have an array of complex floats that I'm working with in C++11:

std::complex<float> *cx;

I am trying to use an older library which takes in C style complex floats:

float complex *cx;

When I try this:

std::complex<float> *cpp = new std::complex<float>[100];
complex float *c = reinterpret_cast<complex float*>(cpp);
c_process(c); // c_process(complex float *c)    [ c library function ]

I get:

error: expected ';' before 'float'   
complex float *c = reinterpret_cast<complex float*>(cpp);
        ^

The problem is that the C library's headers contains #include <complex.h>, and I have #include <complex> in my C++11 code which uses std::complex.

class complex from <complex> and #define complex _Complex from <complex.h> seem to be clashing on the word complex.

One Solution: This solution only works if you can modify the other library's headers.

Since the powers at be closed this question (although I think it's a pretty good), here's what I did:

Since <complex.h> has a #define complex _Complex and <complex> contains a class complex, there is some conflict when including both headers.

I had to change all float complex* to float _Complex* everywhere in my code and other library's header files. This removed the clash on the word complex from the cstyle <complex.h>'s define and c++ style's class complex without changing what the code does.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This incompatibility is syntax only, both languages guarantee that their complex types have the same layout as a float[2] (or double[2] etc). So you could mediate between the two on having a macro cfloat that is defined according to the language in which it is expanded. Something along the lines of

#ifdef __cplusplus
typedef std::complex<float> cfloat;
#else
typedef float _Complex cfloat;
#endif

And then use this cfloat in all your function prototypes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...