r/cpp_questions • • 7d ago

OPEN Operator + overloads on std::string

Consider https://godbolt.org/z/oY9r59P5E

#include <string>

const char *temp = "Hello";

std::string returnstring(){
    return "abc";
}

int main(){
    std::string abc = temp + returnstring() + "_";
    std::string def = temp + "_";
}

The second line in main() does not compile with the error of "invalid operands". The first line compiles fine and that can only be due to the intervening "returnstring()" call which returns an std::string, somehow.

What is the cleanest way to get the intent behind the second line to compile? In my application, I cannot change const char * temp into an std::string.

9 Upvotes

19 comments sorted by

View all comments

13

u/Highborn_Hellest 7d ago

Temp is a const char pointer. Def is not. You need to cast temp to string

std:: string (temp)+ "_"; Should work. Such overloads dont exist by default.

8

u/TeraFlint 7d ago

To expand on this, string literals themselves are a primitive type: a statically sized const char[n] array, that often gets interpreted as a const char * pointer to its first element.

std::string, on the other hand, is nothing more than a class the standard ships. And that class defined the + operator to be usable if it takes part on the left or right side, and if it's paired with a compatible type (one of which is one of these primitive string literals).

If you (OP) use + between two non-std::string objects, these custom operators won't have any chance to do what they're meant to do. If you introduce an std::string somewhere in the first two operands, it ripples through the entire chain of + operators to eventually produce one combined std::string object. That's why the comment above suggested the construction of an std::string.

3

u/onecable5781 7d ago

If you introduce an std::string somewhere in the first two operands, it riples through the entire chain of + operators to eventually produce one combined std::string object.

I imagined so. In a more general case, with n (>2) operands, I suppose still the same holds: one of the first two of n operands has to be an std::string assuming the compiler evaluates L to R ? It would not work if only the last of, say, 10 operands is an std::string while all of the others are const char * ?

3

u/TeraFlint 7d ago

You are correct. The associativity of + is left-to-right.

This means, that the type information also flows from left to right.

2

u/flyingron 7d ago

x + y + z means (x + y) + z as far as the syntax goes as + is left-to-right associative.

Note that just refers to the interpretation, not the order of evaluation.

2

u/flyingron 7d ago

String literals are never "interpreted" as const char*. They are implicitly convertible to const char*.

2

u/dodexahedron 7d ago

And, for a detailed explanation of what it all means and how to interpret constness and pointiness, check this out: https://isocpp.org/wiki/faq/const-correctness#const-ptr-vs-ptr-const

Read the whole page. There are subtleties that it explains specifically.

The short of it is: You evaluate it from right to left and should write it that way in tje specific cases where there are two ways to do it, so it stays consistent.

From right to left, const int * const i reads as "The symbol 'i' is a constant pointer to an int which is constant.

Your example, const char * temp = "Hello"; reads as "assign the value "Hello" to the symbol named temp, which is a pointer to a char that is constant.

The pointer is movable.

The string constructor needs to know that what it is being fed is not going to move or be changed while it initializes. So, both the pointer and the referent chars need to be const.

A const char * const is such a value, reading as a constant pointer to a char which is constant (right to left). Thus, a non-movable pointer to read-only char values.