tags:

views:

67

answers:

1
void main()
{
  typedef boost::ptr_map<int, char>  MyMap;
  //typedef std::map<int, char *>  MyMap;  // in contrast with std type it works

  MyMap mymap;

  mymap[1] = new char('a');
  mymap[2] = new char('b');
  mymap[3] = new char('c');

  BOOST_FOREACH(MyMap::value_type value, mymap)
  {
    std::cout << value.first << "  " << value.second << std::endl;
  }

  MyMap const & const_mymap = mymap;

  BOOST_FOREACH(const MyMap::value_type value, const_mymap)
  {
    std::cout << value.first << "  " << value.second << std::endl;
  }
}

The following error message comes from GCC at the second BOOST_FOREACH

error: conversion from 'boost::ptr_container_detail::ref_pair<int, const char* const>' to non-scalar type 'boost::ptr_container_detail::ref_pair<int, char* const>' requested

I reckon that this is the weakness of the pointer container's ref_pair...

+3  A: 

Based on this answer, it looks like you're right. But there's a workaround. Change your second loop to this:

BOOST_FOREACH(MyMap::const_iterator::value_type value, const_mymap)
{
    std::cout << value.first << "  " << value.second << std::endl;
}
Kristo