I have this simple example:
struct base
{
constexpr base () {};
};
struct derived: public base
{
constexpr derived () {};
};
int main()
{
constexpr derived d{};
constexpr const base &b = d; // why error?
}
G++ gives me the following error:
error: '(const base&)(& d)' is not a constant expression
And clang++ gives me following error:
error: constexpr variable 'b' must be initialized by a constant expression.
note: reference to subobject of 'd' is not a constant expression. Add 'static' to give it a constant address.
It's clear to me that clang gives the solution but I cannot understand why the problem is solved?
It said that "the reference to subobject of d
is not a constant expression.", and the subobject here has constexpr constructor so why it's not constant expression?
Finally, I don't understand why using static
solves the problem?
Please includes the answer with a quote from the standard (if possible).