#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
    //
}

为什么不能默认设置template template parameter

最佳答案

试试看

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>>是一个简单的类型名;如果需要template<typename, typename, typename> class,则必须丢弃模板参数并使用框架:std::basic_string

10-06 07:54