Correct practice using 'using' in C++ templates -
i have small class uses several stl lists internally
template<class t> class mc_base { using obj_list = std::list<t>; using obj_val = typename obj_list::value_type; using obj_itr = typename obj_list::iterator; using obj_citr = typename obj_list::const_iterator; obj_list a,b,c; ... };
with using
statements, if write iterator inside class definition, looks nice , clean:
obj_itr begin() { return a.begin(); }; obj_itr end() { return a.end(); }; obj_citr begin() const { return a.begin(); }; obj_citr end() const { return a.end(); };
writing new functions, again inside class definition, easy since can use names obj_xxxx
names when needed. furthermore, if decide change container type (to std::vector
) @ point later, have change 1 line , long new container supports same actions should seamless.
this problematic when want define new class function outside of class definition
template<class t> obj_itr mc_base<t>::foo(obj_itr x) { ... }
i'm not sure how "bring out" using statements work correctly templates , not define them every function overly verbose. also, don't want pollute namespace using statements.
is there proper way use using
templates?
you can use trailing return type. type looked in scope of class, parameter types, nested types don't need qualification.
template<class t> auto mc_base<t>::foo(obj_itr x) -> obj_itr { ... }
Comments
Post a Comment