This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Bug or no bug? gcc 2.95: Q: initializing non-const .. will use a
- To: koen at easics dot be (Koen Deforche)
- Subject: Re: Bug or no bug? gcc 2.95: Q: initializing non-const .. will use a
- From: Joe Buck <jbuck at synopsys dot COM>
- Date: Tue, 5 Oct 99 9:12:27 PDT
- Cc: gcc at gcc dot gnu dot org
> I have a question about whether this is or is not correct ansi C++.
> What the code does, is passing a Foo object to generic_procedure
> that accepts a non-const Bar.
This is not allowed in C++. A non-const reference to Bar must
refer to an lvalue of type Bar (or of a class derived from Bar).
No exceptions.
> This code does not compile using gcc 2.95, unless I use the
> -fpermissive flag.
>
> The error (or with -fpermissive, warning) I get is:
> pass.cc: In function `int main()':
> pass.cc:40: initializing non-const `Bar &' with `Foo' will use a
> temporary
> pass.cc:30: in passing argument 1 of `generic_procedure(Bar &)'
-fpermissive gives you the traditional cfront behavior, which was to copy
the Foo to a temporary of type Bar, and assign the reference to the temporary
(cfront was the first C++ compiler). But that was a hack which was almost
never the right thing, so the construct was just banned. That's not a new
change, the ARM bans this kind of thing and it was written in '92 or so.
> To come back to my question: does the standard specifically
> state that this is invalid C++ code ...
Yes, it is invalid.