473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Nov 17 '05 #1
5 919
"songie D" <an*******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.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)".
Nov 17 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.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

Nov 17 '05 #3
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" <Ja*********@mvps.org> wrote in message
news:Om**************@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" <an*******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.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


Nov 17 '05 #4
"songie D" <so****@D.com> wrote in message
news:uA**************@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.
Nov 17 '05 #5

"Simon Trew" <ten.enagro@werts> wrote in message
news:OH**************@tk2msftngp13.phx.gbl...
"songie D" <so****@D.com> wrote in message
news:uA**************@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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Martin DeMello | last post by:
It seems to be a fairly common pattern for an object-modifying method to return None - however, this is often quite inconvenient. For instance def f(lst1, lst2): g((lst1 + lst2).reverse()) #...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
7
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.