在C ++ 11中,在允许就地构造的时候,通常emplace_back()方法emplace_back()送到push_back() , 而在使用push_back(std::move())的时候,emplace_back构建对象?
例如,在以下情况下, emplace_back()仍然是首选?
std::string mystring("hello world"); std::vector<std::string> myvector; myvector.emplace_back(mystring); myvector.push_back(std::move(mystring)); // (of course assuming we don't care about using the value of mystring after)另外,上面的例子是否有任何好处呢?
myvector.emplace_back(std::move(mystring));还是这里的移动完全是多余的,还是没有效果?
In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object?
For instance, is emplace_back() still preferred in cases like the following?
std::string mystring("hello world"); std::vector<std::string> myvector; myvector.emplace_back(mystring); myvector.push_back(std::move(mystring)); // (of course assuming we don't care about using the value of mystring after)Additionally, is there any benefit in the above example to instead doing:
myvector.emplace_back(std::move(mystring));or is the move here entirely redundant, or has no effect?
最满意答案
让我们看看你提供的不同的电话是什么:
emplace_back(mystring) :这是你提供的任何参数的新元素的就地构造。 由于您提供了一个左值,因此这个就地构造实际上是一个复制构造,就像调用push_back(mystring)
push_back(std::move(mystring)) :这调用move-insert,在std :: string ist的情况下就是move-construction。
emplace_back(std::move(mystring))这是一个就地构造,其中包含你提供的参数。 由于该参数是一个rvalue,它调用std::string的移动构造函数,即它是一个就地移动构造,如2所示。
换句话说,如果调用一个类型为T的参数,无论是rvalue还是lvalue, emplace_back和push_back都是等效的。
然而,对于任何其他参数, emplace_back赢得比赛,例如在vector<string>使用char const* :
emplace_back("foo")为in-place-construction调用string::string(char const*) 。
push_back("foo")首先必须调用string::string(char const*)用于匹配函数的签名所需的隐式转换,然后像上面的case 2一样移动插入。 因此它相当于push_back(string("foo"))
Let's see what the different calls that you provided do:
emplace_back(mystring): This is an in-place construction of the new element with whatever argument you provided. Since you provided an lvalue, that in-place construction in fact is a copy-construction, i.e. this is the same as calling push_back(mystring)
push_back(std::move(mystring)): This calls the move-insertion, which in the case of std::string ist an in-place move-construction.
emplace_back(std::move(mystring)): This is again an in-place construction with the arguments you provided. Since that argument is an rvalue, it calls the move-constructor of std::string, i.e. it is an in-place move-construction like in 2.
In other words, if called with one argument of type T, be it an rvalue or lvalue, emplace_back and push_back are equivalent.
However, for any other argument(s), emplace_back wins the race, for example with a char const* in a vector<string>:
emplace_back("foo") calls string::string(char const*) for in-place-construction.
push_back("foo") first has to call string::string(char const*) for the implicit conversion needed to match the function's signature, and then a move-insertion like case 2. above. Therefore it is equivalent to push_back(string("foo"))
更多推荐
发布评论