微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – T = char不能推导出std :: basic_string foo =“foo”?

题:
在下面的代码中,模板参数类型扣除对于第一个样本似乎失败,但不适用于第二个样本.我不明白为什么第一个样本不能推导出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>可以部分或全部专门化,这些专业的成员与主要模板的成员没有任何关系.

原文地址:https://www.jb51.cc/c/113875.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐