lundi 7 décembre 2015

Alternative to template variables?

I'm building a binary search tree. As the generic person I am, I want to allow all types to be able to act as keys to nodes in the tree.

So I came up with the following:

class foo
{
private:
    template<class T>
    struct node
    {
        T key;
        node* left;
        node* right;
    };

    node<>* _root;  //point of interest
public:
    //.....
    template<class T>
    void insert(const T& key);
};

As I insert the nodes into the tree, I can create node objects according to the type of the key, but I don't know how to declare the _root in such a way (when using insert on an empty tree, I can easily pick the type for _root).

I believe C++14's template variables can help me here, but unfortunately MSVC's compiler hasn't implemented that feature yet.

Question: How do I declate _root in the most generic way? How did people do it in the past?

Aucun commentaire:

Enregistrer un commentaire