This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Bug or no bug? gcc 2.95: Q: initializing non-const .. will use a



> 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.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]