Given the code in Visual Studio 2010:
void Foo::Bar() const
{
map_t::const_iterator iter(my_map_.find(key));
if(my_map_.end() != iter)
DoStuff(iter->second);
}
Do stuff will take the mapped_type by value. The mapped type is a copyable, movable, assignable type.
I get error messages that the key/value pair can’t be copied when trying to access second. Even if I write on their own lines:
iter->second;
(*iter).second;
to ensure it’s nothing to do with DoStuff…
I presume the iterator is making a copy of the key/value pair before returning operator-> or operator*.
How do I get a copy of the mapped type?
EDIT:
The map itself is of unsigned shorts to boost variants, roughly as such:
typedef struct{} empty_t;
typedef boost::variant<empty_t, double, long, some POD types> variant_t;
typedef std::map<unsigned short, variant_t> map_t;
And then as a private member of the class:
map_t my_map_;
And to be clear, the problem is not in passing to DoStuff. I can remove that line, and simply dereference the iterator and access second, and that will cause the compiler error.
if(my_map_.end() != iter)
iter->second; //Doesn't do anything, but illustrates the error.
I figured it out.
WAYYYYY away from this code, and a couple abstractions deep, the iterators to the map were being passed to std::partition! You can’t partition a map, it seems.
I copied the contents of the map into a local vector and continued processing from there, and it cleared up all the error messages.
I have no idea why the compiler was getting so throughly confused. The problem code was somewhere completely unrelated. But it doesn’t matter.