Do parens cause problems when returning references?

R

red floyd

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.
 
R

Roy Smith

red floyd said:
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.
 
R

Roy Smith

"Victor Bazarov said:
Roy said:
[..]
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.
 
V

Victor Bazarov

Roy said:
Victor Bazarov said:
Roy said:
[..]
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
 
R

Roy Smith

"Victor Bazarov said:
Yes. So? Post nit-free code and there will be no need for that.


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.
 
V

Victor Bazarov

Roy said:
[..]
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
 
R

red floyd

Roy said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top