The following code fails for push_back
and succeeds for emplace_back
:
#include <vector>
volatile int x = 0;
int main()
{
std::vector<int> vec;
vec.emplace_back(x);
vec.push_back(x); // error: no matching function for call to 'std::vector<int>::push_back(volatile int&)'
}
I understand that push_back
failes because it takes a reference and tries to cast away volatile
qualifier implicitly from that reference.
However, emplace_back
also takes a reference (rvalue-references are references). Why is it treated differently?