3

I want to use code from a C library (my specific example: FFTW) to implement a C++ class template. The C library defines some data types and functions that do the same thing, but on different kinds of primitive numeric types, e.g. fftw_complex (pair of doubles) vs. fftwf_complex (pair of floats); fftw_execute() vs. fftwf_execute(), and so on.

I would like to implement a class template that only depends on this primitive type, e.g.

FFT<float>, FFT<double>, FFT<long double> etc.

In this example the library follows the convention that all functions and data have the same prefix that specifies the data type they work with, so I might be able to do something very ugly with macros, but I would prefer not to.

Does anyone know of an elegant way to achieve this?

EDIT

An explicit example at the request of SergeyA: I would like to define a template class FFT<typename T> such that FFT<double> specializes to something like

class FFT {
 private:
  fftw_complex* _data;
  ...
 public:
  execute() {
   fftw_execute(...); 
  }
};

and

FFT<float> specializes to something like

class FFT {
 private:
  fftwf_complex* _data;
  ...
 public:
  execute() {
   fftwf_execute(...); 
  }
};
doetoe
  • 765
  • 7
  • 16

2 Answers2

2

I'm not sure I've caught exactly the problem. Something like that using partial specialization could solve?

template<class T>
class Fwd { }

template<>
class Fwd<double> {
    void execute(std::pair<double, double> pp) {
       fftw_execute(pp);
    }
}

template<>
class Fwd<float> {
    void execute(std::pair<float, float> pp) {
        fftwf_execute(pp);
    }
}

// and so on...
skypjack
  • 49,335
  • 19
  • 95
  • 187
  • Essentially that is what I want, only that these types and functions are all over the place, so that I would essentially have to do a copy/paste of the whole class definition, essentially only replacing the prefix fftw_ with fftwf_, fftwl_ and fftwq_ for the different float widths supported. – doetoe Oct 09 '15 at 13:41
  • Ok, I guess you prefer to have a delegate that simply wraps the function. Have you looked there? http://blog.molecular-matters.com/2011/09/19/generic-type-safe-delegates-and-events-in-c/ – skypjack Oct 09 '15 at 13:43
0

Another solution could be that one:

// class
template<T, void(F*)(T*)>
class FFT {
    T* _data;
public:
    FFT(T *data): _data{data} { }

    void execute() {
        (F)(_data);
    }
}

// how to use it
fftw_complex *data = get_them_from_somewhere();
auto fft = new FFT<fftw_complex, fftw_execute>{data};
fft->execute();

Does it fit your needs? I'm not even sure it works, I'm a bit in a hurry, but you can get from it the idea to develop your right one.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • I was thinking more in that direction, it does work but I get: template class ___FFT {...}. – doetoe Oct 10 '15 at 09:01
  • Then with a typedef we could define using FFT_double = ___FFT; This is not so satisfactory if we want to use the class in a template again. – doetoe Oct 10 '15 at 09:05
  • Just now I found in this stackoverflow post http://stackoverflow.com/a/2795024/919305 how to make a templatized alias. I'll try to fit that to my needs and post the result. – doetoe Oct 10 '15 at 09:07
  • Nice, let me know if you need help and eventually accept one of the responses to close the question. – skypjack Oct 10 '15 at 09:09
  • I asked a new, related question (that would provide a solution to this): http://stackoverflow.com/q/33106550/919305 – doetoe Oct 13 '15 at 15:23
  • I guess you should accept a response if this topic is closed, for having another pending request somewhere means having another problem, otherwise you would have put it there. Does it make sense? – skypjack Oct 13 '15 at 17:21
  • I asked a new question because it really was a new question; even though an answer to it would provide one here, a solution to this one doesn't necessarily have to use templatized aliases. Both of your proposed solutions are valuable and on-topic, but they didn't solve my particular problem (I had already considered them). Since the first did answer the question as posed anyway, I will accept that one. – doetoe Oct 13 '15 at 17:32
  • Don't worry, I didn't mean that you have to accept a response of mines, absolutely!! :-) ... I was simply trying to figure out why you were opening another issue on the same problem or, if it was not the same, why that one could not be marked as closed. You did reply to me, it's fine... I misunderstood the mean of your comment, my fault. Sorry. ;-) – skypjack Oct 13 '15 at 17:39