Subject: Re: [boost] [Pointer Container] Modification of pointer containers to allow const elements
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2010-10-15 14:16:34
On 15/10/10 10:41, Gerardo Hernández wrote:
> Scott McMurray <me22.ca+boost_at_[hidden]> wrote:
>>> The problem is that the underlying type of boost::ptr_vector<T> is
>>> always vector<void*>, no matter T is const.
>> Since the point of this is to only ever instantiate one version of the
>> underlying container, wouldn't it be better to always use
>> underlying_container<void const*>, then const_cast when returning to
>> the user?
> My point was to allow ptr containers of <const T> to work, maintaining
> const-correctness. I believe that requires different underlying
> container types and not only one as you suggest. So in this code
> boost::ptr_vector<const int> v;
> v.push_back(new int);
> v = 3;
> the assignment shall produce a compiler error as v would be const,
> to maintain const-correctness.
> With the underlying type defined as mpl::if_<boost::is_const<T>, const
> void*, void*>::type this is accomplished. But using void* const always
> as the underlying type and using const_cast during returning (and
> inserting) elements, would not const-correctness be lost?
I presume that Scott only intended to suggest abandoning
const-correctness in the implementation, not the interface. Thus the
above assignment would still cause an error. Since type safety has
already been abandoned by making all pointers void*, const-correctness
doesn't seem much of an additional sacrifice.