简体   繁体   English

为什么我不能将2个const char *连接到std :: string?

[英]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* . 我知道运算符+ for std::string没有为两个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* ? 没有运算符+两个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. 没有这样的运算符的技术原因集中在所有权(什么将拥有此运算符? std::string ,全局命名空间,或其他东西),添加两个const char*指针没有意义的事实,以及其他问题以只读NUL终止字符文字的属性为中心。

"Hello" + " World" knows nothing about what it's about to be assigned to. "Hello" + " World"对它将被分配到什么一无所知。 The use of the + requires the const char[] literals to decay to const char* pointers. 使用+需要const char[]文字衰减const char*指针。 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. 添加两个指针完全没有意义,因此现代编译器将发出一个诊断,指出+没有为const char[]类型定义。

"Hello" " World" is the C-idiomatic string literal concatenation syntax. "Hello" " World"是C-idiomatic字符串文字串联语法。 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. 这种情况自20世纪70年代以来一直存在,并且当每行代码只有80个左右的字符可见时,帮助民间写长字符串。

hello + " World" is using the overloaded + operator on std::string . hello + " World"std::string上使用重载 +运算符。 That's because hello is a std::string . 那是因为hello是一个std::string


From C++14 onwards you could exploit user defined literals with 从C ++ 14开始,您可以利用用户定义的文字

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

or even 甚至

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

Note the suffixed s . 注意后缀s

What is the technical reason for not having a operator + for two const char*? 没有运算符+两个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. 在C ++中,只为指针定义了某些类型的操作 - 从/向指针添加/减少intergal值,从另一个指针中减去指针。 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. 在这种意义上,Char指针与其他任何指针没有什么不同,并且没有可行的解决方案来为const char *提供这样的异常,因为它在一般情况下不起作用。 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. 字符串文字和const char*都不是用户定义的。

I know that the operator + for std::string is not overloaded for two const char*. 我知道运算符+ for std :: string没有为两个const char *重载。

This expectation is like you write + for Obj classes taking obj1 and obj2 as arguments. 这种预期是喜欢你写+Obj上课obj1obj2作为参数。 Then saying 1+5 does not overload your operator. 然后说1+5不会使您的操作员过载。 Of course here Obj class is not involved. 当然这里没有参与Obj课程。

The part 那个部分

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

works because operator involves std::string overload 4 from here . 是因为运算符从这里涉及std::string overload 4。

And finally, "Hello" " World" does not concate anything. 最后, "Hello" " World"并没有结合任何东西。 It is like writing one string in separate parts. 这就像在一个单独的部分写一个字符串。 This helps you writing a long sentence in two lines. 这有助于你用两行写一个长句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我可以在std :: map中使用const char *作为键<std::string, int> - Why I can use const char* as key in std::map<std::string, int> 为什么我不能在这个专门的 function 模板中将字符串文字传递给 const char* const& - Why I can't pass string literals to const char* const& in this specialized function template 为什么我不能使类内初始化的`const const std::string` 成为静态成员 - Why can't I make in-class initialized `const const std::string` a static member 为什么std :: string {“ const char ptr”}有效? - Why std::string{“const char ptr”} works? std :: string和const char * - std::string and const char * 允许为std :: string分配“const char *”,但是不能编译分配给std :: wstring。 为什么? - Assigning a “const char*” to std::string is allowed, but assigning to std::wstring doesn't compile. Why? 为什么我可以将字符串文字初始化为const char *和QString而不是QString *? - Why can I initialize string literal as const char* and QString but not QString*? C ++:为什么我不能用sprintf打印const char *? - C++ : Why I can't print a const char* with sprintf? std :: string强制转换为const char *在std :: unordered_set中找不到<const char *> - std::string casted to const char * can not be found in an std::unordered_set<const char *> 为什么编译器不能优化std :: string concat? - Why compiler can not optimize std::string concat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM