Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Today I wrote an expression:

"<" + message_id + "@" +  + ">"
                          ^
                          |
                          \____  see that extra '+' here!

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 + ">"

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
247 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

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.


“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.”


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...