提问人:Evethir 提问时间:7/21/2020 更新时间:7/21/2020 访问量:362
Move and Copy 构造函数与 std::move [duplicate] 同时调用
Move and Copy Constructor called at the same time with std::move [duplicate]
问:
我正在练习 std::move wih std::vector::p ush_back。我有这个简单的代码:
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;
}
由于某种原因,输出是:
using rValue
move constructor
using lValue
move constructor
copy constructor
为什么仍然调用复制构造函数?我的输出中不应该只有移动构造函数吗?
答: 暂无答案
评论
push_back
noexcept