简体   繁体   中英

Why can't I concat 2 const char* to a std::string?

I know that the operator + for std::string is not overloaded for two const char* . Thus,

std::string str = "Hello" + " World";

does not work; Whereas, the following code works perfectly.

std::string hello = "Hello";
std::string str = hello  + " World";

Moreover, static string concatenation (without +operator) works fine as well

std::string str = "Hello" " World";

What is the technical reason for not having a operator + for two const char* ?

The technical reasons for not having such an operator are centred around ownership (what would own this operator? std::string , the global namespace, or something else), the fact that adding two const char* pointers makes no sense, and other issues centred around the properties of read-only NUL-terminated character literals.

"Hello" + " World" knows nothing about what it's about to be assigned to. The use of the + requires the const char[] literals to decay to const char* pointers. Adding two pointers makes no sense at all and so modern compilers will issue a diagnostic stating that + is not defined for const char[] types.

"Hello" " World" is the C-idiomatic string literal concatenation syntax. That's been around since the 1970s and helps folk write long strings when there were only 80 or so characters visible per line of code.

hello + " World" is using the overloaded + operator on std::string . That's because hello is a std::string .


From C++14 onwards you could exploit user defined literals with

std::string str = "Hello"s + " World";

or even

std::string str = ""s + "Hello" + " World";

Note the suffixed s .

What is the technical reason for not having a operator + for two const char*?

In C++ only certain types of operations defined for pointers - adding/subtraction intergal value from/to pointer, subtracting a pointer from another pointer. Adding a pointer to pointer is not defined as it does not make any sense. Char pointers are not different than any other in that sense and there is no viable solution to provide such exception for const char * as it would not work in general case. It could only work for string literals, but construction to concatenate them already in the language.

This is from the language. If you are asking why such overload is not provided by library writers - you cannot override operators for builtin types, only for user defined. Neither string literal nor const char* is user defined.

I know that the operator + for std::string is not overloaded for two const char*.

This expectation is like you write + for Obj classes taking obj1 and obj2 as arguments. Then saying 1+5 does not overload your operator. Of course here Obj class is not involved.

The part

std::string str = hello  + " World";

works because operator involves std::string overload 4 from here .

And finally, "Hello" " World" does not concate anything. It is like writing one string in separate parts. This helps you writing a long sentence in two lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM