From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,31d5d62cd1d0755 X-Google-Attributes: gid1164f8,public X-Google-Thread: fee84,31d5d62cd1d0755 X-Google-Attributes: gidfee84,public From: dsr@mail.lns.cornell.edu (Daniel S. Riley) Subject: Re: Strange warning when compiling with cxx 6.2 Date: 2000/05/18 Message-ID: #1/1 X-Deja-AN: 625005354 References: <3923E84C.10DDF60A@nagra.com> Organization: LNS, Cornell U., Ithaca, NY 14853 Newsgroups: comp.sys.dec,comp.unix.osf.osf1 Valentin Koch 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 "History teaches us that days like this are best spent in bed"