312

C++11 vectors have the new function emplace_back. Unlike push_back, which relies on compiler optimizations to avoid copies, emplace_back uses perfect forwarding to send the arguments directly to the constructor to create an object in-place. It seems to me that emplace_back does everything push_back can do, but some of the time it will do it better (but never worse).

What reason do I have to use push_back?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
David Stone
  • 26,872
  • 14
  • 68
  • 84

6 Answers6

290

I have thought about this question quite a bit over the past four years. I have come to the conclusion that most explanations about push_back vs. emplace_back miss the full picture.

Last year, I gave a presentation at C++Now on Type Deduction in C++14. I start talking about push_back vs. emplace_back at 13:49, but there is useful information that provides some supporting evidence prior to that.

The real primary difference has to do with implicit vs. explicit constructors. Consider the case where we have a single argument that we want to pass to push_back or emplace_back.

std::vector<T> v;
v.push_back(x);
v.emplace_back(x);

After your optimizing compiler gets its hands on this, there is no difference between these two statements in terms of generated code. The traditional wisdom is that push_back will construct a temporary object, which will then get moved into v whereas emplace_back will forward the argument along and construct it directly in place with no copies or moves. This may be true based on the code as written in standard libraries, but it makes the mistaken assumption that the optimizing compiler's job is to generate the code you wrote. The optimizing compiler's job is actually to generate the code you would have written if you were an expert on platform-specific optimizations and did not care about maintainability, just performance.

The actual difference between these two statements is that the more powerful emplace_back will call any type of constructor out there, whereas the more cautious push_back will call only constructors that are implicit. Implicit constructors are supposed to be safe. If you can implicitly construct a U from a T, you are saying that U can hold all of the information in T with no loss. It is safe in pretty much any situation to pass a T and no one will mind if you make it a U instead. A good example of an implicit constructor is the conversion from std::uint32_t to std::uint64_t. A bad example of an implicit conversion is double to std::uint8_t.

We want to be cautious in our programming. We do not want to use powerful features because the more powerful the feature, the easier it is to accidentally do something incorrect or unexpected. If you intend to call explicit constructors, then you need the power of emplace_back. If you want to call only implicit constructors, stick with the safety of push_back.

An example

std::vector<std::unique_ptr<T>> v;
T a;
v.emplace_back(std::addressof(a)); // compiles
v.push_back(std::addressof(a)); // fails to compile

std::unique_ptr<T> has an explicit constructor from T *. Because emplace_back can call explicit constructors, passing a non-owning pointer compiles just fine. However, when v goes out of scope, the destructor will attempt to call delete on that pointer, which was not allocated by new because it is just a stack object. This leads to undefined behavior.

This is not just invented code. This was a real production bug I encountered. The code was std::vector<T *>, but it owned the contents. As part of the migration to C++11, I correctly changed T * to std::unique_ptr<T> to indicate that the vector owned its memory. However, I was basing these changes off my understanding in 2012, during which I thought "emplace_back does everything push_back can do and more, so why would I ever use push_back?", so I also changed the push_back to emplace_back.

Had I instead left the code as using the safer push_back, I would have instantly caught this long-standing bug and it would have been viewed as a success of upgrading to C++11. Instead, I masked the bug and didn't find it until months later.

David Stone
  • 26,872
  • 14
  • 68
  • 84
  • 3
    It would help if you could elaborate on what exactly emplace does in your example, and why it's wrong. – eddi Aug 09 '16 at 21:50
  • 9
    @eddi: I added a section explaining this: `std::unique_ptr` has an explicit constructor from `T *`. Because `emplace_back` can call explicit constructors, passing a non-owning pointer compiles just fine. However, when `v` goes out of scope, the destructor will attempt to call `delete` on that pointer, which was not allocated by `new` because it is just a stack object. This leads to undefined behavior. – David Stone Aug 10 '16 at 00:47
  • Thanks for posting this. I didn't know about it when I wrote my answer but now I wish I'd written it myself when I did learn it afterward :) I really want to slap people who switch to new features just to do the hippest thing they can find. Guys, people were using C++ before C++11 too, and not *everything* about it was problematic. *If you don't know why you're using a feature, **don't use it***. So glad you posted this and I hope it gets way more upvotes so it goes above mine. +1 – user541686 Aug 19 '17 at 20:55
  • @DavidStone Did you mean to say 'implicit' instead of 'explicit' in your answer? – Captain Jack sparrow Oct 30 '17 at 21:59
  • 1
    @CaptainJacksparrow: It looks like I say implicit and explicit where I mean them. Which part has you confused? – David Stone Oct 31 '17 at 22:22
  • @DavidStone Doesn't this statement :"`std::unique_ptr` has an explicit constructor from `T *`" mean that the programmer actually wrote that constructor. But I thought you wanted to imply the constructor that is automatically provided by the language or generated by the compiler (implicit). – Captain Jack sparrow Nov 01 '17 at 18:53
  • 6
    @CaptainJacksparrow: An `explicit` constructor is a constructor that has the keyword `explicit` applied to it. An "implicit" constructor is any constructor that does not have that keyword. In the case of `std::unique_ptr`'s constructor from `T *`, the implementor of `std::unique_ptr` wrote that constructor, but the issue here is that the user of that type called `emplace_back`, which called that explicit constructor. If it had been `push_back`, instead of calling that constructor, it would have relied on an implicit conversion, which can only call implicit constructors. – David Stone Nov 02 '17 at 12:37
  • 2
    Excellent answer. For what it’s worth, Abseil’s [Tip of the week #112](https://abseil.io/tips/112) argues much the same. – Konrad Rudolph Feb 04 '21 at 11:26
130

push_back always allows the use of uniform initialization, which I'm very fond of. For instance:

struct aggregate {
    int foo;
    int bar;
};

std::vector<aggregate> v;
v.push_back({ 42, 121 });

On the other hand, v.emplace_back({ 42, 121 }); will not work.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
  • 67
    Note that this only applies to aggregate initialization and initializer-list initialization. If you would be using `{}` syntax to call an actual constructor, then you can just remove the `{}`'s and use `emplace_back`. – Nicol Bolas Jun 05 '12 at 02:30
  • Dumb question time: so emplace_back can't be used for vectors of structs at all? Or just not for this style using literal {42,121}? – Phil H Jun 05 '12 at 06:31
  • 1
    @LucDanton: As I said, it only applies to *aggregate* and *initializer-list* initialization. You can use `{}` syntax to call actual constructors. You could give `aggregate` a constructor that takes 2 integers, and this constructor would be called when using `{}` syntax. The point being that if you're *trying* to call a constructor, `emplace_back` would be preferable, since it calls the constructor in-place. And therefore doesn't require the type to be copyable. – Nicol Bolas Jun 05 '12 at 13:04
  • Why `std::initializer_list`? I do not think that `emplace_back({ anything here })` ever works. – Johannes Schaub - litb Jun 05 '12 at 21:38
  • @NicolBolas Ah, I got confused by your use of "this only applies": what does *this* refer to? (Similarly what's "it" in "it only applies"?) What I value in `push_back` is not that it's *possible* to use uniform initialization for some cases, it's that it's reliable whether e.g. the value type is an aggregate or not, as you mention. – Luc Danton Jun 05 '12 at 21:48
  • Fixed some confusion regarding passing `{ a, b, c }` to function templates. – Luc Danton Jun 05 '12 at 21:49
  • @NicolBolas: I don't think `push_back` requires the type to be copyable in C++11, does it? – user541686 Jun 29 '13 at 03:43
  • std::tuple would allow use of emplace_back in this case – paulm Jul 29 '14 at 10:05
  • @NicolBolas So the only reason to ever use push_back is "aggregate initialization and initializer-list initialization"? Since that's not exactly the same as "uniform initialization", shouldn't this answer be modified? – Jon Jul 31 '14 at 12:12
  • 15
    This was viewed as a defect in the standard, and has been resolved. See https://cplusplus.github.io/LWG/lwg-active.html#2089 – David Stone Apr 28 '16 at 15:49
  • 4
    @DavidStone Had it been resolved, it wouldn't still be in the "active" list... no? It appears to remain an outstanding issue. The latest update, headed "_[2018-08-23 Batavia Issues processing]_", says that "_[P0960](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0960r1.html) (currently in flight) should resolve this._" And I still can't compile code that tries to `emplace` aggregates without explicitly writing a boilerplate constructor. It's also unclear at this point whether it'll be treated as a defect and thus eligible for backporting, or whether users of C++ < 20 will remain SoL. – underscore_d Jan 06 '19 at 15:03
  • 1
    @underscore_d: Excellent point. I had misunderstood the status of the issue when I had posted that comment. Instead, this has been resolved in C++20 by http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html, which was voted into the standard. – David Stone Aug 28 '20 at 19:50
  • I am curious how does the object gets constructed here. There is no deafult constructor with 2 arguments. So, how does it actually construct the object based on `{42,21}` – Sandeep Dec 13 '21 at 04:27
83

Backwards compatibility with pre-C++11 compilers.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 23
    That seems to be the curse of C++. We get tons of cool features with each new release, but a lot of companies are either stuck using some old version for the sake of compatibility or discouraging (if not disallowing) use of certain features. – Dan Albert Jul 06 '13 at 10:37
  • @DanAlbert: Not sure why it's a curse. Why use a new feature when the old one is sufficient? – user541686 Nov 12 '13 at 10:07
  • 7
    @Mehrdad: Why settle for sufficient when you can have great? I sure wouldn't want to be programming in [blub](http://www.paulgraham.com/avg.html), even if it was sufficient. Not saying that's the case for this example in particular, but as someone who spends most of his time programming in C89 for the sake of compatibility, it's definitely a real problem. – Dan Albert Nov 13 '13 at 22:14
  • 1
    @DanAlbert: I wasn't making a general statement, I was just talking about `push_back` lol. – user541686 Nov 13 '13 at 22:17
  • 3
    I don't think this is really an answer to the question. To me he's asking for use-cases where `push_back` is preferable. – Mr. Boy Nov 03 '15 at 16:12
  • 4
    @Mr.Boy: It's preferable when you want to be backward-compatible with pre-C++11 compilers. Was that unclear in my answer? – user541686 Jul 18 '16 at 09:27
  • 8
    This has gotten way more attention than I've expected, so for all of you reading this: `emplace_back` is **not** a "great" version of `push_back`. It's a potentially **dangerous** version of it. *Read the other answers.* – user541686 Aug 19 '17 at 20:51
  • @user541686, you should add this last comment of yours to the answer itself, as it's important. Also, there are many other answers, so just saying "_read the other answers_" is insufficient. Please quote a couple other answers which you find most pertinent, and link to them. – Gabriel Staples Nov 11 '20 at 19:39
73

Some library implementations of emplace_back do not behave as specified in the C++ standard including the version that ship with Visual Studio 2012, 2013 and 2015.

In order to accommodate known compiler bugs, prefer usingstd::vector::push_back() if the parameters reference iterators or other objects which will be invalid after the call.

std::vector<int> v;
v.emplace_back(123);
v.emplace_back(v[0]); // Produces incorrect results in some compilers

On one compiler, v contains the values 123 and 21 instead of the expected 123 and 123. This is due to the fact that the 2nd call to emplace_back results in a resize at which point v[0] becomes invalid.

A working implementation of the above code would use push_back() instead of emplace_back() as follows:

std::vector<int> v;
v.emplace_back(123);
v.push_back(v[0]);

Note: The use of a vector of ints is for demonstration purposes. I discovered this issue with a much more complex class which included dynamically allocated member variables and the call to emplace_back() resulted in a hard crash.

Marc
  • 1,537
  • 11
  • 21
  • Wait. This appears to be a bug. How can `push_back` be different in this case? – balki Mar 03 '15 at 22:22
  • 15
    The call to emplace_back() uses perfect forwarding to perform construction in place and as such v[0] is not evaluated until after the vector has been resized (at which point v[0] is invalid). push_back constructs the new element and copies/moves the element as needed and v[0] is evaluated prior to any reallocation. – Marc Mar 06 '15 at 16:01
  • Can you give an example of a compiler where you see this behaviour? I can't reproduce this problem. I suspect what you actually saw is the reference to v[0] being invalidated when the vector reallocated. – cdyson37 Mar 31 '15 at 15:08
  • @cdyson37 The described behavior was observed using Visual Studio 2012 compiler version 11 update 4. – Marc Apr 07 '15 at 19:02
  • 1
    @David - Although the new space must exist before the old is destroyed, I don't think that there are any guarantees regarding when the parameter of emplace_back is evaluated. Perfect forwarding enables delayed evaluation. It is my observation that the old vector iterators become invalid before the parameter is evaluated in the compile on which I tested and the details are largely implementation dependent. – Marc Aug 06 '15 at 19:23
  • 1
    @Marc: It is guaranteed by the standard that emplace_back works even for elements inside the range. – David Stone Aug 07 '15 at 00:28
  • @Marc why v[0] is not evaluated until after the vector has been resized? could you describe it in detail? – camino Dec 10 '15 at 20:05
  • 1
    @DavidStone: Could please provide a reference to where in the standard this behavior is guaranteed? Either way, Visual Studio 2012 and 2015 exhibit incorrect behavior. – Marc Dec 11 '15 at 21:30
  • 5
    @cameino: emplace_back exists to delay evaluation of its parameter to reduce unnecessary copying. The behavior is either undefined or a compiler bug (pending analysis of the standard). I recently ran the same test against Visual Studio 2015 and got 123,3 under Release x64, 123,40 under Release Win32 and 123,-572662307 under Debug x64 and Debug Win32. – Marc Dec 11 '15 at 21:38
  • The standard guarantee is found at http://eel.is/c++draft/container.requirements#tab:container.seq.opt. The guarantee isn't specifically stated as such, but rather, when the standard describes the behavior it does not include a precondition that the object not already be in the container, therefore the implementations are required to make this work. It's unfortunate that Visual Studio has a bug in this. :( – David Stone Aug 28 '20 at 19:55
  • @DavidStone: it doesn't seem practical to expect the Standard to guarantee this would work. More generally, the problem exists whenever `emplace_back` is passed a reference to anything inside the existing container, however indirectly (it might not come from the function calling `emplace_back`): that wouldn't necessarily be able to detected at compile time, and to check for it at runtime would be unwanted overhead on all `emplace_back` calls. It makes sense that this is - and is left as - undefined behaviour. – Tony Delroy Sep 23 '21 at 18:32
  • 1
    @TonyDelroy: It is not undefined behavior, the standard does guarantee it. There is also no overhead from supporting this (which is the reason it wasn't carved out as being undefined behavior). If you don't need to reallocate, there's no issue because old references are valid. If you do need to reallocate, you allocate the new storage, construct the new object in the correct location in that storage, then move the old objects over. All of that is work you have do regardless, so there's no extra cost. – David Stone Sep 23 '21 at 20:52
  • 2
    @DavidStone: oh yeah, that does make sense. Thanks. – Tony Delroy Sep 24 '21 at 13:54
0

Use push_back only for primitive/built-in types or raw pointers. Otherwise use emplace_back.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
-1

Consider what happens in Visual Studio 2019 with c++-17 compiler. We have emplace_back in a function with proper arguments set up. Then someone changes parameters of the constuctor called by emplace_back. There is no warning whatsover in VS, the code also compiles fine, then it crashes in runtime. I removed all emplace_back from the codebase after this.

Radek
  • 97
  • 1
  • 3