I would like to design a wrapper in C++ with a simple syntax:
Vector<double,stl> v; // aka std::vector<double>
Vector<double, eigen> w; // aka Eigen::VectorXd from Eigen library
Matrix<double, eigen> m; // aka Eigen::MatrixXd from Eigen library
However, I do not manage to get this syntax, especially for the two last examples. Here is my code for the case of wrapping of STL vectors:
#ifndef WRAPPER_HPP
#define WRAPPER_HPP
#include <cstdlib>
#include <vector>
//=============
// BASE WRAPPER
//=============
template<class ScalarType, template<class,class...> class WrappedType>
class Wrapper
{
protected:
WrappedType<ScalarType> wrapped_;
};
//==============
// STL WRAPPER
//==============
template<class ScalarType, template<class,class...> class WrappedType>
struct stl;
template<class ScalarType>
struct stl<ScalarType,std::vector> : public Wrapper<ScalarType,std::vector>
{
public:
size_t size()
{ return this->wrapped_.size(); }
};
//=======
// VECTOR
//=======
template<class ScalarType, template<class, template<class,class...> class> class Wrapper>
using Vector = Wrapper<ScalarType,std::vector>;
// **** Problem : I should not provide "std::vector" above ****
#endif
STL wrapper is a structure called stl. This structure is actually a subclass of Wrapper class. I have specialized stl structure for STL's vectors. I may do some other specalizations for some containers (lists, maps, ...). Thus, when declaring
Vector<double,stl> vec
I would like to be able to deduce from the pair (Vector, stl) that it corresponds to the specialization of stl for std::vector. However, I do not manage to do it. Each time I try something, I get an infinite recursion over template parameters.
There may be a good way to do this with some typedef or template alias but I cannot find it. It may be something like that:
template<class ScalarType, template<class, template<class,class...> class> class Wrapper>
using Vector = Wrapper<ScalarType,????>;
where ???? would be something equivalent to std::vector, but deduced from Wrapper. But I do not know if it is possible.
My design may also be naive. I would really appreciate any suggestion to improve it. I am only interested in the syntax of the code.
Thanks!