Insert v. Emplace

What is the difference between the insert() function and the emplace functions?
The insert function inserts an object (a full object), while emplace acts like the object's constructor and constructs the object into the sequence.
Here's an illustration of what Zhuge already posted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

class Example
{
public:

    // bogus parameters to help illustrate the idea
    Example(const char *, int)
    {
    }
};

int main()
{
    std::vector<Example> ve;
    Example e("Example object 1", 1);

    ve.push_back(e);
    ve.push_back(Example("Example object 2", 2));
    ve.emplace_back("Example object 3", 3);
}


The emplace() functions are variadic template functions, a new feature of C++11. So they can take an arbitrary number of parameters (of different types) to then feed them to a constructor.
Does insert() take an object from somewhere, then put the object somewhere else (Such as the "cut" function on MSW)?

Following up, do the emplace() functions take the information about an object, then construct another object with everything the same in another place?
With emplace(), the object is directly constructed in-situ. There is no copy (or move) performed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>

struct A
{
    A() = default ;

    A( int ii, const std::string& ss ) : i(ii), str(ss)
    { std::cout << "construct object from args\n" ; }

    A( const A& that ) : i(that.i), str(that.str)
    { std::cout << "copy object\n" ; }

    int i = 0 ;
    std::string str ;
};

int main ()
{
    std::vector<A> seq ;
    seq.reserve(10) ;

    A a ;

    seq.push_back(a) ; // copy object into the container
    seq.insert( seq.end(), a ) ; // copy object into the container
    std::cout << "-------------\n" ;

    seq.push_back( A( 10, "hello" ) ) ; // construct an anonymous object
                                        // copy (or move) the anonymous object into the container
                                        // destroy the anonymous object
    std::cout << '\n' ;

    seq.insert( seq.end(), A( 10, "hello" ) ) ; // construct an anonymous object
                                                // copy (or move) the anonymous object into the container
                                                // destroy the anonymous object
    std::cout << "-------------\n" ;

    seq.emplace_back( 10, "hello" ) ; // construct the object in-place in the container
    seq.emplace( seq.end(), 10, "hello" ) ; // construct the object in-place in the container
    std::cout << "-------------\n" ;
}

http://coliru.stacked-crooked.com/a/34b973da96dadc6f
Last edited on
So, does insert() move an object from one location to another?
Last edited on
A copy (or move) implies another object.
Construction does not.

Copy example for vec.push_back( A( 10, "hello" ) );

1) A temporary A is created on the stack
2)a) If A has a copy ctor, then the new object is copy constructed from the temporary
2)b) Otherwise the new object is default constructed, and the copy (assignment) operator is invoked to copy the temporary's value to the new object
3) The temporary object is destroyed

Move example for vec.push_back( A( 10, "hello" ) );

1) A temporary A is created on the stack
2) The new object is default constructed
3) The innards of the two objects are swapped ("moved")
4) The temporary is destroyed

Emplace example for vec.emplace_back( 10, "hello" );

1) The new object is constructed

Hope this helps.

CDuck wrote:
So, does insert() move an object from one location to another?
No, it performs an expensive copy.
LB wrote:
No, it performs an expensive copy.

Does it?
http://en.cppreference.com/w/cpp/container/vector/insert

It may use copy or move semantics.
CDuck's repeated posts and wording made me suspicious that he was talking about this:
1
2
Object obj;
vec.insert(/**/, obj); //does not move 
Last edited on
vec.insert(obj); //does not compile
I'm having a bad day, haha.
Thank you for your help.
Topic archived. No new replies allowed.