c++ - 基类析构函数是否阻止生成移动构造函数

标签 c++ visual-studio-2015

我有一个类,其中 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/

相关文章:

c++ - 增加无序映射中默认构造的 int

c++ - 减少发布版本中非常大的静态库的大小

javascript - 在 Visual Studio 2015 中,有没有办法使用 Internet Explorer 以外的浏览器调试 Javascript?

visual-studio - Nuget 包管理器长文件名错误

c# - ASP.NET 5 RC Web API + Framework 4.6 RC Lib

c++ - 将 stdout/stderr 重定向到 null vs2017 c++

c++ - 字符串变量 : "No operator << matches these operands" 的问题

c++ - Libudev 和 AT 命令

crash - SFML sf::RenderWindow::createWindow()使程序崩溃

C++指针不起作用