Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2002-06-23 10:52:25


Aloha,

I've been using the Pimpl idiom on the last semester using a
boost::shared_ptr<> to
hold the pimpl. A thing that irritated me was that constness of a routine
did not
propagate to the pimpl because boost::shared_ptr's operator->() returns a
T*:

template<typename T> class shared_ptr {
...
T * operator->() const; // never throws
};

This will allow this code to compile although (IMO) it is not supposed to:

struct Int
{
    int i;
    void foo() { i++; }
};

class C
{
    boost::shared_ptr<Int> p_;
public:
    C() : p_( new Int ) { }
    void can_modify() { p_->foo(); }
    void can_also() const { p_->foo(); } // breaks constness contract
};

Am I right that this is undesired behavior? If so, this extension to
the smart pointers should be done:

template<typename T> class shared_ptr {
...
const T * operator->() const; // disallows a call to non-const functions
via
return value
T* operator->();
};

and also for operator*().

regards

--Thorsten
nesotto_at_[hidden]


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