如何复制一个非平凡的 C++ 联合?
How to copy a non-trivial c++ union?
我正在尝试转换以下数据结构:
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
MyUnion() : mChild(NULL) {}
private:
union {
ChildT* mChild;
ValueT* mValue;
};
};
ValueT
可以是 POD(int
、float
等)和重要的东西,如 Vec3
、std::string
,这就是原因它最初被实现为指向动态分配内存的指针。但是,使用 c++11,我们现在可以将值直接存储在 class 中。我正在寻找的结果是这样的:
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
MyUnion() : mChild(NULL) {}
private:
union {
ChildT* mChild;
ValueT mValue;
};
};
改这个会让编译器抱怨缺少拷贝构造函数,所以我想实现
MyUnion(const MyUnion& other);
MyUnion& operator=(const MyUnion& other);
理想情况下移动构造函数也是如此。以前编译器为我实现了这些。有了 POD,我可以做一个 memcpy
或类似的东西——我现在可以使用相同的东西并期待正确的结果吗?
不,您不能 memcpy
不能简单复制的东西 - std::string
当然不能。
此外,要访问此联合的非平凡成员,您必须首先对其调用放置新运算符 - 否则,将不会调用它的构造函数,并且它将保持未初始化状态。
我基本上发现在联合中使用非平凡类型通常是一种可疑的做法,但并不是每个人都同意我的看法。
首先,如果 mValue
是一个指向动态分配内存的指针,那么这个 class 的默认复制构造函数是非常不安全的,除非你乐于泄漏内存。
因为,哪个副本负责删除对象?它们看起来完全相同,并且没有共享指针。所以我假设你刚刚泄露了它。 (也许你有一些 "manager" class?但是你现在不会问如何在联合中按值存储它,对吗?所以,tsk tsk 泄漏 :p)
在大多数情况下,您想要的是存储一个额外的标志,它告诉您哪个成员当前已初始化。然后它被称为 "discriminated union",因为您可以使用有形信息来区分它处于两种状态中的哪一种。
假设ValueT
是,我将给出一个可复制和可移动的最小版本。
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
// Accessors, with ref qualifiers.
bool have_value() const { return mHaveValue; }
ValueT & get_value() & { return mValue; }
ValueT && get_value() && { return std::move(mValue); }
ValueT const & get_value() const & { return mValue; }
ChildT * & get_child() & { return mChild; }
ChildT * && get_child() && { return mChild; }
ChildT * const & get_child() const & { return mChild; }
// Constructors. Default, copy, and move.
MyUnion() {
this->init_child(nullptr);
}
MyUnion(const MyUnion & other) {
if (other.have_value()) {
this->init_value(other.get_value());
} else {
this->init_child(other.get_child());
}
}
MyUnion(MyUnion && other) {
if (other.have_value()) {
this->init_value(std::move(other.get_value()));
} else {
this->init_child(std::move(other.get_child()));
}
}
// Move assignment operator is easier, do that first.
// Note that if move ctors can throw, you can get a UB with this.
// So in most correct code, you would either ban such objects from
// appearing in your union, or try to make backup copies in order
// to recover from the exceptions. In this code, I will just
// assume that moving your object doesn't throw.
// In that case, it's just deinitialize self, then use code from
// move ctor.
MyUnion & operator = (MyUnion && other) {
this->deinitialize();
if (other.have_value()) {
this->init_value(std::move(other.get_value()));
} else {
this->init_child(std::move(other.get_child()));
}
return *this;
}
// Copy ctor basically uses "copy and swap", but instead of
// swap, we use move assignment. This is exception safe, if
// move assignment is.
MyUnion & operator = (const MyUnion & other) {
MyUnion temp{other};
*this = std::move(temp);
return *this;
}
// Dtor simply calls deinitialize.
~MyUnion() { this->deinitialize(); }
private:
union {
ChildT* mChild;
ValueT mValue;
};
bool mHaveValue;
// these next three methods are private helpers for you.
// the users of your class should not mess with these things,
// or UB is quite likely!
void deinitialize() {
if (mHaveValue) {
mValue.~ValueT();
} else {
// pointer type has no dtor. But if you actually *own* the child,
// then you should call delete here I guess.
// Or, replace with `std::unique_ptr` and call
// that guys dtor. RAII is your friend, you can thank me later.
}
}
// Initialize the value, using perfect forwarding.
// Only do this if mValue is not currently initialized!
template <typename ... Args>
void init_value(Args && ... args) {
new (&mValue) ValueT(std::forward<Args>(args)...);
mHaveValue = true;
}
// Here, mChild is a raw pointer, so it doesn't make sense to
// make a similar initialization. But if you change it to be an RAII
// object, then you should probably do a similar pattern to above,
// with perfect forwarding.
void init_child(ChildT * c) {
mChild = c;
mHaveValue = false;
}
};
注意:您通常不需要像这样推出自己的受歧视工会。很多时候,最好使用一些现有的库,如 boost::variant
或评论中提到的 expected
类型之一。但是,像这样建立自己的受歧视的小联盟是
- 没那么难
- 很好的运动
- 如果它需要出现在 API 边界或其他地方
,有时是个好主意
在很多情况下,使用联合根本就是一种不必要的优化,只使用 struct
就可以了。表示对象将占用更多内存,但这无关紧要,并且您的团队可能更容易理解/更容易维护。
我正在尝试转换以下数据结构:
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
MyUnion() : mChild(NULL) {}
private:
union {
ChildT* mChild;
ValueT* mValue;
};
};
ValueT
可以是 POD(int
、float
等)和重要的东西,如 Vec3
、std::string
,这就是原因它最初被实现为指向动态分配内存的指针。但是,使用 c++11,我们现在可以将值直接存储在 class 中。我正在寻找的结果是这样的:
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
MyUnion() : mChild(NULL) {}
private:
union {
ChildT* mChild;
ValueT mValue;
};
};
改这个会让编译器抱怨缺少拷贝构造函数,所以我想实现
MyUnion(const MyUnion& other);
MyUnion& operator=(const MyUnion& other);
理想情况下移动构造函数也是如此。以前编译器为我实现了这些。有了 POD,我可以做一个 memcpy
或类似的东西——我现在可以使用相同的东西并期待正确的结果吗?
不,您不能 memcpy
不能简单复制的东西 - std::string
当然不能。
此外,要访问此联合的非平凡成员,您必须首先对其调用放置新运算符 - 否则,将不会调用它的构造函数,并且它将保持未初始化状态。
我基本上发现在联合中使用非平凡类型通常是一种可疑的做法,但并不是每个人都同意我的看法。
首先,如果 mValue
是一个指向动态分配内存的指针,那么这个 class 的默认复制构造函数是非常不安全的,除非你乐于泄漏内存。
因为,哪个副本负责删除对象?它们看起来完全相同,并且没有共享指针。所以我假设你刚刚泄露了它。 (也许你有一些 "manager" class?但是你现在不会问如何在联合中按值存储它,对吗?所以,tsk tsk 泄漏 :p)
在大多数情况下,您想要的是存储一个额外的标志,它告诉您哪个成员当前已初始化。然后它被称为 "discriminated union",因为您可以使用有形信息来区分它处于两种状态中的哪一种。
假设ValueT
是,我将给出一个可复制和可移动的最小版本。
template<typename ValueT, typename ChildT>
class MyUnion
{
public:
// Accessors, with ref qualifiers.
bool have_value() const { return mHaveValue; }
ValueT & get_value() & { return mValue; }
ValueT && get_value() && { return std::move(mValue); }
ValueT const & get_value() const & { return mValue; }
ChildT * & get_child() & { return mChild; }
ChildT * && get_child() && { return mChild; }
ChildT * const & get_child() const & { return mChild; }
// Constructors. Default, copy, and move.
MyUnion() {
this->init_child(nullptr);
}
MyUnion(const MyUnion & other) {
if (other.have_value()) {
this->init_value(other.get_value());
} else {
this->init_child(other.get_child());
}
}
MyUnion(MyUnion && other) {
if (other.have_value()) {
this->init_value(std::move(other.get_value()));
} else {
this->init_child(std::move(other.get_child()));
}
}
// Move assignment operator is easier, do that first.
// Note that if move ctors can throw, you can get a UB with this.
// So in most correct code, you would either ban such objects from
// appearing in your union, or try to make backup copies in order
// to recover from the exceptions. In this code, I will just
// assume that moving your object doesn't throw.
// In that case, it's just deinitialize self, then use code from
// move ctor.
MyUnion & operator = (MyUnion && other) {
this->deinitialize();
if (other.have_value()) {
this->init_value(std::move(other.get_value()));
} else {
this->init_child(std::move(other.get_child()));
}
return *this;
}
// Copy ctor basically uses "copy and swap", but instead of
// swap, we use move assignment. This is exception safe, if
// move assignment is.
MyUnion & operator = (const MyUnion & other) {
MyUnion temp{other};
*this = std::move(temp);
return *this;
}
// Dtor simply calls deinitialize.
~MyUnion() { this->deinitialize(); }
private:
union {
ChildT* mChild;
ValueT mValue;
};
bool mHaveValue;
// these next three methods are private helpers for you.
// the users of your class should not mess with these things,
// or UB is quite likely!
void deinitialize() {
if (mHaveValue) {
mValue.~ValueT();
} else {
// pointer type has no dtor. But if you actually *own* the child,
// then you should call delete here I guess.
// Or, replace with `std::unique_ptr` and call
// that guys dtor. RAII is your friend, you can thank me later.
}
}
// Initialize the value, using perfect forwarding.
// Only do this if mValue is not currently initialized!
template <typename ... Args>
void init_value(Args && ... args) {
new (&mValue) ValueT(std::forward<Args>(args)...);
mHaveValue = true;
}
// Here, mChild is a raw pointer, so it doesn't make sense to
// make a similar initialization. But if you change it to be an RAII
// object, then you should probably do a similar pattern to above,
// with perfect forwarding.
void init_child(ChildT * c) {
mChild = c;
mHaveValue = false;
}
};
注意:您通常不需要像这样推出自己的受歧视工会。很多时候,最好使用一些现有的库,如 boost::variant
或评论中提到的 expected
类型之一。但是,像这样建立自己的受歧视的小联盟是
- 没那么难
- 很好的运动
- 如果它需要出现在 API 边界或其他地方 ,有时是个好主意
在很多情况下,使用联合根本就是一种不必要的优化,只使用 struct
就可以了。表示对象将占用更多内存,但这无关紧要,并且您的团队可能更容易理解/更容易维护。