Boost logo

Boost :

Subject: [boost] [ptr_container] Can't store a pointer to const in multimap?
From: Amit Kumar (in4tunio_at_[hidden])
Date: 2009-06-15 13:33:27


Please consider the following code:

#include "boost/ptr_container/ptr_map.hpp"

int main()
{
        typedef boost::ptr_multimap<char, const int> M;

        M m;
        char c = 'c';

        int* j = new int(7);

        m.insert(c, j);

        return 0;
}

The gcc 4.3.3 compiler fails to compiler the above code. Am I doing
something wrong by inserting a non-const? Can't I store a pointer to const
in the multimap?

try.cpp: In function ‘int main()’:
try.cpp:8: error: expected primary-expression before ‘=’ token
/usr/include/c++/4.3/bits/stl_pair.h: In constructor ‘std::pair<_T1,
_T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = char, _U2 = const
int*, _T1 = const char, _T2 = void*]’:

boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:765:
instantiated from ‘typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T,
VoidPtrMultiMap, CloneAllocator, Ordered>::insert_impl(const typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::key_type&, typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::mapped_type) [with T = const int,
VoidPtrMultiMap = std::multimap<char, void*, std::less<char>,
std::allocator<std::pair<const char, void*> > >, CloneAllocator =
boost::heap_clone_allocator, bool Ordered = true]’

boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:799:
instantiated from ‘typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T,
VoidPtrMultiMap, CloneAllocator, Ordered>::insert(typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::key_type&, typename
boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::mapped_type) [with T = const int,
VoidPtrMultiMap = std::multimap<char, void*, std::less<char>,
std::allocator<std::pair<const char, void*> > >, CloneAllocator =
boost::heap_clone_allocator, bool Ordered = true]’
try.cpp:9: instantiated from here
/usr/include/c++/4.3/bits/stl_pair.h:106: error: invalid conversion
from ‘const void*’ to ‘void*’

I also tried but failed with const casting j before insertion.


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