对于已经构建的对象,C ++ 11 push

编程入门 行业动态 更新时间:2024-10-28 16:29:47
对于已经构建的对象,C ++ 11 push_back()与std :: move对emplace_back()的效率(Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects)

在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"))

更多推荐

本文发布于:2023-08-06 14:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   push

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!