Since in C++, primitive types destructors do nothing [Do Primitive Types in C++ have destructors?], is it safe to rely on the value of int a
to be the same after a call to queue::emplace
? Specifically,
queue<int> q;
int a = 5;
q.emplace(a);
// is a==5 here?
Perhaps the first question would also answer this, though for this example:
queue<pair<int,int>> q;
int a = 1, b = 2;
q.emplace(a,b);
// is a == 1, b == 2?
The parameters in the shown code are all lvalues.
For the emplaced primitive type, as is the case here: an lvalue that gets passed to emplace()
does not get modified. If the container contains a class with a constructor, that emplace ends up invoking, for a "well-behaved" constructor the parameter won't get modified whether it's an int
or a discrete class instance.
The only time something that gets passed to emplace()
typically gets altered, in some way, would be if that "something" is a movable rvalue, and the corresponding constructor parameter is an rvalue, and the constructor moves the rvalue somewhere else, leaving the original parameter in some valid, but unspecified state. Or, if "something" is a non-const lvalue, and the constructor intentionally modifies it, but that would be rather rude (not well-behaved).
This wouldn't be the case for primitive types, in any case.