C++ - Template template parameter with default argument doesn't work

783 views Asked by At

So I have a class of the form:

template <typename T, template <typename T> class Container = std::vector>
class MyArray : Container<T>
{
    ...
}

Now I'm trying to instantiate this as

template <typename T>
class MyClass
{
    MyArray<T> array;
}

This is producing an error:

error: type/value mismatch at argument 2 in template parameter list for 'template<class T, template<class T> class Container> class MyArray'
MyArray<T> y;

And it points to the > of MyArray<T>.

What's wrong in this? Why can't I have a default template parameter?

Thanks.

1

There are 1 answers

8
TartanLlama On BEST ANSWER

The issue is that std::vector takes two template arguments: the type and the allocator. Many other container types will takes additional policy arguments which have defaults to allow you to instantiate like T<U>.

In order to support this, you can say that your template template parameter should take at least one template parameter using variadic templates (typename...):

template <typename T, template <typename, typename...> class Container = std::vector>
class MyArray : Container<T>
{
    ...
}