Opened 13 years ago

Closed 12 years ago

Last modified 11 years ago

#4107 closed Bugs (invalid)

ptr_map::mapped_type incorrect

Reported by: olafvdspek@… 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 c_crickmar <chris@…>, 12 years ago

Resolution: invalid
Status: newclosed

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.

comment:2 by anonymous, 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 c_crickmar <chris@…>, 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 anonymous, 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 c_crickmar <chris@…>, 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:6 by anonymous, 12 years ago

The address operator works fine, that's not the problem.

comment:7 by c_crickmar <chris@…>, 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*.

comment:8 by anonymous, 12 years ago

Why can't it be a reference to T?

comment:9 by c_crickmar <chris@…>, 12 years ago

that would require std::pair<Key, T&>.

comment:10 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Just for the record: "T::value_type::second_type*" works.

Note: See TracTickets for help on using tickets.