What is multimap::emplace() and move()?
I was viewing the MSDN doc about mu开发者_运维问答ltimap and find that it has a member function multimap::emplace(). Below is the example of that member function.
int main( ) {
using namespace std;
multimap<int, string> m1;
pair<int, string> is1(1, "a");
m1.emplace(move(is1));
}
It seems that emplace()
and move()
are C++0x. Can someone explain them for me? I read about move()
, but I really don't understand what does it do (under the hood).
Emplacing is easier to understand with vectors. my_vector.emplace_back(1, 2, 3)
is basically an efficient shortcut for my_vector.push_back(some_type(1, 2, 3))
. Instead of copy-constructing the object in-place, any constructor can now be used for in-place-construction, thus saving the creation, copy (or move) and destruction of a temporary object. Emplacing is powered by perfect forwarding.
std::move(expression)
is basically a cast to an xvalue, which effectively allows the entire expression to be bound to an rvalue reference. You typically do this to enable resource pilfering from named objects that you are no longer interested in because they are going to be destroyed soon, anyway.
精彩评论