I am confused about std::maps's implementation of emplace(). emplace() is a variadic template function with the following declaration:
template <class Args...>
iterator emplace( Args&&... args );
As far as I understand, emplace() completely avoids constructing its value_type, i.e., std::pair<const key_type,mapped_type>, and instead constructs the object in-place. I assume this means that the object is constructed only once, the moment that the new map node is created. The object is never copied or moved.
What I don't understand is how std::map finds the correct place to add the new entry before constructing the key-value pair. As far as I see, std::map needs access to the key but the key is contained within args and only accessible after construction of the key-value pair.
I must be misunderstanding some aspect of emplace(). Does std::map actually avoid construction of the key-value pair? If so, how does it avoid move/copy operations? Or is there another way to access the key in args?