c++ - 如何在从模板类继承时应用 move 语义

标签 c++ c++11 templates move-semantics

当我运行下面的代码时,它会产生以下输出, 第一部分直接使用模板, 第二个使用从模板派生的类。 派生类中不调用 move 语义(粗体显示)

Template Dummy:初始化构造函数
Template Dummy:初始化构造函数
模板虚拟:空构造函数
模板虚拟:空构造函数
模板虚拟:+运算符
模板虚拟: move 赋值
2

Template Dummy:初始化构造函数
Template Dummy:初始化构造函数
模板虚拟:空构造函数
模板虚拟:空构造函数
模板虚拟:+运算符
Template Dummy:复制构造函数
Template Dummy:复制作业
2

我认为原因很明确——命名一个参数会将参数变成一个左值,因此模板接收一个左值并调用一个复制构造函数。

问题是在这种情况下如何强制 move 语义?

  #include <iostream>

  using namespace std;

  template <typename T> class Dummy {

  public:

    T val;

    Dummy& operator=(const Dummy& d){
      val = d.val;
      cout << "Template Dummy: copy assignment\n" ;
      return *this;
    }

    Dummy operator+(const Dummy &d) {
      Dummy res;
      res.val = val + d.val;
      cout << "Template Dummy: +  operator\n" ;
      return res;
    }

    // constructors
    Dummy() {
      val = 0;
      cout << "Template Dummy: empty constructor\n" ;
    }

    Dummy(const T v) {
      val = v;
      cout << "Template Dummy: initializing constructor\n" ;
    }

    Dummy(const Dummy &d) {
      val = d.val;
      cout << "Template Dummy: copy constructor\n" ;
    }

    // move semantics
    Dummy(const Dummy&& d) {
      val = d.val;
      cout << "Template Dummy: move constructor\n" ;
    }

    Dummy& operator=(const Dummy&& d){
      val = d.val;
      cout << "Template Dummy: move assignment\n" ;
      return *this;
    }
  };

  class FloatDummy : public Dummy<float> {
  public:

      FloatDummy& operator=(const FloatDummy& d){
        Dummy<float>::operator=(d);
        return *this;
      }

      FloatDummy operator+(const FloatDummy &d) {
        return (FloatDummy) Dummy<float>::operator+(d);
      }

      // constructors
      FloatDummy() : Dummy<float>() {};
      FloatDummy(float v) : Dummy<float>(v) {}
      FloatDummy(const FloatDummy &d) : Dummy<float>(d) {}
      FloatDummy(const Dummy<float> &d) : Dummy<float>(d) {}

      // move semantics
      FloatDummy(const FloatDummy&& d) : Dummy<float>(d) {}

      FloatDummy& operator=(const FloatDummy&& d){

      // here d is already an lvalue because it was named
      // thus the template invokes a copy assignment    
      Dummy<float>::operator=(d);
      return *this;
    }
  };

  int main() {
    Dummy<float> a(1), b(1);
    Dummy<float> c;
    c = a + b;
    cout << c.val << '\n';;

    FloatDummy d(1), e(1);
    FloatDummy f;
    f = d + e;
    cout << f.val << '\n';
  }

最佳答案

您不能从 const& move .要修复,只需删除 const来自您所有的 move 操作:

FloatDummy(FloatDummy&& d) : Dummy<float>(d) {}
//         ^^^^^^^^^^^^ no longer FloatDummy const&&

FloatDummy& operator=(FloatDummy&& d) { /*...*/ }
//                    ^^^^^^^^^^^^ ditto

move 的本质是取得资源的所有权,如果您不能修改您的源,您如何取得某些东西的所有权?因此, move 操作不适用于 const默认类型。

您需要对 Dummy 执行相同的操作类。


您需要调用std::move()d到右值引用:即使d的参数类型是右值引用,在函数内部 d几乎被认为是左值引用。这意味着当您将它传递给实际使用它的任何对象时,您仍然需要将它转换为右值引用(std::move() 的全部意义)。

在您的情况下,将其传递给 Dummy<float> 是正确的的构造函数或 Dummy<float>::operator= :

FloatDummy(FloatDummy&& d) : Dummy<float>(std::move(d)) {}
//                                        ^^^^^^^^^ calling std::move()
//                                                  otherwise copy ctor overload is chosen

FloatDummy& operator=(FloatDummy&& d)
{
    Dummy<float>::operator=(std::move(d));
    //                      ^^^^^^^^^ ditto
    return *this;
}

关于c++ - 如何在从模板类继承时应用 move 语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195656/

相关文章:

c++ - 如何在 Win32 上加速具有大纹理的屏幕外 OpenGL 渲染?

c++ - 没有运算符==的类相等性检查

c++ - 使用空参数包函数参数调用初始化列表中的函数

c++ - 为什么模板编译这么慢?

c++ - 如何计算网格指数

c++ - 如果存在 boost 则有条件编译

c++ - std::atomic_flag 静态初始化线程在 Visual Studio 2012 中是否安全?

C++11 可变类处理程序管理器

c++ - 具有非推导上下文的部分特化排序

c++ - 无法在 Windows 7 64 位上使用 STLsoft-1.9.124 编译 pantheios 1.0.0-beta216