Re: Strange warning from g++ "returning reference to temporary"
blargg wrote:
Bo Persson wrote:
The const version returns a different type, and so possibly
involves a conversion - creating a temporary.
[...]
I'm confused, since as far as I understand, returning a pointer
rather than reference WOULD work (4.4 para 4), without creating any
temporaries:
struct Foo
{
char* s;
operator const char* const*() const { return &s; } // OK, right?
operator const char* const&() const { return s; } // why not OK?
};
If the reference version really is invalid (and the pointer version
not),
then this is one glaring difference between references and pointers
where
one would expect them to behave the same.
Well, references and pointers are different. :-)
In the pointer case, you actually return the pointer BY VALUE. That
way you get a temporary anyway, but it is copied, so it doesn't matter.
The reference version returns a reference to a temporary that
immediately goes out of scope. That's serious!
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]