According to cppreference, std::vector::emplace_back
has only one signature, which is:
template< class... Args >
reference emplace_back( Args&&... args );
And in the description, it says emplace_back
is supposed to forward each of its arguments to the constructor of the type in the vector
. However, when I tested the following code using gcc 12.2, it successfully compiles:
#include <iostream>
#include <vector>
class Foo
{
public:
int x;
int y;
Foo(int x, int y) : x(x), y(y)
{}
};
int main()
{
std::vector<Foo> foos;
Foo foo(1, 2);
foos.push_back(std::move(foo));
foos.emplace_back(foo); // weird
}
(See on compiler explorer)
I expected the line foos.emplace_back(foo);
to fail the compilation. It seems as if emplace_back
has this overload:
template< class T >
reference emplace_back( T& arg );
which isn't mentioned in the documentation. Am I missing something or is this just a compiler extension?