我有一个删除了移动构造函数的类,当我尝试在MSVC中调用std :: vector :: push_back()时(v.15.8.7 Visual C++ 2017)我收到错误消息说我正在尝试访问已删除的移动构造函数.但是,如果我定义了移动构造函数,则代码会编译,但永远不会调用移动构造函数.这两个版本在gcc(v.5.4)上按预期编译和运行.
这是一个简化的例子:
#include <iostream>
#include <vector>
struct A
{
public:
A() { std::cout << "ctor-dflt" << std::endl; }
A(const A&) { std::cout << "ctor-copy" << std::endl; }
A& operator=(const A&) { std::cout << "asgn-copy" << std::endl; return *this; }
A(A&&) = delete;
A& operator=(A&& other) = delete;
~A() { std::cout << "dtor" << std::endl; }
};
int main()
{
std::vector<A> v{};
A a;
v.push_back(a);
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio上编译时会出现以下错误:
error C2280: 'A::A(A &&)': attempting to …Run Code Online (Sandbox Code Playgroud)