如何复制一个非平凡的C++联合体?

4

我会尝试转换以下数据结构:

template<typename ValueT, typename ChildT>
class MyUnion 
{
public:
    MyUnion() : mChild(NULL) {}
private:
    union {
        ChildT* mChild;
        ValueT* mValue;
    };
};

ValueT 可以是POD类型(如intfloat等)和非平凡类型,比如Vec3std::string,这也是它最初被实现为指向动态分配内存的指针的原因。然而,使用c++11,我们现在可以直接在类中存储该值。我要的结果是:

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或类似的方法 – 现在我仍然可以采用这种方式并期望得到正确的结果吗?


1
一般情况下,您不能使用 memcpy。这正是使得像 std::string 这样的类“非平凡”的原因(确切地说是不可平凡地可复制)。您需要某种方式来知道联合成员当前处于活动状态,并相应地编写您的复制构造函数。或者,使用类似 boost::variant 的东西。 - Igor Tandetnik
@XerenNarcy 问题不在于如何复制成员,而在于知道要复制哪个成员。 - Igor Tandetnik
@IgorTandetnik 这两者都有 - 复制联合体需要确定你正在使用哪个元素以及如何复制它。 OP 问到了 memcpy,在这方面我给出了一个小提示,让他看一下被复制的对象(你更直接)。话虽如此,是的 - MyUnion 需要另一个成员来跟踪您对联合体的操作,例如 enum class 是一种快速的方法。 - Xeren Narcy
1
你可以查看例如expected<T>实现中的操作,可以在任何可用的开源实现中找到。例如在这里:https://github.com/martinmoene/expected-lite/blob/master/include/nonstd/expected.hpp 主要的问题是,当你想让联合被占用其他类型时,你应该显式地使用放置new进行反初始化和重新初始化。你应该提供一个小型辅助类来封装它,这至少是大多数实现所做的。 - Chris Beck
1
如果Vec3是平凡可复制的,那么隐式定义的复制构造函数应该可以工作。如果它不起作用,那么Vec3(无论它是什么)实际上不是平凡可复制的,因此不能使用memcpy正确地复制。 - Igor Tandetnik
显示剩余2条评论
2个回答

5
首先,如果mValue是指向动态分配内存的指针,那么该类的默认复制构造函数非常不安全,除非您愿意泄漏内存。
因为两份拷贝都是相同的,不存在共享指针,那么谁来负责删除对象呢?所以我认为您只能泄漏它。(也许您有一些“管理器”类?但那样的话,您现在不会问如何按值在联合中存储它了。所以,tsk tsk泄漏了:p)
在大多数情况下,您需要存储一个附加的标志,告诉您当前初始化的成员是哪个。然后它被称为“带标记的联合”,因为有切实的信息可以用来区分其中的两个状态。
我将提供一个最小版本,假设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即可。它将占用更多内存来表示对象,但这很少有影响,而且可能更易于理解/更易于您的团队维护。


我只展示了我想要改变的代码主要部分--之前没有内存泄漏,所以不用担心 :) 我的实现结果与你提供的类似,但是我可能会从你那里偷几个技巧!然而,感觉你错过了最重要的一点:目标是不需要使用 new 分配堆内存。除此之外,我完全同意你的解决方案。 - pingul
好的,我的代码没有使用new。如果你不想让ChildT成为指针,那么就像我示例中的ValueT一样将其变成值类型。 - Chris Beck
哦,我对事情的运作方式感到困惑。new (&mValue) ValueT(std::forward<Args>(args)...);是什么意思? - pingul
2
这是放置 new。它在位置 mValue 精确构造一个新的 ValueT。https://isocpp.org/wiki/faq/dtors#placement-new - Chris Beck

0

不,你不能使用memcpy复制非平凡可复制的东西 - 而std::string显然不是。

此外,要访问此联合体的非平凡成员,您必须首先对其调用放置new运算符 - 否则,它的构造函数将不会被调用,它将保持未初始化状态。

我基本上认为在联合体中使用非平凡类型通常是一种可疑的做法,但并不是每个人都同意我的看法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接