When I inherit from a templated base class that is a union
and have partial specializations on it, the compilation on MSVC fails since it's forbidden to have union
s as base classes (see 1).
If I however use a struct
for the primary template I inherit from and only use union
for the partial specializations it lets me compile it just fine.
I'm curious as to why that is. Also I'd like to know whether the below code (2) is standard compliant or MSVC only allowed me to get around the standard.
(1) C++ standard (10.4 Unions [class.union])
"A union can have member functions (including constructors and destructors), but it shall not have virtual (10.6.2) functions. A union shall not have base classes. A union shall not be used as a base class."
(2) Example (godbolt.org)
//template <typename T, std::size_t Size>
//↓ ↓ clearly illegal
//union vector_base {
// vector_base() : data() {}
// std::array<T, Size> data;
// }
template <typename T, std::size_t Size>
//↓ legal now?
struct vector_base
{
std::array<T, Size> data;
};
template <typename T>
union vector_base<T, 2>
{
std::array<T, 2> data;
struct { T x, y; };
};
template <typename T, std::size_t Size>
class vector : public vector_base<T, Size> {
};