【发布时间】:2020-08-10 02:27:37
【问题描述】:
为什么将std::basic_string作为函数模板的参数,从const char*推导失败,而直接构造却可以推导成功?
#include <string>
#include <iostream>
template<class Char/*, class Traits, class Allocator*/>
//^doesn't matter whether the second and third template parameter is specified
void printString(std::basic_string<Char/*, Traits, Allocator*/> s)
{
std::cout << s;
}
int main()
{
printString("hello"); //nope
std::basic_string s{ "hello" };//works
}
我找到了一个相关的帖子here,但答案并没有解释背后的原因
【问题讨论】:
-
如果多个特化具有匹配的构造函数,编译器将无法选择一个类型。
标签: c++ templates implicit-conversion template-argument-deduction