473,388 Members | 1,327 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,388 software developers and data experts.

Do parens cause problems when returning references?


Given

class C {
int m;
public:
int& func() { return m; }
// other elements redacted for brevity
};

Other than the fact that functions that return a handle are bad, is
there any difference between the given C::func and

int& C::func() { return (m); }

I have some third party code that uses this, and the people in question
are reluctant to change it. Comeau online doesn't complain, but I have
doubts about whether (m) is an lvalue that's returnable.
May 17 '06 #1
7 1241
In article <bk*******************@newssvr12.news.prodigy.com> ,
red floyd <no*****@here.dude> wrote:
Given

class C {
int m;
public:
int& func() { return m; }
// other elements redacted for brevity
};

Other than the fact that functions that return a handle are bad, is
there any difference between the given C::func and

int& C::func() { return (m); }

I have some third party code that uses this, and the people in question
are reluctant to change it. Comeau online doesn't complain, but I have
doubts about whether (m) is an lvalue that's returnable.


The ()'s are just for grouping and are essentially no-ops in this case.
(m) is a perfectly legal lvalue:

main()
{
int m;

(m) = 4;
}

compiles fine.
May 17 '06 #2
Roy Smith wrote:
[..]
main()
{
int m;

(m) = 4;
}

compiles fine.


It shouldn't. Your 'main' has no return value type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #3
In article <e4**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.net> wrote:
Roy Smith wrote:
[..]
main()
{
int m;

(m) = 4;
}

compiles fine.


It shouldn't. Your 'main' has no return value type.

V


You're nit-picking. It does indeed compile on my box (Mac OSX, gcc 3.3),
and in any case, that has nothing to do with whether (m) is a legal lvalue
or not.
May 17 '06 #4
Roy Smith wrote:
In article <e4**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.net> wrote:
Roy Smith wrote:
[..]
main()
{
int m;

(m) = 4;
}

compiles fine.
It shouldn't. Your 'main' has no return value type.

V


You're nit-picking.


Yes. So? Post nit-free code and there will be no need for that.
It does indeed compile on my box (Mac OSX, gcc
3.3), and in any case, that has nothing to do with whether (m) is a
legal lvalue or not.


How do you know if you haven't disabled extensions in your compiler?
Besides, even if it compiles on one compiler, it doesn't necessarily
mean the code is fine. Portability is the name of the game here,
and one compiler cannot be a true measure of that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #5
In article <e4**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.net> wrote:
Yes. So? Post nit-free code and there will be no need for that.
It does indeed compile on my box (Mac OSX, gcc
3.3), and in any case, that has nothing to do with whether (m) is a
legal lvalue or not.


How do you know if you haven't disabled extensions in your compiler?
Besides, even if it compiles on one compiler, it doesn't necessarily
mean the code is fine. Portability is the name of the game here,
and one compiler cannot be a true measure of that.


Sigh. I was just using that code as an example. I know (m) is an lvalue
from experience, but since you're being insistent, I went and looked it up.

Section 5.1, paragraph 5 of INTERNATIONAL STANDARD ISO/IEC 14882 First
edition 1998-09-01 Programming languages C++, says:

"A parenthesized expression is a primary expression whose type and value
are identical to those of the enclosed expression. The presence of
parentheses does not affect whether the expression is an lvalue. The
parenthesized expression can be used in exactly the same contexts as those
where the enclosed expression can be used, and with the same meaning,
except as otherwise indicated."

Section 3.6.1, paragraph 2 of that same document does indeed say that main,
"shall have a return type of type int", so yes, the code I posted is not
correct in that respect, but the primary point of my post was to illustrate
that (m) is indeed an lvalue.
May 17 '06 #6
Roy Smith wrote:
[..]
Sigh. I was just using that code as an example. I know (m) is an
lvalue from experience, but since you're being insistent, I went and
looked it up.

Section 5.1, paragraph 5 [..]


Roy,

That's a totally different way of presenting your valuable information!

I am absolutely sure that "red floyd" will be much happier with this in
his arsenal than something like "yeah, don' worry 'boutit", especially
if somebody asks, "how do you know it's OK to drop them parens?"

Best!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #7
Roy Smith wrote:
Section 5.1, paragraph 5 of INTERNATIONAL STANDARD ISO/IEC 14882 First
edition 1998-09-01 Programming languages C++, says:

"A parenthesized expression is a primary expression whose type and value
are identical to those of the enclosed expression. The presence of
parentheses does not affect whether the expression is an lvalue. The
parenthesized expression can be used in exactly the same contexts as those
where the enclosed expression can be used, and with the same meaning,
except as otherwise indicated."

Thank you. I was looking for 5.1/5.
May 17 '06 #8

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

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
4
by: Asfand Yar Qazi | last post by:
Sorry about this, but its driving me up the wall. First some code: typedef unsigned int size_t; struct AddOp { template<class I1, class I2> static inline float call(size_t i, const I1&...
6
by: Matthias Kaeppler | last post by:
Hello, during a discussion on a C++ internet forum, some question came up regarding references and the lifetime of the objects they alias. I can't find any clear wording on that in the draft...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
21
by: Doug Lerner | last post by:
I'm working on a client/server app that seems to work fine in OS Firefox and Windows IE and Firefox. However, in OS X Safari, although the UI/communications themselves work fine, if the...
11
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
4
by: jasonnerothin | last post by:
I'm trying to figure out exactly what's going wrong with an xpath keyref validation scenario. In brief, I have something like the following in my xsd: <xs:schema> <xs:complexType...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.