提问人:niran90 提问时间:10/12/2021 最后编辑:niran90 更新时间:10/12/2021 访问量:135
使用奇怪的重复模板模式 (CRTP) 在抽象基类中实现赋值运算符
Implementing the assignment operator in an abstract base class using the curiously recurring template pattern (CRTP)
问:
我正在为静态/动态数组编写一个基于 CRTP 的抽象基类。我打算在基类中放置尽可能多的方法,以便在派生类中没有代码重复。我已经让索引运算符工作了,但我正在为赋值 (=) 运算符而苦苦挣扎。
/** Array base class. */
template <class t_derived_class, class data_type>
class ArrayBase
{
private:
t_derived_class& Derived;
public:
/** Default constructor. */
ArrayBase() : Derived(static_cast<t_derived_class&>(*this)) {}
/** Pure virtual desctructor. */
virtual ~ArrayBase() {};
/** Index operator overloads. */
data_type& operator[](const std::size_t _index)
{
return *(Derived.begin() + _index);
}
const data_type& operator[](const std::size_t _index) const
{
return *(Derived.begin() + _index);
}
/** Assignment operator overloads. */
t_derived_class& operator=(const t_derived_class& _other_derived)
{
for(std::size_t i = 0; i < Derived.size(); ++i) Derived[i] = _other_derived[i];
return Derived;
}
};
/** Static array class. */
template <class data_type, int array_size>
class StaticArray : public std::array<data_type, array_size>, public ArrayBase<StaticArray<data_type, array_size>, data_type>
{
using Base = ArrayBase<StaticArray<data_type, array_size>, data_type>;
friend Base;
public:
/** Default constructor. */
StaticArray() : std::array<data_type, array_size>() {}
/** Default destructor. */
~StaticArray() = default;
using Base::operator[];
using Base::operator=;
};
/** Dynamic array class. */
template <class data_type>
class DynamicArray : public std::vector<data_type>, public ArrayBase<DynamicArray<data_type>, data_type>
{
// ...
};
int main()
{
StaticArray<double, 3> array1;
array1[0] = 1.0;
array1[1] = 2.0;
array1[2] = 3.0;
// This code compiles
StaticArray<double, 3> array2 = array1;
// This code does not compile
array2 = array1;
return 0;
}
当我使用上述赋值运算符时,我的IDE(CLion)给了我以下错误:
Object of type 'StaticArray<Apeiron::Float, 3>' (aka 'StaticArray<double, 3>') cannot be assigned because its copy assignment operator is implicitly deleted
编译器错误包括:
error: ‘Apeiron::StaticArray<double, 3>& Apeiron::StaticArray<double, 3>::operator=(const Apeiron::StaticArray<double, 3>&)’ cannot be overloaded with ‘t_derived_class& Apeiron::ArrayBase<t_derived_class, data_type>::operator=(const t_derived_class&) [with t_derived_class = Apeiron::StaticArray<double, 3>; data_type = double]’
90 | class StaticArray : public std::array<data_type, array_size>, public ArrayBase<StaticArray<data_type, array_size>, data_type>
| ^~~~~~~~~~~
Array.h:62:20: note: previous declaration ‘t_derived_class& Apeiron::ArrayBase<t_derived_class, data_type>::operator=(const t_derived_class&) [with t_derived_class = Apeiron::StaticArray<double, 3>; data_type = double]’
62 | t_derived_class& operator=(const t_derived_class& _other_derived)
谁能建议我如何让它工作?
答:
2赞
康桓瑋
10/12/2021
#1
由于 ur 的成员变量是引用的,因此隐式声明的变量将被自动删除。ArrayBase
ArrayBase::operator=
另一种方法是删除成员变量并直接使用 help 函数来获取派生类的引用:
template <class t_derived_class, class data_type>
class ArrayBase
{
private:
t_derived_class& Derived() noexcept {
return static_cast<t_derived_class&>(*this);
};
const t_derived_class& Derived() const noexcept {
return static_cast<const t_derived_class&>(*this);
};
public:
/** Pure virtual desctructor. */
virtual ~ArrayBase() {};
/** Index operator overloads. */
data_type& operator[](const std::size_t _index)
{
return *(Derived().begin() + _index);
}
// ...
};
评论
0赞
Jarod42
10/12/2021
gcc 仍然拒绝代码 Demo 并出现错误:“'constexpr StaticArray<float, 3>& StaticArray<float, 3>::operator=(const StaticArray<float, 3>&)' 不能用 't_derived_class& ArrayBase<t_derived_class, data_type>::operator=(const t_derived_class&) [with t_derived_class = StaticArray<float, 3>;data_type = 浮点数]'”。当其他编译器使用拼写错误而不是 时,我怀疑它没有像预期的那样运行。return Derived;
return Derived();
0赞
Jarod42
10/12/2021
但正如我也说过的,没有发现错别字,所以该方法不会被实例化,并且可以删除......(碰巧默认已经完成了这项工作)。return Derived;
Derived::operator=
0赞
康桓瑋
10/12/2021
你是对的,这里没有实例化用户定义的。operator=
上一个:算子重载的基本规则和习语是什么?
下一个:赋值运算符的返回类型
评论
DynamicArray
operator=
data_type
std::array
std::vector