0

In my class I've got such soundmap

class SoundSubSystem
{
private:
    boost::unordered_map<string, ISoundEngine*> soundmap;
....
};

But how to iterate through it using BOOST_FOREACH?

SoundSubSystem::~SoundSubSystem()
{
    BOOST_FOREACH(/*?*/ item, soundmap) {
        item.second->drop();
    }
};

What value type should item have?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
lapots
  • 12,553
  • 32
  • 121
  • 242
  • Just as an advice, consider upgrading your compiler to a version that offers C++11 support, and use a range-based `for` loop: `for (auto& item : soundmap) { item.second->drop(); }`. – Andy Prowl Jan 18 '14 at 16:04
  • I'm using visual studio 2010 - as far as I know it does not support c++11 – lapots Jan 18 '14 at 16:09
  • 1
    Indeed it does not support C++11's range-based `for` loop, but it does support `auto`. So you can do: `BOOST_FOREACH(auto& item, soundmap) { item.second->drop(); }`. – Andy Prowl Jan 18 '14 at 16:11
  • I see. Yes! It works! – lapots Jan 18 '14 at 16:13

1 Answers1

2

Try this :-

typedef boost::unordered_map<string, ISoundEngine*> myhash;

BOOST_FOREACH(myhash::value_type& item, soundmap) {
        item.second->drop();
    }
P0W
  • 46,614
  • 9
  • 72
  • 119
  • @user1432980 Could you try it with `typename` keyword. I wonder if `BOOST_FOREACH` macro might need that for such type. OR can use `auto` as suggested in one of the comment – P0W Jan 18 '14 at 16:22
  • 3
    I'm afraid the problem here is with the comma that separates `string` from `ISoundEngine*`. Remember `BOOST_FOREACH` is a macro, so the `, ISoundEngine* ...` part will likely be interpreted as a second argument - I think. Rather use `BOOST_FOREACH(auto& item, soundmap)` or, if you really want to go the C++03 way, define a `typedef` for `boost::unordered_map` or `boost::unordered_map::value_type` outside the macro. – Andy Prowl Jan 18 '14 at 16:43