I have a std::map
whose keys are std::string
and values are my own defined type.
Let's suppose I have the following code:
std::map<std::string, MyType> mymap;
std::string str1("test");
MyType value(pars); //I want value to be moved
mymap.emplace(std::make_pair(str1, std::move(value))); //A
mymap.emplace(str, std::move(value)); //B
Assuming std::map
stores pairs, I guess A would generate a further call to std::pair
constructor (make_pair
), followed by another call to std::pair
move constructor (in-place construction with rvalue argument).
And I think B would just generate a call to std::pair
constructor.
So can we say B is preferred over A in order to avoid unnecessary constructions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…