public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Address of a cast expression in C++
@ 2002-07-25 13:47 Matt Austern
  2002-07-25 14:30 ` Gabriel Dos Reis
  0 siblings, 1 reply; 4+ messages in thread
From: Matt Austern @ 2002-07-25 13:47 UTC (permalink / raw)
  To: gcc

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?

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?

			--Matt

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Address of a cast expression in C++
  2002-07-25 13:47 Address of a cast expression in C++ Matt Austern
@ 2002-07-25 14:30 ` Gabriel Dos Reis
  2002-08-06 16:27   ` Matt Austern
  0 siblings, 1 reply; 4+ messages in thread
From: Gabriel Dos Reis @ 2002-07-25 14:30 UTC (permalink / raw)
  To: Matt Austern; +Cc: gcc

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Address of a cast expression in C++
  2002-07-25 14:30 ` Gabriel Dos Reis
@ 2002-08-06 16:27   ` Matt Austern
  2002-08-06 16:37     ` Gabriel Dos Reis
  0 siblings, 1 reply; 4+ messages in thread
From: Matt Austern @ 2002-08-06 16:27 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc

On Thursday, July 25, 2002, at 11:38 AM, Gabriel Dos Reis wrote:

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Address of a cast expression in C++
  2002-08-06 16:27   ` Matt Austern
@ 2002-08-06 16:37     ` Gabriel Dos Reis
  0 siblings, 0 replies; 4+ messages in thread
From: Gabriel Dos Reis @ 2002-08-06 16:37 UTC (permalink / raw)
  To: Matt Austern; +Cc: gcc

Matt Austern <austern@apple.com> writes:

| On Thursday, July 25, 2002, at 11:38 AM, Gabriel Dos Reis wrote:
| 
| > | 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++ :-(
| 
| You're right that code duplication is evil, but the problem here
| turns out to be different from what we thought.

Thanks for the update.  Next time, I'll look much harder :-) :-)

-- Gaby

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-08-06 16:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-25 13:47 Address of a cast expression in C++ Matt Austern
2002-07-25 14:30 ` Gabriel Dos Reis
2002-08-06 16:27   ` Matt Austern
2002-08-06 16:37     ` Gabriel Dos Reis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).