[ptr_container][ptr_map]

Consider the following code: class Base { public: virtual ~Base() = 0 {}; virtual double get() = 0; }; class D1 : public Base { public: D1(double n) : val_(n) {} double get() const { return val_; } private: double val_; }; class D2 : public Base { D2(double n) : val_(n) {} double get() const { return val_; } private: double val_; }; bool operator < (const Base& left, const Base& right) { return (left.get() < right.get()); } I can create a ptr_set of Base like this: boost::ptr_set<Base> set; But I can't create a ptr_map with Base as key, like this: boost::ptr_map<Base, Base> map; The problem is that Base is abstract and so the key cannot be instantiated by ptr_map. What is the cause for this distinction between ptr_set and ptr_map. -- Aaron Levy aaron.levy@yandex.com

On 13-09-2014 22:52, Aaron Levy wrote:
Consider the following code:
I can create a ptr_set of Base like this:
boost::ptr_set<Base> set;
But I can't create a ptr_map with Base as key, like this:
boost::ptr_map<Base, Base> map;
The problem is that Base is abstract and so the key cannot be instantiated by ptr_map. What is the cause for this distinction between ptr_set and ptr_map.
I guess it might be possible, but the work involved was probably large. The workaround is boost::ptr_map<boost::shared_ptr<const Base>,Base> although it's probably not a great workaround, depending on what semantics you are looking for. IIRC, the map interface was not really easy to adopt for a key that is polymorphic. regards -Thorsten
participants (2)
-
Aaron Levy
-
Thorsten Ottosen