Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ templated map contain itself

I have a templated class, that can have a map type as template parameter.

template<typename map_t> class my_class
{
    map_t my_map;
};

Now I'd like the value type of the map, to be equal to this class.

my_class<std::map<std::string, my_class<...> > > problems;

However, this is impossible to declare. How can I achieve the same effect?

like image 384
Ruud Avatar asked Feb 26 '26 04:02

Ruud


1 Answers

Since you want to be generic in the container type, the closest I have been able to find (if I understand well your intent) is:

template <template <typename, typename> class Map>
struct my_class
{
    typedef typename Map<std::string, my_class>::type map_t;
    map_t children;
};

// Since the standard doesn't allow default arguments to be
// taken into account for template template parameters, we
// have to close them.
// Write one such structure for each map type you want.
struct std_map
{ 
    template <typename Key, typename Value>
    struct rebind { typedef std::map<Key, Value> type; }
};

and use

my_class<std_map::rebind> problematic_tree;
like image 192
Alexandre C. Avatar answered Feb 27 '26 18:02

Alexandre C.