Re: returning lvalue in C vs C++



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
.



Relevant Pages

  • Re: object system...
    ... So a C compiler that does that sort of optimization is not a valid C compiler because the language semantics are being violated. ... Note that even if one writes a very smart debugger that looks at the register, the language semantics is still being broken because the lvalue memory location is not being updated as the instructions are executed. ... optimizing an lvalue into a register violates the spec in a manner that is usually not a problem so that optimization is deemed justified. ...
    (comp.object)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... An 'lvalue' is *not* any kind of value, ... function it allocates more memory and copies the literal to the new ... might keep a copy of the pointer, and store that pointer other ... and the C compiler will already forbid you ...
    (comp.lang.c)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... You consider a compiler to be stupid for following the language ... How can an array be an lvalue, if you can't assign to it? ... side of an assignment. ...
    (comp.lang.c)
  • Re: Rvalue of struct type
    ... "Lvalue required". ... array type returned by a function is converted from Rvalue to Lvalue! ... struct S s; ... (The compiler I'm trying right now ...
    (comp.lang.c)
  • Re: +++i
    ... +i doesn't yield an lvalue, which gives us a constraint violation. ... And where is the order relevant here? ... A correct C compiler can do neither of these. ... Practical Solution #1: ...
    (comp.lang.c)