Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法推断基本字符串的模板参数_C++_Templates - Fatal编程技术网

C++ 无法推断基本字符串的模板参数

C++ 无法推断基本字符串的模板参数,c++,templates,C++,Templates,我的代码中有此函数定义: template < class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > std::basic_string<CharT, Traits, Allocator> bytes2string(const Bytes& bytes) { // do

我的代码中有此函数定义:

template <
    class CharT,
    class Traits = std::char_traits<CharT>, 
    class Allocator = std::allocator<CharT>
> std::basic_string<CharT, Traits, Allocator> bytes2string(const Bytes& bytes)
{
     // do work ...
}
我遇到以下错误:

error: 
      no matching function for call to 'bytes2string'

note: 
  candidate template ignored: couldn't infer template argument 'CharT'
> std::basic_string<CharT, Traits, Allocator> bytes2string(const Bytes& bytes)
错误:
对“bytes2string”的调用没有匹配的函数
注:
已忽略候选模板:无法推断模板参数“图表”
>std::basic_string bytes2字符串(常量字节和字节)

我很确定它应该会起作用,但唉,它不会起作用。另外,
Bytes
只是一个
std::vector
,以防有人想知道。

仔细查看您的签名:

template <
    class CharT,
    class Traits = std::char_traits<CharT>, 
    class Allocator = std::allocator<CharT>
> std::basic_string<CharT, Traits, Allocator> bytes2string(const Bytes& bytes);
遗憾的是C++没有类型推断——这样就不可能推断出返回类型的模板参数。函数模板参数只能通过传递给函数的参数来推导

如果您将签名更改为:

template <
    class CharT,
    class Traits = std::char_traits<CharT>, 
    class Allocator = std::allocator<CharT>
> void bytes2string(std::basic_string<CharT, Traits, Allocator>& out, const Bytes& bytes);
将推导出模板参数


如果函数应该返回
std::string
,那么就这样写:

std::string bytes2string(const Bytes& bytes);
如果假定它能够生成
std::basic_string
的任意实例化,那么您需要提供适当的模板参数。这通常是通过传入适当类型的字符串来完成的。这在这里似乎不合适,因为除了提供模板参数外,传入字符串没有明显的用途

另一种方法是在调用时命名适当的类型:

std::string str = bytes2string<char>(bytes);
std::string str=bytes2字符串(字节);

您希望如何推导模板参数?嗯
std::string
实际上是
std::basic_string
,所以我希望编译器能够理解这一点。在这种特殊情况下,
CharT
应该被推断为
char
,而参数中的
CharT
没有任何来源,因此它无法确定模板参数应该是什么。很明显,您希望将结果分配给
std::string
,因此您希望
CharT
分配给基本字符,但这不包括在参数推断中。在
C++
中,返回类型不参与重载解析。是的,我猜了很多,但我确实想要类型推断。谢谢你。我觉得它支持这样的推论。谢谢
std::string str;
bytes2string(str, bytes);
std::string bytes2string(const Bytes& bytes);
std::string str = bytes2string<char>(bytes);