繁体   English   中英

如何复制一个非平凡的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( 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或类似的东西 - 我现在可以使用相同的,并期望正确的结果?

首先,如果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边界或其他东西,有时是个好主意

在很多情况下使用union是一个不必要的优化,你只需要一个struct就可以了。 代表对象需要更多的内存,但这很少重要,并且可能更容易理解/更容易让团队维护。

不,你不能memcpy那些不易于复制的东西 - 而std::string肯定不是。

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

我基本上发现在工会中使用非平凡类型通常是一种可疑的做法,但并不是每个人都同意我的看法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM