Address of a cast expression in C++
Gabriel Dos Reis
gdr@integrable-solutions.net
Thu Jul 25 14:30:00 GMT 2002
Matt Austern <austern@apple.com> writes:
| With both 3.1 and TOT, the following program is rejected by
| the C front end but accepted by the C++ front end. g++
| compiles it without error or warning.
| #include <stdio.h>
|
| int main() {
| int n = 0;
| char* p = &(char)n;
| *p = 0x7f;
| printf("%x\n", n);
| }
|
| As I read the C++ Standard, this is incorrect; a diagnostic is
| required. 5.4/1 says that the result of (char)n is an rvalue,
| and 5.3.1/2 says that you can't take the address of an rvalue.
|
| Question: is this violation of the C++ Standard deliberate,
| or is it a bug?
I would say it is a result of a bug -- I suspect another instance of
code duplication where the problem is fixed in C but not in C++ :-(
Indeed, the C++ front-end's copy (in typeck.c) of the C front-end's
build_unary_op() (in c-typeck.c) doesn't not handle the case you
spotted. Yet another instance of "code duplication is evil".
| The reason I ask, of course, is that gcc has a generalized
| lvalue extension. The manual says that even with this
| extension it's illegal to write &(int)f. Who's wrong: the manual,
| or the compiler?
The compiler is buggy.
-- Gaby
More information about the Gcc
mailing list