0

我想继承一个模板类,并使用“使用”来继承它的构造函数。但是当我调用移动构造函数时,“没有匹配的构造函数”失败

#include <iostream>

template <typename ARG>
class TBase {
 public:
  TBase() {}
  TBase(TBase<ARG>&& t) {}

 private:
  ARG arg;
};

class Int : public TBase<int> {
 public:
  using TBase<int>::TBase;
};

int main() {
  TBase<int> t1;
  Int t2(std::move(t1));
  return 0;
}

构建结果

 In function 'int main()':
20:23: error: no matching function for call to 'Int::Int(std::remove_reference<TBase<int>&>::type)'
20:23: note: candidates are:
13:7: note: Int::Int()
13:7: note:   candidate expects 0 arguments, 1 provided
13:7: note: Int::Int(Int&&)
13:7: note:   no known conversion for argument 1 from 'std::remove_reference<TBase<int>&>::type {aka TBase<int>}' to 'Int&&'
4

1 回答 1

1

好吧,这个问题很容易解释:

Default-、Copy- 和 Move- 是特殊的。它们不是通过继承 ctor 继承的。有关详细信息,请在此处阅读有关继承构造函数的更多信息。

因此,编写您自己的接受基类实例的 ctor。不应该太难,因为您尝试简单地继承 ctors。

于 2018-04-19T02:27:19.557 回答