Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: dsr@mail.lns.cornell.edu (Daniel S. Riley)
Subject: Re: Strange warning when compiling with cxx 6.2
Date: 2000/05/18	[thread overview]
Message-ID: <shsnvf6ho6.fsf@lns130.lns.cornell.edu> (raw)
In-Reply-To: 3923E84C.10DDF60A@nagra.com

Valentin Koch <koch@nagra.com> writes:
> cxx: Warning: Test.cxx, line 250: initial value of reference to
>           non-const must be an lvalue
> myResult = CSingleton::instance().myFunc();
> -----------^

There's some deep conversion magic in auto_ptr to allow it to be used
as a function argument or return type.  As I understand it (which
isn't all that well), the copy constructor in the version of auto_ptr
in the final standard does not work for rvalues (this changed between
the last public review draft and the final standard, so it only shows
up in relatively recent compilers).  This is ok when the return type
is an auto_ptr because things were carefully arranged so that the
compiler is not allowed to create any temporaries.  In your code,
returning by value a struct that contains an auto_ptr, the compiler
has to create a temporary, that temporary is an rvalue, and that means
it can't do the assignment because you can't copy an rvalue auto_ptr.

If you compile with '-std strict_ansi', instead of a warning you'll
get an error:

cxx: Error: foo.cc, line 21: no operator "=" matches these operands
            operand types are: Result = Result

and g++ 2.95.2 gives the error:

foo.cc:21: initialization of non-const reference type `struct Result &'
foo.cc:21: from rvalue of type `Result'
foo.cc:9: in passing argument 1 of `Result::operator =(Result &)'

AFAIK, these compilers are right (and Sun Workshop 5.0, which happily
compiles it without warning, is wrong), but you might get a more
convincing answer on on comp.lang.c++.moderated or comp.std.c++, where
auto_ptr intricacies are a recurring topic.

-- 
Dan Riley                                         dsr@mail.lns.cornell.edu
Wilson Lab, Cornell University      <URL:http://www.lns.cornell.edu/~dsr/>
    "History teaches us that days like this are best spent in bed"



      reply	other threads:[~2000-05-18  4:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-18  4:00 Strange warning when compiling with cxx 6.2 Valentin Koch
2000-05-18  4:00 ` Daniel S. Riley [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox