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: Different behavior between temporary and declared object


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


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