Would you teach me why both
std::unordered_map::insert(const value_type&)
and
template<class P> std::unordered_map::insert(P&&)
exist in the standard?
I think that insert(P&&) can serve as insert(const value_type&).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both of these overloads
have their advantages and neither can fully replace the other. The first one seems like a special case of the second one since
Pmight be deduced to beconst value_type&. The nice thing about the 2nd overload is that you can avoid unnecessary copies. For example, in this case:Here, the result of make_pair is actually a
pair<int, const char*>whereasvalue_typemight bepair<const int, string>. So, instead of creating a temporaryvalue_typeobject and copying it into the container, we have the chance of directly creating thevalue_typeobject into the map by converting the argument and/or moving its members.On the other hand, it would be nice if this worked as well:
But this list is actually not an expression! The compiler can’t deduce P for the second overload because of that. The first overload is still viable since you can copy-initialize a
pair<const int,string>parameter with such a list.