It is required to implement an arbitrary tree, where each node can be a string, an integer or a real number, and have any number of children, also one of these three types (children can be of different types). While designing a class for a node, I got two ideas on how to do this:
template <typename T>
class tree_node
{
using value_type = typename std::enable_if
<
std::is_same<std::string, T>::value ||
std::is_same<int, T>::value ||
std::is_same<float, T>::value, T
>::type;
value_type data;
std::vector<??????> children;// What type must be there?
public:
...
and
struct node_data {};
struct str_node: public node_data
{
std::string data;
str_node(std::string &str): data(str) {};
};
struct int_node: public node_data
{
int data;
int_node(int n): data(n) {};
};
struct float_node: public node_data
{
float data;
float_node(float f): data(f) {};
};
class tree_node
{
std::shared_ptr<node_data> val;
std::vector<std::shared_ptr<node_data>> children;
public:
....
Is there an elegant solution (for example, some design patterns) to this problem?