class 的构造函数和复制构造函数包含具有非平凡成员的联合

Constructor and copy-constructor for class containing union with non-trivial members

我正在尝试实现一个自定义变体类型,它使用联合来存储各种不同类型的数据。在字段type_id中我打算存储union中存储的数据是什么类型的。联合包含非平凡的成员。这是我当前的实现:

struct MyVariant {
  enum { t_invalid, t_string, t_int, t_double, t_ptr, t_dictionary } type_id;
  union {
    int                             as_int;
    double                          as_double;
    std::string                     as_string;
    std::unique_ptr<int>            as_ptr;
    std::map<int, double>           as_dictionary;
  };
};

我尝试创建一个 MyVariant 的实例,如下所示:

MyVariant v;

我收到错误消息:调用隐式删除的 MyVariant 默认构造函数。所以,我尝试像下面这样手动实现构造函数:

MyVariant() : type_id{t_int}, as_int{0} {}

这给了我类似的错误消息:尝试使用已删除的函数。 接下来,我尝试实现以下构造函数:

MyVariant(int value) : type_id{t_int}, as_int{value} {}

并按如下方式构建我的实例:

MyVariant v{123};

=> 相同的错误消息:尝试使用已删除的函数

我也开始实现拷贝构造函数了,大概是这样的。但是,这当然对编译器错误没有帮助。

MyVariant::MyVariant(const MyVariant& other)
{
    type_id = other.type_id;
    switch (type_id) {
        case t_invalid:
            break;
        case t_string:
            new (&as_string) std::string();
            as_string = other.as_string;
            break;
        case t_int:
            as_int = other.as_int;
            break;
        case t_double:
            as_double = other.as_double;
            break;
        case t_ptr:
            new (&as_ptr) std::unique_ptr<int>(nullptr);
            as_ptr = std::make_unique<int>(*other.as_ptr);
            break;
        case t_dictionary:
            new (&as_dictionary) std::map<int, double>();
            // TODO: copy values from other
            break;
    }
}

我正在使用 Xcode 和 Apple LLVM 6.1 作为编译器。

主要问题是:为什么我会收到编译器错误以及我必须如何修改我的代码才能使其编译?

另一个问题是:我对构造函数和复制构造函数的实现是否正确?

你的联合有 stringunique_ptrmap 类型的数据成员,所有这些成员都有非平凡的 default/copy/move 构造函数,copy/move 赋值运算符和析构函数。因此,所有这些都为您的联盟隐式删除。

§9.5/2 [class.union]

... [ Note: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be user-provided or it will be implicitly deleted (8.4.3) for the union. —end note ]

因此您必须为您的工会手动实施这些。至少,为了能够创建 MyVariant 的实例,class 需要是可构造和可破坏的。所以你需要

MyVariant() : type_id{t_int}, as_int{0} {}
~MyVariant()
{
  switch(type_id)
  {
      case t_int:
      case t_double:
        // trivially destructible, no need to do anything
        break;
      case t_string:
        as_string.~basic_string();
        break;
      case t_ptr:
        as_ptr.~unique_ptr();
        break;
      case t_dictionary:
        as_dictionary.~map();
        break;
      case t_invalid:
        // do nothing
        break;
      default:
        throw std::runtime_error("unknown type");
  }
}

您的复制构造函数实现看起来有效,但我要做的不同之处在于,不是首先默认构造成员,然后从源对象复制,而是在 placement new 调用本身中复制构造。

MyVariant(const MyVariant& other)
{
  type_id = other.type_id;
  switch (type_id) {
      case t_invalid:
          break;
      case t_string:
          new (&as_string) auto(other.as_string);
          break;
      case t_int:
          as_int = other.as_int;
          break;
      case t_double:
          as_double = other.as_double;
          break;
      case t_ptr:
          new (&as_ptr) auto(std::make_unique<int>(*other.as_ptr));
          break;
      case t_dictionary:
          new (&as_dictionary) auto(other.as_dictionary);
          break;
  }

Live demo

请注意,如果 unique_ptr 成员处于活动状态,并且正在通过基 class 指针存储指向某个派生 class 实例的指针,那么您的复制构造函数实现将只复制基础 class 部分。

最后,除非您将此作为学习练习,否则我强烈建议您使用 Boost.Variant 而不是自己滚动。