Jump to content
  • Advertisement
Sign in to follow this  
helix

[c++] x + + y compiles and runs??

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just looking through some code that someone else had written for a library that we are using and found a line where it looks like they deleted a value in the addition but left the plus sign. Yet it compiles and runs as if there is only one plus sign. I would expect the compiler to complain and give an error (or at least a warning) but it says nothing and we're at warning level 3. Does the compiler just ignore the redundant operator?

Share this post


Link to post
Share on other sites
Advertisement
You can append a single + before any expression and it compiles. It's called unary plus. I don't know why C++ allows it except that it also allows unary minus.

Share this post


Link to post
Share on other sites
Oh, I think I see what you're saying.

So if I have z = x + + y; That is just saying z = x + (+y); which is just specifying that y is positive (actually -- see my edit below).

Correct?

edit:
Also, does this unary + operator actually do anything or is it a no-op? ie, if y is negative will it make it positive? I would think that it doesn't do anything because it shouldn't affect a positive number.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
y isn't necessarily positive. +(-5) → -5.


Ok, that's what I was getting at in my edit. So it really does absolutely nothing then?

Share this post


Link to post
Share on other sites
According to the standard, section 5.3.1 paragraph 6:
Quote:

The operand of the unary + operator shall have arithmetic, 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.


So it does integral promotion. But then using a value in almost any expression also does integral promotion. About the only point I can see is that it theoretically should turn a variable reference into a rvalue, if you want to make sure that it's not being treated as an lvalue.

Share this post


Link to post
Share on other sites
Well, it does integral promotion, which means that values of type char, signed char, unsigned char, short and unsigned short will be turned into an int iff the entire range of the type can fit into an int otherwise it turns into an unsigned int.

wchar_t and enumerations turn into the first one in this list that can fit all the values of the type: int, unsigned, long and unsigned long.

bools are turned into ints, and bitfields do some weird magic that I can't remember off the top of my head.

Oh, and these are all rvalues.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!