Jan Bessai
Posts: 8
Nickname: janbessai
Registered: Sep, 2008
|
|
Re: C++ way to design tree structures
|
Posted: Mar 4, 2009 10:20 AM
|
|
#ifndef TREE_HPP #define TREE_HPP
#include <vector> #include <boost/optional.hpp>
template <typename T, int child_count = 2, template <typename, typename> class parent_container_type = std::vector, template <typename> class Allocator = std::allocator > class tree : public parent_container_type< boost::optional< tree <T, child_count, parent_container_type, Allocator> >, Allocator<boost::optional< tree <T, child_count, parent_container_type, Allocator> > > > { private: boost::optional<T> data; public: typedef parent_container_type< boost::optional< tree <T, child_count, parent_container_type, Allocator> >, Allocator<boost::optional< tree <T, child_count, parent_container_type, Allocator> > > > parent_type;
tree() : parent_type(child_count), data() { }
tree(const T & some_data) : parent_type(child_count), data(some_data) { } boost::optional<T> & operator*() { return data; } const boost::optional<T> & operator*() const { return data; } };
#endif
It is terrible to decipher the template arguments, but this way one can change the container class used for child nodes and its allocator on the fly. Example above still works. To have variadic templates would be great in this situation, as not all container classes imaginable need exactly two template parameters.
|
|