23
struct Object {  
    Object() { cout << "constructor\n"; }
    Object(const Object &) { cout << "copy constructor\n"; }
    Object(Object &&) { cout << "move constructor\n"; }
};

int main() {
    vector<Object> v;
    v.reserve(10);
    v.emplace_back(Object{});
}

This gives me the following output:

constructor
move constructor

Why? I thought that emplace_back does create the Object in place, so that no copy or move constructors have to be called.
From the description:

The element is constructed in-place, i.e. no copy or move operations are performed.

EDIT: Ah, okay, it seems that I fundamentally misunderstood emplace_back(). You don't have to have the Object as an argument, since it is automatically created in place for you. You only have to give the arguments for the Object-constructor to emplace_back().

So, if I had a new constructor like this:

Object(int) { cout << "int constructor\n"; }   

I would call emplace_back like this:

v.emplace_back(42);  

instead of this:

v.emplace_back(Object(42));

Makes sense now, thanks a lot!

EDIT2: I wish I could accept all of your answers! :-P

6
  • 2
    Use v.emplace_back() to avoid any move.
    – Jarod42
    Feb 18, 2014 at 15:30
  • Only vaguely related warning: With emplace_back on a map, you can still get pair construction, causing a move, even in cases when no insertion ends up being performed: stackoverflow.com/a/20328676/560648 Feb 18, 2014 at 15:37
  • @LightnessRacesinOrbit To my knowledge, that only happens with std::map - I've never seen that happen with a std::vector. Feb 18, 2014 at 16:38
  • 1
    @ZacHowland: Yep it should be limited to those containers with pair value types. Hence my bold ;) I could have simply not posted the comment, but I was half-way through before I realised how pointless it was and... well... I don't like waste... :) Feb 18, 2014 at 16:56
  • I stumbled upon the same question but for legibility reasons, i.e. we wanted to use v.emplace_back(Object(42)); to be able to grep for the constructor uses. Now I thought I can solve the legibility problem without performance loss by calling v.emplace_back<Object>(42); but to my surprise this also called the move constructor. I didn't find the reason for that, any clue? Sep 12, 2019 at 8:58

5 Answers 5

24

emplace_back forwards its arguments to the constructor of the vector element class, called in-place on the next available position of the vector.

v.emplace_back(Object{});

is sort of equivalent to:

{
     Object tmp;
     v.emplace_back(std::move(tmp));
}

That's why you are getting a regular constructor call followed by a move constructor call. If you want to append a new object with emplace_back, just call:

v.emplace_back();

Just for the sake of completeness, another reason why emplace_back might call a move constructor is: emplace_back may cause the vector to grow, and thus move its initial contents to their new memory location. This is not the problem here, because calling reserve guarantees enough capacity, but generally it's an answer to the question.

4
  • 1
    @Yakk Could you expand on that 'False'?
    – bames53
    Feb 18, 2014 at 15:36
  • @Yakk: I actually tested this in my IDE, and at least on a practical level, my answer seems to work... Do you have a problem with my "sort of equivalent" formulation ?
    – Martin J.
    Feb 18, 2014 at 15:40
  • That is strange. The one I read had no std::move in it I swear... I know SO sometimes collapses edits: did you add std::move after originally posting, or did I hallucinate? Feb 18, 2014 at 16:32
  • @Yakk: Rejoice, for you are not crazy! For the first minute or so, my answer was actually missing the std::move call.
    – Martin J.
    Feb 18, 2014 at 16:38
10

v.emplace_back(Object{}); will call a placement new new (place) Object(Object{}) so a move constructor.

Use v.emplace_back(); to avoid to call extra object constructor,
which will result in new (place) Object;

5
  • If you will try v.emplace_back() then gcc compiler will reports Default constructor Copy constructor Feb 18, 2014 at 15:54
  • @VladfromMoscow: What have you try ? It works as expected : ideone.com/FPzZfw
    – Jarod42
    Feb 18, 2014 at 16:03
  • I tried the same ideone.com and for code v.emplace_back( Object() ); v.emplace_back(); I got Default constructor Move constructor Default constructor Copy constructor Feb 18, 2014 at 16:13
  • @VladfromMoscow: What you see is due to internal resize() : ideone.com/8YQCd2, with some noexecpt, the last copy constructor becomes move constructor.
    – Jarod42
    Feb 18, 2014 at 16:24
  • Oh, You are write, I totally forgot about this because in the original code there is a call of reserve but I omited it.:) Feb 18, 2014 at 16:30
6

emplace_back calls whatever constructor matches the arguments you pass it. In this case you have chosen to pass Object{}, which matches the Object&& move constructor.

When the description says, "no move or copy is performed", it means apart from the object construction that you specify. If that construction happens to be a move or copy then of course it is performed.

If you want emplace_back to use the no-args constructor then pass it no arguments: v.emplace_back().

3

The version you are linking is for passing constructor arguments. To achieve the desired effect, you could call v.emplace_back();

3
  • v.emplace_back<Object>(Object{}); still gives me the same output. Feb 18, 2014 at 15:33
  • 4
    I meant with no arguments at all. Feb 18, 2014 at 15:33
  • @gartenriese You do not need the Object{} in the function call. Just call emplace_back with no arguments. Feb 18, 2014 at 15:34
1

You are calling emplace_back with an object of the same type that is stored in the vector so you are requesting a copy of that object (Same as Object O(Object{});). Since you provide a temporary, the move constructor is invoked instead of the copy constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.