Opened 15 years ago
Closed 15 years ago
#1422 closed Bugs (invalid)
const_iterator does not equate to value_type (or const value_type)
Reported by: | Owned by: | Thorsten Ottosen | |
---|---|---|---|
Milestone: | To Be Determined | Component: | ptr_container |
Version: | Boost 1.34.1 | Severity: | Problem |
Keywords: | ptr_map, ptr_container, const_iterator | Cc: |
Description
In ptr_map, const_iterator does not represent value_type. Given this map:
typedef boost::ptr_map<DWORD, int> BOOSTMAP;
BOOSTMAP::value_type equates to:
boost::ptr_container_detail::ref_pair<DWORD, int* const>
BOOSTMAP::iterator does return this, but const_iterator returns:
boost::ptr_container_detail::ref_pair<DWORD, const int* const> which is unlike std::map which represents const value_type for const_iterator. Since this is a different structure, it's incompatible with BOOSTMAP::value_type.
The following code works using std::map:
typedef std::map<DWORD, int*> STDMAP; int sample = 5; STDMAP sm; sm.insert( STDMAP::value_type(10,&sample) ); STDMAP::iterator sfirst = sm.begin(); STDMAP::const_iterator scfirst = sm.begin(); STDMAP::value_type& svt = *sfirst; const STDMAP::value_type& scvt = *scfirst;
But, the same fails on the last line with boost::ptr_map
typedef boost::ptr_map<DWORD, int> BOOSTMAP; BOOSTMAP bm; DWORD key = 10; bm.insert( key, new int(5) ); BOOSTMAP::iterator bfirst = bm.begin(); BOOSTMAP::const_iterator bcfirst = bm.begin(); BOOSTMAP::value_type& bvt = *bfirst; const BOOSTMAP::value_type& bcvt = *bcfirst;
If you replace the last line with
BOOST_MAP::const_reference bcvt = *bcfirst;
it should work.
Since a major design goal was to propagate constness, your line can never be made to work, unfortunately.