[英]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
上课obj1
和obj2
作为参数。 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.