c++ - How to define a template class for a linked list node with pointer as template type -
how define node template linked list? want keep pointer type template parameter can change unique_ptr or shared_ptr depends on available.
template<typename t, typename nodeptr> struct node{ t data; nodeptr parent = nullptr; }; the question how initiate class nodeptr shared_ptr < node <t ,what?> > type?
the "simplest" solution can think of variadic template template parameter:
template<class t,template<class ...> class ptr_t> struct node { t data; ptr_t<node> parent{nullptr}; }; this works both unique_ptr , shared_ptr this:
node<int,std::shared_ptr> roots; node<int,std::unique_ptr> rootu; as suggested yourself, you'd have introduce type alias, if want use raw pointers:
template<class t> using raw_ptr = t*;
Comments
Post a Comment