Given struct B
, which inherits an anonymous union
data member from struct A
:
#include <cstddef>
struct A
{
union
{
struct { int a, b, c; };
int vals[3];
};
};
struct B: A
{
constexpr B(int x)
:
A({ x, x, x })
{}
constexpr int& operator[](size_t i)
{
return this->vals[i];
}
constexpr const int& operator[](size_t i) const
{
return this->vals[i];
}
};
I declare a constexpr
variable of type B
, then calling its operator[]
to assign the return value to a constexpr int
:
int main()
{
constexpr B b(7);
constexpr int i = b[2];
return 0;
}
However Clang 3.8 gives me the error message
constexpr variable 'i' must be initialized by a constant expression
The issue is related to the anonymous union
, since when I simply use int vals[3]
then everything works fine.
Is there a C++14 constexpr
restriction I'm missing? Anyhow, a solution would be highly appreciated.
Aucun commentaire:
Enregistrer un commentaire