#4107 closed Bugs (invalid)
ptr_map::mapped_type incorrect
Reported by: | Owned by: | Thorsten Ottosen | |
---|---|---|---|
Milestone: | Boost 1.43.0 | Component: | ptr_container |
Version: | Boost 1.42.0 | Severity: | Problem |
Keywords: | Cc: |
Description
ptr_map::mapped_type seems incorrect. It's int* instead of int.
#include <boost/ptr_container/ptr_map.hpp> #include <map> template <class T, class U> typename T::mapped_type* find_ptr(T& c, U v) { typename T::iterator i = c.find(v); return i == c.end() ? NULL : &i->second; } int main() { typedef boost::ptr_map<char, int> c1_t; std::map<char, int> c0; c1_t c1; int* p0 = find_ptr(c0, 'x'); int* p1 = find_ptr(c1, 'x'); return 0; }
Change History (10)
comment:1 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
I know. But isn't ptr_map designed to provide the interface of std::map as close as possible? When 'reading' ptr_map, it looks like it contains (references to) objects. So it'd be great if mapped_type reflected that.
comment:3 by , 12 years ago
The value_type would have to be translated from std::pair<Key, T*> to std::pair<Key, T> which couldn't be done without copying T. I suppose a reference wrapper could be used but then the value type is std::pair<Key, boost::reference_wrapper<T> > which I don't think is an improvement.
comment:4 by , 12 years ago
I'm not sure what you're trying to say. If you look at the code example, I'm returning a pointer, so no copy is necessary.
comment:5 by , 12 years ago
yes but you are taking the address of the value here '&i->second' hence the value_type (i) would have to be std::pair<Key, T>.
comment:7 by , 12 years ago
For 'second' to be T and not T*, which is required in your code fragment, it would have to be a copy of the stored T*.
boost::ptr_map is not a drop in replacement for std::map. mapped_type is always a pointer, the container is designed to handle the memory management tasks of the heap allocated object.