#include <iostream>
#include <vector>
class Example
{
public:
// bogus parameters to help illustrate the idea
Example(constchar *, 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?
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" );