oktonion / stdex

std C++ 11 library impementation with extra features using only C++ 98 and POSIX threads
MIT License
67 stars 6 forks source link

is_union implementation #4

Closed oktonion closed 5 years ago

oktonion commented 6 years ago

Currently is_union has a bug of detecting class as union. This leads to is_class detecting union as class. And that circle of bugs is awful. Have no idea how to distinguish union from class at compile-time without compile-time errors. Boost is not good at this either - all they do is to use compiler specific build-in macro when possible.

oktonion commented 5 years ago

According to the C++ standard (§9.5.1):

A union can have member functions (including constructors and destructors), but not virtual functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor, a non-trivial copy-constructor, a non-trivial destructor, or a non-trivial copy assignment operator cannot be a member of a union, nor can an array of such objects. If a union contains a static data member, or a member of a reference type, the program is ill-formed.

oktonion commented 5 years ago
  1. I can not check without hard compiler error if type can be derived from - union will fail.
  2. I can not check if type has virtual anything without deriving from it - see №1.
  3. I can not check if type contains any specific (non-trivial or reference) members - by standard there is no reference-to-member type.

So nothing can be done except using compiler intrinsic support.