return value of += operator (probably an easy q for somebody)

G

Guest

Consider the following small program

#include <stdio.h

#define printvars printf("a = %d, b = %d, c = %d\n", a, b, c)

int main(

int a = 1, b = 2, c = 3
printvar
int x = a + (b += 3)
printf("x = %d\n", x)
printvar
return 0


on line 3, I'm saying I want the variable b to be incremented by 3, and then the result added to a and the result of that stored in x
But is it according to the C++ standard that the expression '(b += 3)' should actually RETURN 'b+3' aswell as just doing the action of incrementing 'b' by 3? Or does it just happen to on MSVC

Thanks
 
S

Simon Trew

songie D said:
int main()
{
int a = 1, b = 2, c = 3;
int x = a + (b += 3);
printf("x = %d\n", x);
return 0;
}

on line 3, I'm saying I want the variable b to be incremented by 3, and
then the result added to a and the result of that stored in x.
But is it according to the C++ standard that the expression '(b += 3)'
should actually RETURN 'b+3' aswell as just doing the action of incrementing
'b' by 3? Or does it just happen to on MSVC?

I'm not going to quote from the standard, but the expression "b + 3" should
actually act as if returning a reference to b, i.e. its signature would look
like this

int & operator +=(int other);

So yes, the new value of b will be added to a, and this is portable.

Note that you are not guaranteed the order of evaluation of operands, so if
"a" was an alias for b then all bets would be off:

int b = 2, &a = b; // a is an alias for b
int x = a + (b += 3); // this is not portable as "a" might be evaluated
before or after "(b += 3)".
 
J

James Curran

um... Exactly what is that getting you that

b+=3;
x = a + b;

does not.

The two versions should generate exactly the same machine code, but with
mine, NO ONE will ever have to ask on a newsgroup, what it does.....

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
songie D said:
Consider the following small program:

#include <stdio.h>

#define printvars printf("a = %d, b = %d, c = %d\n", a, b, c);

int main()
{
int a = 1, b = 2, c = 3;
printvars
int x = a + (b += 3);
printf("x = %d\n", x);
printvars
return 0;
}

on line 3, I'm saying I want the variable b to be incremented by 3, and
then the result added to a and the result of that stored in x.
But is it according to the C++ standard that the expression '(b += 3)'
should actually RETURN 'b+3' aswell as just doing the action of incrementing
'b' by 3? Or does it just happen to on MSVC?
 
S

songie D

But... your version's two lines. Mine's one. That makes mine 'better'.
I've a compulsion for wanting to write code that's as confusing
to others as possible. Does this make me a sick pervert?
 
S

Simon Trew

songie D said:
But... your version's two lines. Mine's one. That makes mine 'better'.
I've a compulsion for wanting to write code that's as confusing
to others as possible. Does this make me a sick pervert?

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top