I'm practicing with std::move wih std::vector::push_back. I have this simple code:
struct A
{
A(int i,bool b,float f) : _i(i), _b(b), _f(f) {}
A(const A& a)
{
std::cout << "copy constructor" << std::endl;
}
A(A&& a)
{
std::cout << "move constructor" << std::endl;
}
int _i;
bool _b;
float _f;
};
struct B
{
template <typename T>
void Add(T&& t) // universal reference
{
_v.push_back(std::move(t));
}
private:
std::vector<A> _v;
};
int main() {
A a(1, false, 2.f);
B b;
std::cout << "using rValue" << std::endl;
b.Add(A(1, true, 2.f));
std::cout << "using lValue" << std::endl;
b.Add(a);
return 0;
}
the output for some reason is :
using rValue
move constructor
using lValue
move constructor
copy constructor
Why is the copy constructor still invoked? shouldn't I have only move-constructors in my output??