我有一个类,其中 VS 2015 没有生成一个 move ctor,现在 VS 2015 Update 1 正在生成一个,这导致了二进制兼容性问题,不确定哪个版本在做正确的事情。
// MyLib
struct Shared
{
virtual ~Shared(){}
}
struct Callback : public Shared
{
virtual void response() = 0;
}
// App
struct CallbackI : public Callback
{
virtual void response(){}
}
当我使用 VS 2015 构建 MyLib 并使用 VS 2015 update 1 构建应用程序时,链接应用程序失败,因为缺少引用回调基类的移动赋值运算符的符号。
在我看来,VS 2015 没有生成这个运算符,而 VS 2015 Update 1 是,但是哪个编译器版本就在这里?
最佳答案
VS 2015 Update 1 正在做正确的事情(实际上是第一个实现所有 C++11 的 VS 版本)
来自标准:
§12.8 Copying and moving class objects
If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if
- X does not have a user-declared copy constructor,
- X does not have a user-declared copy assignment operator,
- X does not have a user-declared move assignment operator,
- X does not have a user-declared destructor, and
- the move constructor would not be implicitly defined as deleted.
Shared
中用户生成的析构函数隐式地将移动构造函数标记为已删除。当你让编译器生成它时,编译器也可以生成移动赋值运算符。
struct Shared
{
virtual ~Shared() = default;
}
出于性能原因,最好以这种方式声明空的虚拟析构函数,因为它保证了编译器的内联。
关于c++ - 基类析构函数是否阻止生成移动构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34810580/