c++ - Template template parameters - extension to std::array -
i have question template template parameters:
let's consider following class:
template<typename t, template<class, class=std::allocator<t> > class underlyingcontainertype = std::vector> class mycontainer { public: t value1; t value2; underlyingcontainertype<t> container; mycontainer() {} /* methods implementation goes here */ };
in example, mycontainer container chat uses underlying stl-compatible container whatever stuff.
declaring underlying container type template template parameters, instead of regular template argument, allows handy usage of class mycontainer, such as:
mycontainer<int> mcv; //implicitly using vector mycontainer<int, std::list> mcl; //no need write std::list<int> //template template parameters
now, while work of stl-container, such std::vector, std::deque, std::list, , forth, not work, example, std::array provided in c++11.
the reason std::array has different signature of template parameters vector. specifically, are:
std::vector<class t, class allocator = std::allocator<t> > std::array<class t, int n>
my question if there way generalize class mycontainer, underlying container can std::array well.
i thank in advance.
the commonality between interfaces of vector
, array
limited element type. container should reflect that:
template<typename t, template<typename> class underlying_container> struct container { underlying_container<t> storage; };
usage requires tiny trick:
template<typename t> using vec = vector<t>; template<typename t> using arr2 = array<t, 2>;
note vec
, unlike vector
, has fixed allocator , arr2
, unlike array
, has fixed size.
usage simple:
container<int, vec> a; container<double, arr2> b;
alternatively, if prefer match interface used vector, add template alias array
instantiates size , adds unused type parameter:
template<typename t, typename> using arr2 = array<t, 2>;
Comments
Post a Comment