Home > Mobile >  Why isn't the source std::map for std::map::merge const?
Why isn't the source std::map for std::map::merge const?

Time:10-22

Is there a reason why

template<class C2>
void std::map<Key,T,Compare,Allocator>::merge( std::map<Key, T, C2, Allocator>& source );

takes source as a reference, rather than a const reference?

Perhaps I'm having a senior moment here, but I don't see how source is changed in any way by the function, plus it's less than helpful the way it is.

CodePudding user response:

merge() does, in fact, modify the source map. It's an optimized operation that executes the merge by relinking both maps' innards, transplanting the merged values from one map to another without actually copying either the key or the value, only by fiddling the internal pointers.

Hence, at the end, the source map will (usually) be just a shadow of its former glory. And it cannot be const, for that to happen.

  • Related