1

According to my previous question's answer,

std::pair<iterator, bool> std::unordered_map::insert(const value_type&) 

is useful than

template<class P> std::pair<iterator, bool> std::unordered_map::insert(P&&) 

for some situations like

std::unordered_map<std::string, int> v; v.insert({"key", 1}); 

because the compiler cannot deduce P from {"key", 1}. (Thanks sellibitze)

However, although this argument {"key", 1} (= std::pair<std::string, int>("key", 1)) is prvalue, it cannot move to the container because the argument type is const value_type&.

My question is --- I am sorry to be persistent --- why the standard choose const value_type& for the argument type instead of value_type&&? Or, are there any other reasons that the function with an argument type const value_type& exists and the function with an argument type value_type&& does not exist?

Edit: I created a sample.

3
  • 3
    The missing value_type&& overload of insert was likely an oversight, as with make_unique. Commented Feb 7, 2013 at 18:06
  • 1
    The absence of make_unique was deliberate too. Though I've no objection to it being in a future standard. For C++11 there were not enough people sufficiently expert in unique_ptr to answer questions like: what do you do with the custom deleter? With array types? And I was spread too thin to push it. Commented Feb 7, 2013 at 19:22
  • If you're concerned about performance you might want to use emplace -- possibly using pair's "pairwise constructor". Commented Feb 8, 2013 at 8:12

1 Answer 1

4

The "missing" value_type&& overload of insert for (unordered_)(multi)map is intentional. value_type is a pair<const key_type, value_type>. The const on the key_type pretty much rains on the move-semantics parade.

I really wanted to allow moving from a pair<key_type, value_type>, and thus the introduction of the P&& overload. In hindsight, that may not have been the best solution. However it was a deliberate design decision.

At the time that decision was made, the uniform initialization proposal had not come up. And so the interaction with that was never thoroughly investigated prior to standardization (just not enough time/manpower).

The const value_type& overload comes from C++98, and it was never a consideration to remove or modify it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.