c – T = char不能推导出std :: basic_string foo =“foo”?
发布时间:2020-12-16 05:43:27 所属栏目:百科 来源:网络整理
导读:题: 在下面的代码中,模板参数类型扣除对于第一个样本似乎失败,但不适用于第二个样本.我不明白为什么第一个样本不能推导出T = char.我会认为,当从“foo”转换为std :: bacis_string T时,可以推导出T,但是即使没有这样做,我提供了第二个函数参数,我认为这将明
题:
在下面的代码中,模板参数类型扣除对于第一个样本似乎失败,但不适用于第二个样本.我不明白为什么第一个样本不能推导出T = char.我会认为,当从“foo”转换为std :: bacis_string< T>时,可以推导出T,但是即使没有这样做,我提供了第二个函数参数,我认为这将明确地限制T到char .为什么会失败? Does not work: #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. Works: #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); } 解决方法
问题是这里需要转换.为了推断T,编译器必须检查std :: basic_string的所有可能的实例,并查看哪些可以从一个const char *(或者实际上是const char(&)[4])构造.这当然是不可能的,因为它们中的无数很多.为什么它必须检查所有的,不能只是扫描主模板定义的构造函数使用const char *或const char(&)[4],对于一些T,std :: basic_string< T>可以部分或全部专门化,这些专业的成员与主要模板的成员没有任何关系.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |