题:
在下面的代码中,模板参数类型扣除对于第一个样本似乎失败,但不适用于第二个样本.我不明白为什么第一个样本不能推导出T = char.我会认为,当从“foo”转换为std :: bacis_string< T>时,可以推导出T,但是即使没有这样做,我提供了第二个函数参数,我认为这将明确地限制T到char .为什么会失败?
在下面的代码中,模板参数类型扣除对于第一个样本似乎失败,但不适用于第二个样本.我不明白为什么第一个样本不能推导出T = char.我会认为,当从“foo”转换为std :: bacis_string< T>时,可以推导出T,但是即使没有这样做,我提供了第二个函数参数,我认为这将明确地限制T到char .为什么会失败?
#include <iostream> #include <string> template <typename T> void print(const std::basic_string<T>& a,const std::basic_string<T>& b) { std::cout << a << b << std::endl; } int main() { std::string bar = "bar"; print("foo",bar); }
错误:
string.cpp:14:5: error: no matching function for call to 'print' print("foo",bar); ^~~~~ string.cpp:6:6: note: candidate template ignored: Could not match 'basic_string<type-parameter-0-0,char_traits<type-parameter-0-0>,allocator<type-parameter-0-0> >' against 'char const[4]' void print(const std::basic_string<T>& a,const std::basic_string<T>& b) ^ 1 error generated.
#include <iostream> #include <string> template <typename T> void print(const std::basic_string<T>& a,const std::basic_string<T>& b) { std::cout << a << b << std::endl; } int main() { std::string foo = "foo"; std::string bar = "bar"; print(foo,bar); }