Advertisement Jump to content
Sign in to follow this  
gasto

Increment and decrement unary operator hell.

This topic is 1821 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

If one attempts to  compile in Visual Studio 2008:
 

    UINT x = 5;

    signed int y = -x; /*Negation unary operator */



    cout << x++ << endl << x << endl; /*Postfix unary increment operator*/

    cout << x << endl;

    cout << ++x << endl; /*Prefix unary increment operator*/

The output is:
 

5
6
6
7

Whilst if we change the third argument for the first instance of cout to "--x", the output is:

4
5
5
6

The second printed integer is evidence that --x was called first, whilst in the former code, x++ was called

first. Not only that, it managed to add 1 to x before passing it as an insertion << argument despite the first argument being a postfix increment operator.

Mind blown.

Edited by gasto

Share this post


Link to post
Share on other sites
Advertisement

As an addition to my reply, if you really want to scratch your head:

    int a = 1;
    std::cout << ++a << ++a << ++a;

MSVC2012, both release and debug builds, prints "444".

 

There are six expressions; evaluate ++a three times, and the side effect of ++a three times. The side effect has to occur before the corresponding evaluation since it's a pre-increment, and the compiler simply stick all three increments first into a single a+=3 and then proceeds to evaluate the value of a as 4 three times.

 

Still undefined behavior, of course.

Share this post


Link to post
Share on other sites

Why is there an associativity chart if the evaluation order is not defined?

 

Those are separate concepts. If you find an expression like A() + B() + C(), the three function calls could happen in any order (or even in parallel!) and then the three results would be added left to right or in any other way that gives the same answer.

Share this post


Link to post
Share on other sites

I don't remember functions having side effects unless they possess a reference or pointer argument.

Share this post


Link to post
Share on other sites

I don't remember functions having side effects unless they possess a reference or pointer argument.

void A() { cout << "A was called!" << endl; }
void B() { ++myGlobalVariable; }

Share this post


Link to post
Share on other sites

I don't remember functions having side effects unless they possess a reference or pointer argument.

 

There are functional languages where functions don't have side effects. C++ is not one of them, as shown by osmanb.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!