FFTW / fftw3

DO NOT CHECK OUT THESE FILES FROM GITHUB UNLESS YOU KNOW WHAT YOU ARE DOING. (See below.)
GNU General Public License v2.0
2.72k stars 661 forks source link

Better C++ support (std::complex) #254

Open maazl opened 3 years ago

maazl commented 3 years ago

Using fftw3 in a C++ application requires thunking layers because the array data type R C[2] is not compatible to std::complex at language level. They are guaranteed to be binary compatible, but C++ does not even allow casts between arrays and classes. So pointer conversions are required at several places. This makes the code poorly readable, error prone and may even prevent some compiler optimizations due to the required reinterpret casts.

It should be possible to force fftw3.h to use std::complex similar to the support for C99 _Complex.

maazl commented 3 years ago

I added a PR (#255) with a possible solution.

Lqlsoftware commented 3 years ago

Since FFTW is a pure C library, APIs should be wrapped by extern "C" {}. With the addition of std::complex, there should be an implementation of the whole complex interface to do the reinterpret casts... like the f77 interface did.

maazl commented 3 years ago

Hmm, yes the injection of C++ code in the header is not that pretty. A separate include for this purpose might be a better solution than a macro like FFTW_cpp_complex. However, there is no need to wrap any function. Replacing the definition of FFTW_DEFINE_COMPLEX is sufficient.