Boost logo

Boost :

From: Hillel Y. Sims (hsims_at_[hidden])
Date: 2002-09-04 11:11:28


"Anthony Williams" <anthwil_at_[hidden]> wrote in message
news:15733.50724.159000.285126_at_gargle.gargle.HOWL...
> David B. Held writes:
> > // Requirements:
> > // T must be default-constructible
> > template <typename T>
> > T const& safe_dereference(T* p)
> > {
> > return p ? *p : T();
> > }
> >
>
> I can see the point, but this is undefined behaviour --- you are returning
a
> reference to a temporary, which as gone out of scope.
>

Why is it ok to say

   int main()
   {
       const string& s = func();
       cout << s << endl;
   }

when func is defined as
   string func() { return string("hello"); }

but then not ok if func is
   const string& func() { return string("hello"); } ?

Is it not possible for a compiler to bind the temporary directly to the
const & in both cases?

hys

--
Hillel Y. Sims
FactSet Research Systems
hsims AT factset.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk