I have a tree_node
class and a tree
class.
template<typename T>
class tree_node
{
public:
tree_node(const std::string& key_, const T& value_)
: key(key_), value(value_)
{
}
private:
T value;
std::string key;
};
template<typename T>
class tree
{
public:
tree() : root(new tree_node<T>("", ???)) { }
private:
tree_node<T>* root;
};
tree_node
expects an instance of T
when creating. How can I pass it in the ???
place? I can say T()
, but it will work only if T
has a parameterless constructor. I can't have a parameterless constructor for tree_node
as it won't compile if T
doesn't have a parameterless constructor.
I am looking for a way to design tree_node
which can hold all types correctly including pointer types.
Edit
After trying various methods, I found that boost::optional
is helpful in this case. I can make the T value
into boost::optional<T> value
. This will solve the empty constructor issue. So I can have another constructor overload of tree_node
which just takes a key
. This can be used by the root node. Is this the correct way to go?
Thanks..