gpt4 book ai didi

c++ - 为什么不能默认模板模板参数?

转载 作者:行者123 更新时间:2023-11-30 00:45:36 25 4
gpt4 key购买 nike

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

关于c++ - 为什么不能默认模板模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42810147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com