Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2002-06-24 10:38:41


The behavior is as desired. The non-constness is a side effect of the pImpl
idiom; consider if you just had a raw pointer instead of a smart_ptr.

The new SmartPtr may be able to provide this const-restriction as a policy.

This question has been brought up several times; I think it should be added
to the FAQ section on shared_ptr. And while we're dealing with
documentation, I think links should be made from the Boost FAQ to the
various library FAQs (or at least an entry stating that the individual
library documentation may have additional FAQs for that library).

        -Steve

> -----Original Message-----
> From: Thorsten Ottosen [mailto:nesotto_at_[hidden]]
> Sent: Sunday, June 23, 2002 11:52 AM
> To: boost_at_[hidden]
> Subject: [boost] bug: constness of smart pointers
>
>
> 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]
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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