Boost logo

Boost :

Subject: [boost] [Pointer Container] Modification of pointer containers to allow const elements
From: Gerardo Hernández (g.hernandez_at_[hidden])
Date: 2010-10-14 19:13:35


The following code snippet:

boost::ptr_vector<const int> v;
v.push_back(new int);

produces the following error message (Visual C++ 9, boost 1.44):

boost\ptr_container\ptr_sequence_adapter.hpp(249) : error C2664:
'std::vector<_Ty,_Ax>::push_back' : cannot convert parameter 1 from
'const int *' to 'void *const &'

The problem is that the underlying type of boost::ptr_vector<T> is
always vector<void*>, no matter T is const. This prevents push_back()
method from maintaining const-correctness. The rest of the pointer
containers contain an equivalent problem and this has been a TODO for
some time.

According to Thorsten Ottosen (library author) (
https://svn.boost.org/trac/boost/ticket/3832 ):
"The fix is basically just some meta-functions taking into account
that T might be const, and if so, instantiate container<const void*>
instead of container<void*>"

To the vector problem, this means that the underlying type should be:

vector<typename boost::mpl::if_<boost::is_const<T>, const void*, void*> >

I have sucessfully applied this approach to the pointer containers
that mimic STL counterparts (see attachment) and I guess it should
work for the others.

I need feedback on whether this is a completely correct approach and
if so I would like to contribute a modification.

Gerardo.




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