Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c++ - push_back和emplace_back(push_back vs emplace_back)

I'm a bit confused regarding the difference between push_back and emplace_back .

(关于push_backemplace_back之间的区别,我有些困惑。)

void emplace_back(Type&& _Val);
void push_back(const Type& _Val);
void push_back(Type&& _Val);

As there is a push_back overload taking a rvalue reference I don't quite see what the purpose of emplace_back becomes?

(由于存在push_back重载获取右值引用,我不太明白emplace_back的目的是什么?)

  ask by ronag translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In addition to what visitor said :

(访客除了说什么:)

The function void emplace_back(Type&& _Val) provided by MSCV10 is non conforming and redundant, because as you noted it is strictly equivalent to push_back(Type&& _Val) .

(void emplace_back(Type&& _Val)提供的void emplace_back(Type&& _Val)是不符合标准且多余的,因为如您所述,它严格等效于push_back(Type&& _Val) 。)

But the real C++0x form of emplace_back is really useful: void emplace_back(Args&&...) ;

(但是emplace_back的真正C ++ 0x形式确实有用: void emplace_back(Args&&...) ;)

Instead of taking a value_type it takes a variadic list of arguments, so that means that you can now perfectly forward the arguments and construct directly an object into a container without a temporary at all.

(无需采用value_type而是采用可变参数列表,因此,这意味着您现在可以完美地转发参数,并直接将对象构造到容器中,而无需任何临时操作。)

That's useful because no matter how much cleverness RVO and move semantic bring to the table there is still complicated cases where a push_back is likely to make unnecessary copies (or move).

(这很有用,因为无论RVO和移动语义有多么聪明,仍然存在复杂的情况,其中push_back可能会产生不必要的副本(或移动)。)

For example, with the traditional insert() function of a std::map , you have to create a temporary, which will then be copied into a std::pair<Key, Value> , which will then be copied into the map :

(例如,使用std::map的传统insert()函数,您必须创建一个临时文件,然后将其复制到std::pair<Key, Value> ,然后将其复制到地图中:)

std::map<int, Complicated> m;
int anInt = 4;
double aDouble = 5.0;
std::string aString = "C++";

// cross your finger so that the optimizer is really good
m.insert(std::make_pair(4, Complicated(anInt, aDouble, aString))); 

// should be easier for the optimizer
m.emplace(4, anInt, aDouble, aString);

So why didn't they implement the right version of emplace_back in MSVC?

(那么,为什么他们没有在MSVC中实现正确版本的emplace_back?)

Actually, it bugged me too a while ago, so I asked the same question on the Visual C++ blog .

(实际上,它在一段时间之前就困扰了我,因此我在Visual C ++博客上提出了同样的问题。)

Here is the answer from Stephan T Lavavej, the official maintainer of the Visual C++ standard library implementation at Microsoft.

(这是Microsoft Visual C ++标准库实现的官方维护者Stephan T Lavavej的回答。)

Q: Are beta 2 emplace functions just some kind of placeholder right now?

(问:beta 2 emplace函数现在只是某种占位符吗?)

A: As you may know, variadic templates aren't implemented in VC10.

(答:您可能知道,可变参数模板未在VC10中实现。)

We simulate them with preprocessor machinery for things like make_shared<T>() , tuple, and the new things in <functional> .

(我们使用预处理器机制对它们进行仿真,以处理诸如make_shared<T>() ,元组和<functional>的新事物。)

This preprocessor machinery is relatively difficult to use and maintain.

(这种预处理器机器相对难以使用和维护。)

Also, it significantly affects compilation speed, as we have to repeatedly include subheaders.

(另外,由于我们不得不反复包含子标题,因此它会显着影响编译速度。)

Due to a combination of our time constraints and compilation speed concerns, we haven't simulated variadic templates in our emplace functions.

(由于时间限制和编译速度方面的考虑,我们没有在emplace函数中模拟可变参数模板。)

When variadic templates are implemented in the compiler, you can expect that we'll take advantage of them in the libraries, including in our emplace functions.

(当可变参数模板在编译器中实现时,您可以期望我们将在库中使用它们,包括在我们的emplace函数中。)

We take conformance very seriously, but unfortunately, we can't do everything all at once.

(我们非常重视一致性,但不幸的是,我们不能一次完成所有事情。)

It's an understandable decision.

(这是一个可以理解的决定。)

Everyone who tried just once to emulate variadic template with preprocessor horrible tricks knows how disgusting this stuff gets.

(每个曾经尝试使用预处理器可怕技巧来模拟可变参数模板的人都知道这些东西有多恶心。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...