2

我正在从Modern C++ Design学习基于策略的设计,我陷入了下面的一个简单示例,我试图在我的模板化策略类中使用 a std::vectorof s:std::unique_ptr

#include <memory>
#include <vector>

template <class T> struct MyPolicy {
  MyPolicy() = default;
  MyPolicy(std::size_t N) : myvec{std::vector<std::unique_ptr<T>>(N)} {
    for (std::size_t i = 0; i < N; i++)
      myvec[i].reset(new T(i));
  }

  // protected: // or, even, public:
  //   /*virtual*/ ~MyPolicy() = default;

private:
  std::vector<std::unique_ptr<T>> myvec;
};

template <class T, template <class> class Policy>
struct Shell : public Policy<T> {
  Shell() = default;
  Shell(std::size_t N) : Policy<T>(N) {}
};

int main(int argc, char *argv[]) {
  Shell<double, MyPolicy> s;
  s = Shell<double, MyPolicy>(7);

  Shell<double, MyPolicy> s2{6};
  s = std::move(s2);
  return 0;
}

上面的一切都很好。然而,问题在于,因为MyPolicy应该是继承自的,所以它的析构函数必须是virtualand public,或者非virtualand protected(至少,从书中引用)。

在上面的示例中,每当我取消注释这些行以使其成为

public:
virtual ~MyPolicy() = default;

或者

protected:
~MyPolicy() = default;

代码无法编译。我无法理解std::unique_ptr此示例中与问题相关的内容,因为类型T不完整或具有受保护/私有析构函数的东西。

我会很感激你的帮助。谢谢。

4

1 回答 1

3

声明析构函数可防止隐式声明移动构造函数和移动赋值运算符。因此,如果您声明一个虚拟析构函数并想要默认的特殊移动函数,您需要自己显式声明它们:

public:
  virtual ~MyPolicy() = default;
  MyPolicy(MyPolicy&&) = default;             //here
  MyPolicy& operator= (MyPolicy&&) = default; //and here

现场演示

于 2017-11-30T17:24:51.367 回答