4 ms·
i haven't done c++ in a million years, but huh, you can! can't have any types w/ non-trival copy constructors in a union, though, apparently, which is quite the
by dilap 4y ago
i haven't done c++ in a million years, but huh, you can! can't have any types w/ non-trival copy constructors in a union, though, apparently, which is quite the restriction.
https://gist.github.com/erinok/c823af95db408653c7e42ab1893073b7 https://gist.github.com/erinok/c823af95db408653c7e42ab189307...
- ivmaykov 4y agoI may be wrong, but I thought it’s that you can’t have any fields with non trivial destructors, because the union wouldn’t know which destructor to call. So POD types / raw pointers / arrays of the above / structs of the above are allowed, and that’s pretty much it.
- gpderetta 4y agoYou can have non trivial types in an union, but you need to explicitly define constructor in the union or the containing type (i.e. the default union copy constructor and assignment operator are disabled).
- jcranmer 4y ago> can't have any types w/ non-trival copy constructors in a union, though, apparently, which is quite the restriction. That was removed in C++11. The rule now is: > Absent default member initializers ([class.mem]), if any non-static data member of a union has a non-trivial default constructor ([class.default.ctor]), copy constructor, move constructor ([class.copy.ctor]), copy assignment operator, move assignment operator ([class.copy.assign]), or destructor ([class.dtor]), the corresponding member function of the union must be user-provided or it will be implicitly deleted ([dcl.fct.def.delete]) for the union. i.e., you need to provide an explicit version of the special function for the union if any member has a nontrivial implementation.
- dilap 4y agoah, good to know, thanks! unfortunately too late to edit my comment