This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Different behavior between temporary and declared object
- To: Koos Vriezen <koos at polder dot ubc dot kun dot nl>
- Subject: Re: Different behavior between temporary and declared object
- From: Alexandre Oliva <oliva at dcc dot unicamp dot br>
- Date: 04 Jun 1999 16:42:22 -0300
- Cc: egcs at egcs dot cygnus dot com
- References: <Pine.NEB.4.05.9906041436470.18376-100000@klei.intern.polderland.nl>
On Jun 4, 1999, Koos Vriezen <koos@polder.ubc.kun.nl> wrote:
> C (B());
> warning: initialization of non-const reference `class A &' from rvalue `B'
> warning: in passing argument 1 of `C::C(A &)'
That's correct. According to the C++ Standard, you can't bind a
non-const reference to a temporary.
> B b;
> C (b);
> conflicting types for `class C b'
> previous declaration as `class B b'
Also correct. The C++ grammar is ambiguous and the Standard mandates
that `C (b)' be parsed as a declaration (and definition with
default-initialization) of a `b', whose type is `C', not as an
expression that initializes a temporary of type C from `b'. You can
make it an expression by prepending `(void)'.
> I agree with the compiler that there is an error, but isn't the
> first case also wrong?
Both are, but the first case is accepted as an extension. If you
compile with -ansi -pedantic, you'll get an error.
--
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Bra[sz]il
{oliva,Alexandre.Oliva}@dcc.unicamp.br aoliva@{acm.org,computer.org}
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
*** E-mail about software projects will be forwarded to mailing lists