Boost logo

Boost Users :

Subject: Re: [Boost-users] Covariant return type, method hiding, and shared_ptr advice
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2009-04-15 16:08:34


On Wed, Apr 15, 2009 at 12:25 PM, Steven Watanabe <watanabesj_at_[hidden]> wrote:
> Dominique Devienne wrote:
>> So I used covariant return type, which AFAIK works only for raw
>> pointers. If I were to remove the raw pointer accessors, I'd have to
>> do either:
>
> I would probably use a non-member template that
> encapsulates the necessary logic:
>
> template<class T>
> shared_ptr<typename T::IdentType> getIdent(T& t);

I have:

template <class T> shared_ptr<typename T::ident_type> ident_of(T* t) {
    BOOST_STATIC_ASSERT((is_base_of<Object, T>::value));
    return static_pointer_cast<typename T::ident_type>(t->ident());
}
template <class T> shared_ptr<typename T::ident_type const>
ident_of(const T* t) {
    BOOST_STATIC_ASSERT((is_base_of<Object, T>::value));
    return static_pointer_cast<typename T::ident_type const>(t->ident());
}

but it chokes on:

    scoped_ptr<Car> z(new Car("nissan", "370Z"));
    ident_of(z)->set_make("datsun");

error C2784: 'boost::shared_ptr<T::ident_type> ident_of(T *)' :
could not deduce template argument for 'T *' from 'boost::scoped_ptr<T>'

Instead of doing overloads for various smart pointer types, is it
possible (and easy) to do the ChainedPtr thing that B.MI does for the
ident_of template functions above?

Thanks, --DD


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net