lundi 5 février 2018

Parametrize function at compile time based on user expression

I'm writing a C++11 library in which the user needs to specify a struct to use and the elements to be used (in order). For example:

// Declaration
Struct MyData{ double x, y ,z;}
...
// Use
MyClass<MyData, y, x> myobj;
MyData mydata;
myobj.set(mydata);
...

Then, MyClass should generate different functions that iterate through the parameter pack, but considering the parameters as expressions. For that, I declared a class with a parameter pack:

template<class T, class ... G>
class MyClass{
    std::vector<double*> var;
    public:
    Test();
    void set(T const& var_);
 };

And then I can't figure out how to generate the set function. The behavior should be like the following pseudo-code:

 template<class T, class ... G>
 void Test<T, G...>::set(T const& var_){
 unsigned pos = 0;
 for(unsigned i =0; i < sizeof...(G); i++)
     *var[i] = var_.G(i);
}

Which, in the case of MyClass would generate:

...
*var[0] = var_.y;
*var[1] = var_.x;
...

Aucun commentaire:

Enregistrer un commentaire