4

I'm having trouble using the C99 complex data type with FFTW and C++14.

// fftw14.cc
#include <complex.h>
#include <fftw3.h>

int main() {
    fftw_complex* in;

    in  = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 100);

    in[0] = 1337.42;

    return 0;
}

There is no problem when I compile it with g++ fftw14.cc -lfftw3 -lm. However, using C++14 (g++ -std=c++14 fftw14.cc -lfftw3 -lm), fftw_complex gets double[2] and hence the implicit typecast fails.

fftw14.cc: In function ‘int main()’:
fftw14.cc:11:8: error: incompatible types in assignment of ‘double’ to ‘fftw_complex {aka double [2]}’
  in[0] = 1337.42;
        ^

Why is that? I know I could mess around with the std::complex<double> type, but I would like to avoid that.

murphy
  • 179
  • 1
  • 3
  • 1
    `fftw_complex` changes its definition depending on a few things read http://www.fftw.org/fftw3_doc/Complex-numbers.html carefully. I think this bit _"if you #include before , then fftw_complex is defined to be the native complex"_ is confusing as it only applies to the `C` compiler, and does not state what happens with `C++`. – Richard Critten Feb 16 '17 at 14:07
  • You should be using `static_cast` or `reinterpret_cast` to more clearly express your intent. C-style casts are frowned upon usually. – tambre Feb 16 '17 at 14:32
  • What bugs me, is that it works with C++98 but not with C++14. I want to understand what could cause this. – murphy Feb 16 '17 at 19:51

0 Answers0