Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2005-04-28 16:22:40


On Apr 28, 2005, at 3:10 PM, Peter Dimov wrote:

> Howard Hinnant wrote:
>> I've got 3 ownership policies on my mind:
>>
>> 1. shared
>> 2. unique
>> 3. cloned
>>
>> And if anything I'm leaning towards shallow const on all of them.
>> However I also believe deep const versions of all of these would
>> probably be valuable as well. And I'm especially looking at
>> signatures like:
>>
>> template<class T, class U>
>> bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b);
>>
>> shared_ptr today is a shallow const animal, and I think that is good.
>> But the above signature assumes deep const. It would be nice to have
>> a zero-overhead way to express and enforce that assumption.
>
> I'm not sure I understand. Why does the above assume deep const? It's
> just the shared_ptr equivalent of:
>
> template<class T, class U>
> bool operator==(T * const & a, U * const & b);

I didn't state myself clearly. I meant that the semantics of
operator== on shared_ptr (regardless of the actual signature) imply
that not only will the shared_ptr remain unchanged, but also what it
points to. I.e. the implied semantics of operator== is:

template<class T, class U>
bool operator==(const T * const & a, const U * const & b);

But the actual signature that we use is the one you wrote (with
non-const pointee).

It would be nice if we had a zero-overhead way to express deep const on
the operator== signature.

With shared_ptr we could:

template<class T, class U>
bool operator==(const shared_ptr<const T>& a, const shared_ptr<const
U>& b);

and I believe it would compile and work as expected, and now deep const
semantics are guaranteed by the complier. However the above isn't
zero-overhead (shared_ptr<T> will convert to shared_ptr<const T> for
the operation), and thus is unacceptable. And the same idea flat
wouldn't compile for unique_ptr.

If we had a deep_const smart pointer adaptor that adapted either smart
pointers, or references to smart pointers, and had a non-explicit
converting constructor, then the following might work (haven't actually
tried it) with zero overhead:

template<class T, class U>
bool operator==(const deep_const<const shared_ptr<T>&&>& a, const
deep_const<const shared_ptr<U>&&>& b);

But that's getting pretty darned ugly and I immediately wonder if the
cure isn't worse than the disease. :-)

-Howard


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