Re: returning lvalue in C vs C++
- From: Andrey Tarasevich <andreytarasevich@xxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 16:28:50 -0800
Alf P. Steinbach wrote:
...
It's invalid in C++ too.
Comeau Online reports:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest.c", line 22: error: expression must be a modifiable lvalue
f().x = 200; /* "invalid lvalue" in C, but not in C++
apparently */
^
1 error detected in the compilation of "ComeauTest.c".
However, the 1998 standard isn't very clear. ╖5.2.5/4 second hyphen
tells us that in the expression E1.E2, "If E1 is an lvalue, then E1.E2
is an lvalue.". But nothing is said of the case where E1 is not an
lvalue. One must simply /infer/ that when E1 is not an lvalue, E1.E2 is
not an lvalue, either. I don't think was clarified in 2003.
The footnote 31 in 6.2.2.1 in C89/90 seems to imply that in C any "value
of an expression" is assumed to be an "rvalue" (i.e. "non-lvalue")
unless stated otherwise. However, I agree that the standard is not
sufficiently clear in tis case.
BTW, the difference between C89/90 and C++ can still be observed with
Comeau compiler as well (although that is a different difference :)
struct S { int a[10]; };
struct S foo();
int main() {
foo().a[5];
}
In C89/90 mode the code will not compile, since the compiler does not
consider 'foo().a' to be an lvalue, and in C89/90 array-to-pointer
conversion can only be applied to lvalue arrays.
In C++ and C99 array-to-pointer conversion is allowed for non-lvalue
arrays, which makes the above code to compile in these modes.
--
Best regards,
Andrey Tarasevich
.
- References:
- returning lvalue in C vs C++
- From: Ben C
- Re: returning lvalue in C vs C++
- From: Alf P. Steinbach
- returning lvalue in C vs C++
- Prev by Date: Re: Subtract smaller or larger number from x?
- Next by Date: Re: returning lvalue in C vs C++
- Previous by thread: Re: returning lvalue in C vs C++
- Index(es):
Relevant Pages
|