samedi 30 avril 2016

Variadic template class constructor with lvalues and rvalues

I'm building a machine learning library trying to get the most from the built-in features of C++, particulary C++11. I have a variety of classes that performs modification of the input, called Transformations. Now I want to build a pipeline of them, chaining them one after the other (and eventually having at the end of the chain a machine learning algorithm like a classifer or a regressor).

I think that a class with variadic template parameters is the perfect match for this use case. The thing is that I want to accept both rvalues and lvalues in the constructor.

In the case of an rvalue I want to move it, and in the case of an lvalue I want to keep a reference to it(although I'm still not 100% sure of this, because it could be a reference binded to some scope, and returning the pipeline as result of a function would blow up; but for the purporses of this library this could just be documented).

This would be the class:

template <class... Ts>
class Pipeline {
};

template <class T, class... Ts>
class Pipeline<T, Ts...> {
public:
    Pipeline(T?? transformation, Ts ??... following) : Pipeline<Ts...>(following...), _transformation(???) {}
...
}

I don't know if _transformation should be a reference or not, whether to std::move in the initialization list and what should be the types T and Ts in the constructor.

Edit: In the case of an lvalue, it should be non-const, because the pipeline can modify the transformation.

Aucun commentaire:

Enregistrer un commentaire