0

Whenever I tried to compile this code:

union foo {
    std::string dunno;
} bar;

It gives me this bunch of errors. What's wrong with it?:

_foo.cpp:6:3: error: use of deleted function 'foo::foo()'
 } bar;
   ^
_foo.cpp:4:7: note: 'foo::foo()' is implicitly deleted because the default definition would be ill-formed:
 union foo {
       ^
_foo.cpp:5:14: error: union member 'foo::dunno' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  std::string dunno;
              ^
_foo.cpp: In function 'void __static_initialization_and_destruction_0(int, int)':
_foo.cpp:6:3: error: use of deleted function 'foo::~foo()'
 } bar;
   ^
_foo.cpp:4:7: note: 'foo::~foo()' is implicitly deleted because the default definition would be ill-formed:
 union foo {
       ^
_foo.cpp:5:14: error: union member 'foo::dunno' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  std::string dunno;
              ^
_foo.cpp: In function 'void __tcf_1()':
_foo.cpp:6:3: error: use of deleted function 'foo::~foo()'
 } bar;
   ^

Can you explain, why?

Bruce
  • 9
  • 1

2 Answers2

10

C++11 did indeed introduce the possibility to include arbitrary types in unions. However, you need to give the union all special member functions which those types have. It's nicely summarised in C++11 9.5/2:

[ 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 ]

Which means that if you want your union to have a default constructor, you must define it, something like this:

union foo {
    std::string dunno;
    foo() : dunno() {}
} bar;
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

C++03 standard forbids types with non-trivial constructor(std::string constructor is non-trivial) to be used in union. This restriction has been removed in C++11. Also you need to define a constructor if the union has member with non-trivial constructor.

Windoze
  • 321
  • 2
  • 13