简体   繁体   English

使用已删除的 function 'std::pair <const std::pair<std::basic_string<char> , std::basic_string<char> ></char></const>

[英]use of deleted function 'std::pair<const std::pair<std::basic_string<char>, std::basic_string<char> >

I am using std::pair<string, string> and make_pair() to create a std::map using this pair of strings.我正在使用std::pair<string, string>make_pair()创建一个std::map使用这对字符串。 But I am getting the following compilation error:但是我收到以下编译错误:

/opt/rh/devtoolset-7/root/usr/include/c++/7/ext/new_allocator.h:140:22: error: use of deleted function 'std::pair<const std::pair<std::basic_string<char>, std::basic_string<char> >, firmware_data>::~pair()'
  destroy(_Up* __p) { __p->~_Up(); }

The declarations I have done is as follows:我所做的声明如下:

typedef std::pair<std::string, string> FirmwareKey_t;
typedef std::map<FirmwareKey_t, firmware_data_t> FirmwareDataMap_t;

Where firmware_data_t is a struct object, defined as:其中firmware_data_t是一个结构体 object,定义为:

typedef struct firmware_data {
  string name;
  value_t abc; // value_t is of union type
  bool configurable;
  update_status_t def; //update_status_t is of enum type

} firmware_data_t;

Union Declaration:联盟宣言:

typedef union value {
  string string_val;
  bool boolean_val;
  int64_t int_val;
  uint uint_val;
} value_t;

The error is saying that the map 's value_type (aka std::pair<const Key, T> ) has a deleted destructor.错误是说mapvalue_type (又名std::pair<const Key, T> )具有已删除的析构函数。

For that to happen, either the map::key_type or map::mapped_type type (in this case, FirmwareKey_t and firmware_data_t , respectively) has a deleted destructor.为此, map::key_typemap::mapped_type类型(在本例中分别为FirmwareKey_tfirmware_data_t )具有已删除的析构函数。 That is clearly not the case for FirmwareKey_t , so it must be firmware_data_t , which contains a value_t member of union type. FirmwareKey_t显然不是这种情况,因此它必须是firmware_data_t ,它包含union类型的value_t成员。 According to cppreference :根据 cppreference

(since C++11) If a union contains a non-static data member with a non-trivial special member function (copy/move constructor, copy/move assignment, or destructor ), that function is deleted by default in the union and needs to be defined explicitly by the programmer. (C++11 起)如果联合包含一个非静态数据成员和一个非平凡的特殊成员 function(复制/移动构造函数、复制/移动赋值或析构函数),则 function 在联合中默认被删除并且需要由程序员明确定义。

Which means one of the fields in your union is of a non-trivial type that has a destructor defined, and so the union itself has a deleted destructor, and thus any type that uses the union as a data member also has a deleted destructor, and so on.这意味着union中的一个字段是具有定义的析构函数的非平凡类型,因此union本身具有已删除的析构函数,因此任何使用union作为数据成员的类型也具有已删除的析构函数,等等。

And, in fact, that field in your union is the string string_val field.而且,实际上, union中的那个字段是string string_val字段。

So, you need to fix your union .所以,你需要修复你的union Since std::string requires destruction, you need to explicitly define a destructor for your union to call std::string 's destructor, but only when string_val is the active field of the union , eg:由于std::string需要销毁,因此您需要为union显式定义析构函数以调用std::string的析构函数,但前提是string_valunion的活动字段,例如:

union value_t {
  string string_val;
  bool boolean_val;
  int64_t int_val;
  uint uint_val;

  ~value_t() {
    if (some condition indicating string_val is active) {
      string_val.~string();
    }
  }
};.

You will also need to define a copy costructor and copy assignment operator for your union , too.您还需要为您的union定义一个复制构造函数和复制赋值运算符。

That being said, a better solution is to use std::variant instead of a union .也就是说,更好的解决方案是使用std::variant而不是union std::variant is a type-safe union that handles these details automatically for you. std::variant是一个类型安全的union ,可以自动为您处理这些细节。

using value_t = variant<string, bool, int64_t, uint>;

暂无
暂无

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

相关问题 从std :: map转换 <std::basic_string<char> ,std :: pair - conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat 无法从&#39;const std :: __ 1 :: basic_string转换 <char> 到&#39;std :: __ 1 :: basic_string <char> *” - No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *' 转换 std::basic_string<Char> 串起来 - Convert std::basic_string<Char> to string 从&#39;void&#39;转换为非标量类型&#39;std :: pair <std::basic_string<char, std::char_traits<char> - conversion from ‘void’ to non-scalar type ‘std::pair<std::basic_string<char, std::char_traits<char> std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:无法匹配'(const std :: basic_string <char>)()' - error: no match for call to '(const std::basic_string<char>) ()' 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:不匹配调用 (std::string{aka std::basic_string<char> })(const char[5])&#39; - Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])' 是否可以使用std :: basic_string作为char *的包装? - Is it possible to use std::basic_string as a wrapper around char*? 没有匹配的函数来调用&#39;std :: basic_string <char> :: basic_string C ++ - no matching function for call to 'std::basic_string<char>::basic_string c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM