Look at this code:
struct Dummy
{
int bla;
int blabla;
char character;
Dummy (int b, int bb, char c)
: bla(b), blabla(bb), character(c)
{}
};
std::stack<Dummy> s;
Dummy dummy;
s.push(dummy); // (1)
s.emplace(dummy); // (2)
I fail to see the difference between (1)
and (2)
. I know emplace()
is useful when you're providing arguments for the constructor of the object being added, e.g.:
s.emplace(1, 2, 'c');
but I don't know what the difference is in the case I described because both push()
and emplace()
should take a reference to the local dummy
object and create a new object using copy ctor or something like that. Does push()
create any temporary objects in the process?