$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: David B. Held (dheld_at_[hidden])
Date: 2002-09-05 13:59:41
"Anthony Williams" <anthwil_at_[hidden]> wrote in message
news:15733.50724.159000.285126_at_gargle.gargle.HOWL...
> [...]
> I can see the point, but this is undefined behaviour --- you are returning
> a reference to a temporary, which as gone out of scope.
Good point.  So we can just drop in a default singleton, which saves
us a construction anyway, and make the behaviour defined. ;)
    // Requirements:
    //   Ptr must define element_type
    //   Ptr must provide an implicit conversion to bool
    //   Ptr::element_type must be default-constructible
    template <class Ptr>
    typename Ptr::element_type const& safe_dereference(Ptr p)
    {
        typedef typename Ptr::element_type element_type;
        static element_type default_element;
        return p ? *p : default_element;
    }
    // Requirements:
    //   T must be default-constructible
    template <typename T>
    T const& safe_dereference(T* p)
    {
        static T default_T;
        return p ? *p : default_T;
    }
Dave