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

Re: return value of += operator (probably an easy q for somebody) by Simon

Simon
Wed May 12 04:01:27 CDT 2004

"songie D" <anonymous@discussions.microsoft.com> wrote in message
news:0693C351-85D1-44A7-8861-CC03F0258606@microsoft.com...
> 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)".



Re: return value of += operator (probably an easy q for somebody) by anonymous

anonymous
Wed May 12 06:16:10 CDT 2004

that's great, cheers
I just wanted to make sure what i was doing wasn't microsoft specific, for the sake of having clean code really
And yes, it is always nice to hear of the situation when this could break, so I myself can be the judge of whether this would ever prevail, so thanks for that 'alias' example.

Re: return value of += operator (probably an easy q for somebody) by James

James
Wed May 12 10:26:35 CDT 2004

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" <anonymous@discussions.microsoft.com> wrote in message
news:0693C351-85D1-44A7-8861-CC03F0258606@microsoft.com...
> 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?
>
> Thanks



Re: return value of += operator (probably an easy q for somebody) by Simon

Simon
Wed May 12 14:19:15 CDT 2004

"songie D" <anonymous@discussions.microsoft.com> wrote in message
news:ABF364DD-1A15-46A1-B206-79F9D00AA278@microsoft.com...
> that's great, cheers.
> I just wanted to make sure what i was doing wasn't microsoft specific, for
the sake of having clean code really.
> And yes, it is always nice to hear of the situation when this could break,
so I myself can be the judge of whether this would ever prevail, so thanks
for that 'alias' example.

Actually that behavior is not just non-portable, it is undefined.



Re: return value of += operator (probably an easy q for somebody) by songie

songie
Wed May 12 14:29:28 CDT 2004

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?

"James Curran" <JamesCurran@mvps.org> wrote in message
news:OmdQLWDOEHA.2244@tk2msftngp13.phx.gbl...
> 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" <anonymous@discussions.microsoft.com> wrote in message
> news:0693C351-85D1-44A7-8861-CC03F0258606@microsoft.com...
> > 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?
> >
> > Thanks
>
>



Re: return value of += operator (probably an easy q for somebody) by Simon

Simon
Wed May 12 19:19:56 CDT 2004

"songie D" <songie@D.com> wrote in message
news:uAPZxdFOEHA.1400@TK2MSFTNGP10.phx.gbl...
> 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.



Re: return value of += operator (probably an easy q for somebody) by David

David
Thu May 13 06:48:04 CDT 2004


"Simon Trew" <ten.enagro@werts> wrote in message
news:OHqAEAIOEHA.2480@tk2msftngp13.phx.gbl...

> "songie D" <songie@D.com> wrote in message
> news:uAPZxdFOEHA.1400@TK2MSFTNGP10.phx.gbl...
> > 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.

Not to worry. Write it

b+=3; x = a + b;

and you too have only one line of code. :-(

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm



Re: return value of += operator (probably an easy q for somebody) by anonymous

anonymous
Thu May 13 09:21:16 CDT 2004

That's not 1 line of code. It's two lines of code on one line of text.

Re: return value of += operator (probably an easy q for somebody) by Simon

Simon
Thu May 13 18:48:44 CDT 2004

"songie D" <anonymous@discussions.microsoft.com> wrote in message
news:5B56E1D6-AF3A-405C-A45C-88E611558C28@microsoft.com...
> That's not 1 line of code. It's two lines of code on one line of text.

No, it's two statements. It's definitely one line of code.

Now this:

b += 3, x = a + b;

is only one statement and one line of code.