I've been trying to play around with the Boost Pointer Container library and utilize their tutorial examples to get a feel for the library. Maybe I'm missing something, but I can't seem to store a simple class that I've defined as the key to ptr_map. However, the same key works for ptr_set.
<!-- language: lang-cpp -->
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/ptr_container/ptr_set.hpp>
class mammal
{
public:
mammal(string name) : _name(name) {}
mammal(string name, int age) : _name(name), _age(age) {}
int age() const
{
return _age;
}
string name() const
{
return _name;
}
void eat() { cout << "Not Hungry..." << endl; }
bool operator<(const mammal& l) const
{
cout << " Using Mammal Impl" << endl;
if (name() == l.name())
return age() < l.age();
return name() < l.name();
}
private:
string _name;
int _age;
};
int main()
{
std::string bobo = "bobo",
anna = "anna",
zublu = "zublu";
typedef boost::ptr_set<mammal> MammalsContainer;
MammalsContainer mammalZoo;
mammal* m1 = new mammal(zublu, 14);
mammal* m2 = new mammal(bobo, 31);
mammal* m3 = new mammal(anna, 441);
mammal* m4 = new mammal(bobo, 21);
mammalZoo.insert(m1);
mammalZoo.insert(m2);
mammalZoo.insert(m3);
mammalZoo.insert(m4);
for (MammalsContainer::iterator i = mammalZoo.begin(); i != mammalZoo.end(); ++i)
{
cout << " Mammal Name: " << i->name() << " Age: " << i->age() << endl;
}
return 0;
}
This generates the following, correct output:
Mammal Name: anna Age: 441
Mammal Name: bobo Age: 21
Mammal Name: bobo Age: 31
Mammal Name: zublu Age: 14
However, if I switch the MammalsContainer to be a ptr_map, it doesn't even compile:
typedef boost::ptr_map<mammal, int> MammalsContainer;
mammalZoo.insert(m1, 1);
mammalZoo.insert(m2, 2);
mammalZoo.insert(m3, 3);
mammalZoo.insert(m4, 4);
Results in the following compile errors:
ptrcontainer.cpp:125: error: no matching function for call to ‘boost::ptr_map<mammal,
int, std::less<mammal>, boost::heap_clone_allocator, std::allocator<std::pair<const
mammal, void*> > >::insert(mammal*&, int)’
/usr/local/include/boost/ptr_container/ptr_map_adapter.hpp:548: note: candidates are:
std::pair<typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap,
CloneAllocator, Ordered>::iterator, bool> boost::ptr_map_adapter<T, VoidPtrMap,
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 = int, VoidPtrMap =
std::map<mammal, void*, std::less<mammal>, std::allocator<std::pair<const mammal,
void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]
I've read all the documentation/examples on the Pointer Container boost page, along with lots of StackOverflow problems relating to ptr_map; it seems everyone's use case involves using a simple primitive key like a std::string or int. It works if I have a string/int Key in the above example and store the Mammal class as the value.
What I really want to build is a a Key object hierarchy and be able to provide the specialized operator< so that sorting can work. My eventual use-case is that my Keys can be composed of different properties i.e. Key1 contains only a name attribute; Key2 contains a name and location. Additionally the value of the map is also represented by a object hierarchy. I initially started out using a
std::map<boost::shared_ptr<KeyObj>, ValueObj>
but was lead to the ptr_container library as a more efficient solution which handles polymorphism in associative containers. Or maybe I jumped to that conclusion.
In ptr_map
only the value (second
) is stored by pointer. The key is still stored by value, and you're passing in a pointer, not a value.