Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Stack's push() vs emplace() [duplicate]

Tags:

c++

stack

Trying to understand the difference between using push() or emplace() for std::stack.

I was thinking that if I create a std::stack<int>, then I'd use push() because integer is a primitive type and there is nothing for emplace() to construct.

However, if I was creating std::stack<string> then I'd choose emplace() because std::string is an object.

Is this correct usage?

like image 773
Joshua Oliphant Avatar asked Oct 05 '14 00:10

Joshua Oliphant


People also ask

Which is better emplace or push?

Specific use case for emplace_back : If you need to create a temporary object which will then be pushed into a container, use emplace_back instead of push_back . It will create the object in-place within the container. Notes: push_back in the above case will create a temporary object and move it into the container.

What is the difference between push and emplace for queue?

push() adds a copy of an already constructed object into the queue as a parameter, it takes an object of the queue's element type. emplace() constructs a new object in-place at the end of the queue. It takes as parameters the parameters that the queue's element types constructor takes.

What is the difference between push back and emplace back?

push_back: Adds a new element at the end of the container, after its current last element. The content of val is copied (or moved) to the new element. emplace_back: Inserts a new element at the end of the container, right after its current last element.

Should I use Emplace_back or Push_back?

You should definitely use emplace_back when you need its particular set of skills — for example, emplace_back is your only option when dealing with a deque<mutex> or other non-movable type — but push_back is the appropriate default. One reason is that emplace_back is more work for the compiler.


1 Answers

To fully understand what emplace_back does, one must first understand variadic templates and rvalue references.

This is a fairly advanced, and deep concept in modern C++. On a map, it would be labeled "there be dragons".

You say that you're new to C++ and trying to learn this stuff. This may not be the answer you might be looking for, but you should skip this detail for now, and come back later after you've wrapped your brain around variadic templates and rvalue references. Then it should all make sense.

But if you insist: for a container containing simple, elementary types like integers, there's little, if any difference. The difference comes when the container's type is some large, sophisticated class, with a complicated constructor, and/or copy-constructor.

The end result of either push or emplace is exactly, 100%, the same. The container gets another element appended to it. The difference is where the element comes from:

1) push takes an existing element, and appends a copy of it to the container. Simple, straightforward. push always takes exactly one argument, the element to copy to the container.

2) emplace creates another instance of the class in the container, that's already appended to the container. The arguments to emplace are forwarded as arguments to the container's class's constructor. Emplace can have either one argument, more than one argument, or no argument at all, if the class has a default constructor.

Note that when the class's constructor takes one argument and it is not marked as explicit, it's possible to abuse push and pass it a constructor argument, instead of an existing instance of the class. But let's pretend that this option does not exist, it often leads to horrible code performance, especially with non-trivial classes.

So: if you want to add a copy of an existing instance of the class to the container, use push. If you want to create a new instance of the class, from scratch, use emplace.

like image 86
Sam Varshavchik Avatar answered Oct 11 '22 18:10

Sam Varshavchik