递归类型的判别联合
Discriminated union of recursive type
我在这里阅读了 C++ 中的可区分联合:http://www.stroustrup.com/C++11FAQ.html#unions
如果我想创建递归类型的并集怎么办?
例如,考虑以下内容:
class Obj {
enum class Type { kInt, kVec } type;
union {
int i;
std::vector<Obj> v;
};
Obj& operator=(const Obj& o) { ... }
};
在这种情况下,由于递归引用,编译器会抱怨试图使用不完整的对象 Obj
。如何以干净的方式解决这个问题?
谢谢
我现在没时间玩这个,但这可能对你有帮助:
编译器抱怨在您的示例中使用了不完整的对象,因为它此时不知道 Obj 的大小。
尝试使用原始 pointer/unique_ptr/shared_ptr 作为向量值类型。
这里没有类型递归的问题:vector
内部状态只有指向valute_type的指针成员。
我认为,如果您尝试遵循 Whosebug 规则“How to create a minimal, complete and verifiable example,您肯定会找到答案。
在您在评论中提供的示例代码中,有许多编译器抱怨的错误:Obj 的复制构造函数被删除,析构函数也被删除,然后赋值运算符是私有的等等...
我只是一一修正了错误并得到了正确的代码示例,这是您可能找到的答案:
#include <iostream>
#include <vector>
using namespace std;
class Obj {
enum class Type { kInt, kVec } type;
union {
int i;
vector<Obj> v;
};
public:
Obj(const Obj& o):type{o.type}{
if (o.type==Type::kVec)
new (&v) vector<Obj>(o.v);
else
i=o.i;
}
Obj(Obj&& o):type{o.type}{
if (o.type==Type::kVec)
new (&v) vector<Obj>(std::move(o.v));
else
i=o.i;
}
~Obj(){ if (type==Type::kVec) v.~vector<Obj>();}
Obj& operator=(const Obj&o){
if (o.type==Type::kVec && type==Type::kVec){
v=o.v;
return *this;
}
if (type == Type::kVec) {
v.~vector();
}
switch (o.type) {
case Type::kInt:
i = o.i;
break;
case Type::kVec:
new (&v) vector<Obj>(o.v);
break;
}
type=o.type;
return *this;
}
};
int main() {
return 0;
}
我在这里阅读了 C++ 中的可区分联合:http://www.stroustrup.com/C++11FAQ.html#unions
如果我想创建递归类型的并集怎么办?
例如,考虑以下内容:
class Obj {
enum class Type { kInt, kVec } type;
union {
int i;
std::vector<Obj> v;
};
Obj& operator=(const Obj& o) { ... }
};
在这种情况下,由于递归引用,编译器会抱怨试图使用不完整的对象 Obj
。如何以干净的方式解决这个问题?
谢谢
我现在没时间玩这个,但这可能对你有帮助:
编译器抱怨在您的示例中使用了不完整的对象,因为它此时不知道 Obj 的大小。
尝试使用原始 pointer/unique_ptr/shared_ptr 作为向量值类型。
这里没有类型递归的问题:vector
内部状态只有指向valute_type的指针成员。
我认为,如果您尝试遵循 Whosebug 规则“How to create a minimal, complete and verifiable example,您肯定会找到答案。
在您在评论中提供的示例代码中,有许多编译器抱怨的错误:Obj 的复制构造函数被删除,析构函数也被删除,然后赋值运算符是私有的等等...
我只是一一修正了错误并得到了正确的代码示例,这是您可能找到的答案:
#include <iostream>
#include <vector>
using namespace std;
class Obj {
enum class Type { kInt, kVec } type;
union {
int i;
vector<Obj> v;
};
public:
Obj(const Obj& o):type{o.type}{
if (o.type==Type::kVec)
new (&v) vector<Obj>(o.v);
else
i=o.i;
}
Obj(Obj&& o):type{o.type}{
if (o.type==Type::kVec)
new (&v) vector<Obj>(std::move(o.v));
else
i=o.i;
}
~Obj(){ if (type==Type::kVec) v.~vector<Obj>();}
Obj& operator=(const Obj&o){
if (o.type==Type::kVec && type==Type::kVec){
v=o.v;
return *this;
}
if (type == Type::kVec) {
v.~vector();
}
switch (o.type) {
case Type::kInt:
i = o.i;
break;
case Type::kVec:
new (&v) vector<Obj>(o.v);
break;
}
type=o.type;
return *this;
}
};
int main() {
return 0;
}