c++templatestemplate-templates

Using a type without template arguments as a template argument


I have a class named Registry which correlates an ID with some data. I would like to make it so the underlying structure which stores these pairs can be any std::mapish type, of which the user can define (e.g.: std::map, std::unordered_map).

My initial thought was to do something like this:

template<typename Value, typename Container, typename ID = size_t>
class Registry{
    using Storage = Container<ID, value>;
    static_assert(std::is_same<Storage, std::map> || std::is_same<Storage, std::map>, "Underlying storage type must be a std::map-ish.");
    public:
    Storage<ID, Value> store;
    ...

However, trying to use the class results in an error:

Registry<bool, std::map> testRegistry;
err) argument list for class template std::map is missing

I understand the compiler's complaint, but is there any way to work around it so that this syntax (or something similar) might work?

Thanks for the advice.


Solution

  • You need to declare Container as a template template parameter. E.g.

    template<typename Value, template <typename...> class Container, typename ID = size_t>
    class Registry{
    
        using Storage = Container<ID, Value>;
        static_assert(std::is_same_v<Storage, std::map<ID, Value>> || std::is_same_v<Storage, std::unordered_map<ID, Value>>, "Underlying storage type must be a std::map-ish.");
        public:
        Storage store;
        ...
    

    Other issues: