Search code examples
c++c++11templatesoverloadingtemplate-templates

Why cannot a template template parameter be defaulted?


#include <string>

template
<
    typename CharType,
    template<class, class, class> class StringType = std::basic_string
    <CharType,  std::char_traits<CharType>, std::allocator<CharType>>
>
void f(CharType, StringType)
{}

int main()
{
    char c;
    std::string str;

    f(c, str);
    //
    // error : default template argument for
    //  a template template parameter must be a class template
    //
}

Why cannot a template template parameter be defaulted?


Solution

  • Try with

    template
    <
        typename CharType,
        template<class, class, class> class StringType = std::basic_string
    >
    void f(CharType, StringType<CharType, std::char_traits<CharType>,
                                std::allocator<CharType>>)
    {}
    

    std::basic_string<CharType, std::char_traits<CharType>, std::allocator<CharType>> is a simple typename; if you want a template<typename, typename, typename> class, you have to throw away the template arguments and use the skeleton: std::basic_string