phrack101
phrack101

Reputation: 91

C++ concatenation; string + double; operator+ vs operator+=

gcc v10.2.0, -std=c++11

I'm not trying to convert the double value to a literal string using 'std::to_string()'. Trying to acheive a similar effect of adding an integer to a string but with a double value instead.

Expected output: "abcdA"

string s { "abcd" };
double d { 65.1 };
// s = s + d;    // Error. no match for ‘operator+’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘double’)
s += d;

Both the 'operator+' and 'operator+=' methods of 'string' class have a version which accepts a 'char' argument but only the 'operator+=' method seems to receive an implicitly converted value and does not produce an error.

Why does the compiler choose to pass a converted value to one over the other.

Upvotes: 1

Views: 347

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 72044

operator += is a member function, and not a template by itself. So for a given string instance, its RHS argument is char. The compiler will look for a conversion to this type.

operator + is a free function template, templated so that it works with any basic_string instantiation:

template<class CharT, class Traits, class Alloc>
std::basic_string<CharT,Traits,Alloc>
    operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
               CharT rhs );

Note that CharT is used both in the basic_string argument and as the RHS. This means that the compiler will try to deduce it from both arguments, and needs to arrive at a consistent result. But in your addition, the left side is a string, making CharT a char, but the right side is a double, making CharT a double. This inconsistency is why the compiler cannot select a type for CharT and thus dismisses the overload completely. Then, when it is done looking through all the many overloads of operator +, it gives up and says that there is no matching function.

Upvotes: 7

Related Questions