I have this function definition in my code:
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 ... } And when I try to call the function like this:
int main() { Bytes bytes{'H', 'e', 'l', 'l', 'o'}; std::string str = bytes2string(bytes); // error return 0; } I am met with the following error:
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) I'm pretty sure it should work but alas, it doesn't. Also Bytes is just a std::vector<char> in case anyone wanted to know.
std::stringis reallystd::basic_string<char>so I would have expected the compiler to pick up on that. In this particular caseCharTshould get deduced ascharCharTin the arguments, there's no way for it to figure out what the template parameters should be. You're plainly expecting to assign the result to astd::string, so you wantCharTto a basic char, but that doesn't factor into the parameter deduction.C++the return type doesn't participate in overload resolution.