Boost logo

Boost Users :

Subject: Re: [Boost-users] Help with custom smart pointer const correctness
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2009-04-15 17:56:30


On Wed, Apr 15, 2009 at 4:16 PM, Dominique Devienne <ddevienne_at_[hidden]> wrote:
> From my very limited meta-programming experience, I'm guessing I need
> to fix the current typedef shared_ptr<ident_type> ident_ptr; in
> ObjectPtr to somehow use MPL to "return" shared_ptr<typename
> T::ident_type const> given a const T, and shared_ptr<typename
> T::ident_type> given a T, but I don't know how to do that yet.
>
> If someone would be kind enough to point me in the right direction,
> I'd appreciate. Thanks, --DD

I figured the following which seems to work fine.
Didn't think I could do it on my own in 15min, thus my post ;) --DD

#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/mpl/if.hpp>

template <class T> class ObjectPtr {
public:
    typedef typename remove_const<T>::type object_type;
    typedef typename object_type::ident_type ident_type;
    typedef shared_ptr<
        typename mpl::if_c<is_const<T>::value, ident_type const,
ident_type>::type
> ident_ptr;

public:
    ident_ptr ident() const { return ident_; }

private:
    ident_ptr ident_;
};


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