* Strange warning when compiling with cxx 6.2
@ 2000-05-18 4:00 Valentin Koch
2000-05-18 4:00 ` Daniel S. Riley
0 siblings, 1 reply; 2+ messages in thread
From: Valentin Koch @ 2000-05-18 4:00 UTC (permalink / raw)
Hello,
has anyone an idea what the following warning means:
cxx: Warning: Test.cxx, line 250: initial value of reference to
non-const must be an lvalue
myResult = CSingleton::instance().myFunc();
-----------^
Following what happens in myFunc:
Result CSingleton::myFunc() {
Result theResult;
...
return theResult;
};
struct Result {
auto_ptr<AStruct> myStruct;
string myString;
};
And in Test.cxx:
Result myResult;
if(...) {
myResult = CSingleton::instance().myFunc();
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Strange warning when compiling with cxx 6.2
2000-05-18 4:00 Strange warning when compiling with cxx 6.2 Valentin Koch
@ 2000-05-18 4:00 ` Daniel S. Riley
0 siblings, 0 replies; 2+ messages in thread
From: Daniel S. Riley @ 2000-05-18 4:00 UTC (permalink / raw)
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"
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-05-18 4:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-05-18 4:00 Strange warning when compiling with cxx 6.2 Valentin Koch
2000-05-18 4:00 ` Daniel S. Riley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox