jeudi 25 mai 2017

C/C++ interop between _Complex and std::complex

I have some C DSP libraries which use complex [T] types for complex numbers. I want to call these from a C++ application which uses std::complex<T>.

After reading this SO answer and this one, and §26.4 of N4296, I tried an experiment:

extern "C" {
#include <complex.h>
// Using _Complex or __complex__ since C's "complex" type doesn't exist in C++
void cfunc(_Complex float x);
}
#include <complex>

void test()
{
        std::complex<float> z;
        cfunc(reinterpret_cast<float[2]>(z));
}

And tried to compile it with CXXFLAGS="-std=c++11". I got the following from GCC 6.3.1:

error: invalid cast from type ‘std::complex<float>’ to type ‘float [2]’
  cfunc(reinterpret_cast<float[2]>(z));

Is this a compiler bug or am I misunderstanding something? How would one use C functions which take complex [T] arguments, given C++ code using std::complex<T> types? Currently, I use a dirty hack to work around this issue but I'd prefer a clean way.

I tried compiling with -std=c++14 just in case this feature had missed C++11 somehow (despite posts quoting it from the C++11 standard) but I get the same result.

Aucun commentaire:

Enregistrer un commentaire