C++ template parameter with default parameters

The problem is that your template template parameter has only two template parameters, as opposed to map, which has four.

template<class Key, template<class, class, class, class> class Map = std::map>
class MyClass {
};

Or

template<class Key, template<class...> class Map = std::map>
class MyClass {
};

Should compile.
However, to avoid such problems, try to take the map type instead, and extract the key type via the corresponding member typedef. E.g.

template <class Map>
class MyClass {
    using key_type = typename Map::key_type;
};

Your code will compile in C++17. A long-standing defect report of the C++ Core Working Group (CWG 150) was resolved (by P0522R0) in time for C++17.

cppreference.com also discuss this here, and include a helpful example:

template<class T> class A { /* ... */ };
template<class T, class U = T> class B { /* ... */ };
template <class ...Types> class C { /* ... */ };

template<template<class> class P> class X { /* ... */ };
X<A> xa; // OK
X<B> xb; // OK in C++17 after CWG 150
         // Error earlier: not an exact match
X<C> xc; // OK in C++17 after CWG 150
         // Error earlier: not an exact match

Testing with my version of GCC (8.3.0), I find that using the -std=c++17 flag will successfully compile your program; while using earlier versions of C++ (e.g. -std=c++14 or -std=c++11) will fail.

Tags:

C++

Templates