Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2004-01-21 14:16:33


On 1/19/04 9:36 AM, "Daniel Wallin" <dalwan01_at_[hidden]> wrote:

> Daryle Walker wrote:
>> If you want a pointer whose "const" state matches that of the pointer-to
>> object, you can make you own smart pointer for it. Just like using an
>> "auto_ptr" as a class member to prevent forgetting of pointer-based deletion,
>> this new smart pointer will allow a pointed-to (owned) object to have the
>> same "const"-ness of the current member function, instead of having no
>> qualifications (and be changed if the programmer accidentally uses the object
>> in a non-const manner). Post it to Boost for inclusion when you're done!
>
> FWIW, that is exactly what I did and what this thread was about from the
> start. ;)

Was it something like:

template < typename T >
class const_matching_ptr
    : private boost::noncopyable
{
public:
    explicit const_matching_ptr( T &x ) : x_( &x ) {}

    T * get() { return this->x_; }
    T const * get() const { return this->x_; }
    T volatile * get() volatile { return this->x_; }
    T const volatile * get() const volatile { return this->x_; }

    // Don't forget to do all the qualification combinations!
    // (Should we add const/volatile qualifications as a new
    // kind of template parameter? Look at how similar the
    // "get" methods were. This will get worse if we use
    // C-1999's "restrict".)
    T * operator ->();
    T & operator *();
    T & operator []( std::size_t i );

private:
    T * x_;
};

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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