模板化复制构造函数
Templated Copy-Constructor
>想象一下下面的例子:
#include <iostream>
#include <vector>
struct Base
{
virtual void foo() = 0;
};
struct A : public Base
{
void foo() override {};
};
struct B : public Base
{
void foo() override {};
};
template <typename T>
struct C
{
struct Element
{
int x, y, z;
bool operator==(const Element& e)
{
if (x != e.x) return false;
if (y != e.y) return false;
if (z != e.z) return false;
return true;
}
};
Base* p;
std::vector<Element> v;
C()
{
p = new T();
}
void add(int x, int y, int z)
{
Element e;
e.x = x;
e.y = y;
e.z = z;
v.push_back(e);
}
void remove(int x, int y, int z)
{
Element e;
e.x = x;
e.y = y;
e.z = z;
std::vector<Element>::iterator it = std::find(v.begin(), v.end(), e);
if (p != v.end()) v.erase(p);
}
void print()
{
for (Element e : v) std::cout << e.x << " " << e.y << " " << e.z << std::endl;
}
};
int main()
{
C<A> a;
a.add(1, 2, 3);
a.add(4, 5, 6);
a.add(7, 8, 9);
a.remove(4, 5, 6);
a.print();
return 0;
}
现在让我们在 C 中添加一个复制构造函数,以便我们可以用另一个保存另一种数据类型的 C 初始化 C(只要数据类型派生自 Base(。目标是使这成为可能:
int main()
{
C<A> a;
a.add(1, 2, 3);
a.add(4, 5, 6);
a.add(7, 8, 9);
a.remove(4, 5, 6);
a.print();
C<B> b(a); // <----- This should be possible.
return 0;
}
我试图像这样解决它:
template <typename U>
C(const C<U>& c)
{
p = new U(*c.p);
v = c.v;
}
但是我从Visual Studio收到以下2个错误:
错误 C2679 二进制"=":未找到采用类型为"const std::vector::Element,std::分配器<_Ty>>"的右侧操作数的运算符(或者没有可接受的转换(
错误 C2664 "A::A(A &&(":无法将参数 1 从"基数"转换为"常量 A &"
据我所知,std::vector 已经实现了一个赋值运算符,该运算符应该在运算符的右侧创建矢量的深层副本。
那么我做错了什么,我需要做些什么才能让它工作?
v = c.v;
这是两种完全不同的类型。
v
是std::vector<C<T>::Element>
.
c.v
std::vector<C<U>::Element>
.
鉴于不同的T
和U
类型,C<T>
和C<U>
是完全不同的类型,彼此没有关系。
C<T>::Element
和C<U>::Element
也是完全不同的类型。每个std::vector
也是如此。
C<T>::Element
和 C<U>::Element
可能具有相同的类名。它们可能具有完全相同的类成员。但它们是两种独立的类型,就像彼此不同一样class A
与class B
不同。问题不在于模板复制构造函数本身。问题是复制构造函数尝试将一种类型分配给彼此。
您需要做的是删除内部类Element
。它不依赖于模板参数。使其成为独立的顶级类。然后,C<T>
和 C<U>
都将有一个类成员v
它是相同类型的向量,并且可以将它们分配给彼此。
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 使用复制构造函数复制双精度数组
- C 无可行的构造函数复制类型的变量
- 没有可行的构造函数复制类型 'MyString' 的数组元素
- 编译时,复制构造函数/复制分配和正常功能调用优化之间是否存在任何区别
- 如何最小化调用列表构造函数(复制构造函数)的次数?
- C 11矢量构造函数复制与范围
- 我定义了一个非复制构造函数;复制构造函数还会被隐式定义吗
- 可以将构造函数复制为转换运算符
- 将基类指针的构造函数复制到子类
- C++树类:构造函数/复制/内存泄漏
- 如何制作这个在模板构造函数复制中使用类型定义的类型的模板
- 将构造函数复制为模板化的成员函数
- 绕过私有复制构造函数/复制赋值C++
- C++通过构造函数复制对象
- 复制构造函数 - 复制C++中的对象
- 将带unique_ptr的类的构造函数复制到作为成员的抽象类