联盟成员有一个非平凡的复制构造函数 [英] Union member has a non-trivial copy constructor

查看:339
本文介绍了联盟成员有一个非平凡的复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的联合:

  union {
int intValue;
double doubleValue;
std :: string stringValue;
void * pointerValue;
} values;

当我编译它,我得到这个错误信息(是的,我做了 #include< string> ):

  ./ Value.hh:19:19:error :union member'stringValue'有一个非平凡的拷贝构造函数
std :: string stringValue;
^
/Developer/SDKs/MacOSX10.7.sdk//usr/include/c++/4.2.1/bits/basic_string.h:434:7:注意:因为
类型' std :: basic_string< char>'有一个用户声明的复制构造函数
basic_string(const basic_string& __str);
^

我使用此命令编译它:

  $ clang ++ * .cc -isysroot /Developer/SDKs/MacOSX10.7.sdk/ -shared 

如何在联合中使用 std :: string



一个union结合了两个功能:存储一个对象的能力,这个对象可以是的选择数量的类型,以及在这些类型之间有效(和实现定义)转换的能力。你可以把一个整数,看看它的表示为双。



由于并集必须支持这两个功能的 (以及一些其他原因,一个),工会阻止你做某些事情。也就是说,你不能把活的对象。任何生存足够需要非默认拷贝构造函数(除了许多其他限制)的对象不能是联合的成员。



毕竟,联合对象并不真正具有实际存储哪种类型的数据的概念。它不存储一种类型的数据;它同时存储全部。它取决于你能够钓到正确的类型。那么如何能合理地将一个联合值复制到另一个联合值中呢?



联合的成员必须是POD(plain-old-data)类型。虽然C ++ 11放松了这些规则,但对象仍然必须有一个默认的(或者其他微不足道的)拷贝构造函数。和 std :: string 的复制构造函数是不平凡的。



你可能想要的是一个 boost :: variant 。这是一个可以存储多个可能类型的对象,就像一个联合。与联合不同,它是类型安全的。它因此知道实际在联盟中是什么;它因此能够复制自身,否则表现得像一个常规的C ++对象。


I have a union that looks like this:

union {
  int intValue;
  double doubleValue;
  std::string stringValue;
  void *pointerValue;
} values;

When I compile it, I get this error message (yes, I did #include <string>):

./Value.hh:19:19: error: union member 'stringValue' has a non-trivial copy constructor                 
      std::string stringValue;                                                                         
                  ^                                                                                    
/Developer/SDKs/MacOSX10.7.sdk//usr/include/c++/4.2.1/bits/basic_string.h:434:7: note: because         
      type 'std::basic_string<char>' has a user-declared copy constructor                              
      basic_string(const basic_string& __str);                                                         
      ^

I compile it using this command:

$ clang++ *.cc -isysroot /Developer/SDKs/MacOSX10.7.sdk/ -shared

How can I use an std::string in a union?

解决方案

You cannot.

A union combines two pieces of functionality: the ability to store an object which may be of a select number of types, and the ability to effectively (and implementation-defined) convert between those types. You could put an integer in and look at its representation as a double. And so forth.

Because a union must support both of these pieces of functionality (and for a few other reasons, like being able to construct one), a union prevents you from doing certain things. Namely, you cannot put "live" objects in them. Any object that is "living" enough that it needs a non-default copy constructor (among many other restrictions) cannot be a member of a union.

After all, a union object does not really have the concept of which type of data it actually stores. It does not store one type of data; it stores all of them, at the same time. It is up to you to be able to fish the right type out. So how could it reasonably copy one union value into another?

Members of a union must be a POD (plain-old-data) type. And while C++11 does loosen those rules, objects still must have a default (or otherwise trivial) copy constructor. And std::string's copy constructor is non-trivial.

What you likely want is a boost::variant. That is an object that can store a number of possible types, just like a union. Unlike a union however, it is type-safe. It therefore knows what is actually in the union; it is therefore able to copy itself and otherwise behave like a regular C++ object.

这篇关于联盟成员有一个非平凡的复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆