As the new standard gains popular, programmers are able to use template template parameter along with varadic template to define a class template that looks like:
/* C++ code */
template <template <class...> class container = std::unordered_multimap, typename... args>
class basic_node {
public:
container<args...> set;
};
/* class partial specialization */
template <typename key, typename value, typename hash, typename pred, typename alloc>
class basic_node<std::unordered_multimap, key, value, hash, pred, alloc> {
protected:
std::unordered_multimap<key, value, hash, pred, alloc> set;
};
My idea is to define a class template that contains a set of its child in the composed container member:
/* We use CRTP to avoid complain about incomplete type tree_node */
class tree_node : public basic_node<
std::unordered_multimap,
std::string, /* key: name tag in text */
tree_node, /* CRTP on value : element object in container */
std::hash<tree_node>, /* some compatible hash */
std::equal_to<tree_node>, /* some compatible equal_to */
std::allocator<tree_node>> /* some a default allocator */
{
public:
/* ERROR: tree_node is of incomplete type because the compiler is now on it */
//std::unordered_multimap<std::string, tree_node, std::hash<tree_node>, std::equal_to<tree_node>, std::allocator<tree_node>> set; /* inherited members */
};
Is this approach sound? I mean, I used to confuse about the curiously_recurring_template_pattern (CRTP) idiom template tree_node<std::unordered_multimap<tree_node<std::unordered_multimap<tree_node<std::unordered_multimap<...>>>>>>... induced by design but now it seems handy to me if the above works, or maybe?
EDIT:
- Rectified parameters for std::unordered_multimap.
- Thanks and CRTP it is by the design.