一元加号(+)对文字字符串 [英] Unary plus (+) against literal string

查看:154
本文介绍了一元加号(+)对文字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我写了一个表达式:

Today I wrote an expression:

"<" + message_id + "@" +  + ">"

并惊讶于它实际编译。 (PS message_id 是一个QString,它也可以使用std :: string)

and got surprised that it actually compiled. (PS message_id is a QString, it would also work with an std::string)

像这样,当我工作时,不要一个变量,我希望编译器告诉我在哪里我仍然缺少条目。最终将看起来像这样:

I often do things like that, leave out a variable as I'm working and I expect the compiler to tell me where I'm still missing entries. The final would look something like this:

"<" + message_id + "@" + network_domain + ">"

现在我想知道为什么+一元运算符对字符串文字有效? / p>

Now I'd like to know why the + unary operator is valid against a string literal!?

推荐答案

一元 + 可应用于算术类型值,无范围枚举值和指针值,因为...

Unary + can be applied to arithmetic type values, unscoped enumeration values and pointer values because ...

C ++标准在C ++ 11§5.3.1/ 7中定义了它。

the C++ standard defines it that way, in C++11 §5.3.1/7.

在这种情况下, char const 类型数组的字符串文字衰减到 char const

In this case the string literal, which is of type array of char const, decays to pointer to char const.

当人们对某些东西的功能感到疑惑时,总是一个好主意。

It's always a good idea to look at the documentation when one wonders about the functionality of something.

&$一元的 + 操作符应具有算术,无范围枚举或指针类型,
result是参数的值。对积分或枚举操作数执行积分提升。
结果的类型是提升的操作数的类型。

“The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.”

这篇关于一元加号(+)对文字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆