https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107165
Bug ID: 107165
Summary: Downcasting in constexpr, error: "accessing value of
`...Derived::<anonymous>’ through a ‘Derived’ glvalue
in a constant expression"
Product: gcc
Version: 11.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: petr.azmanov at wartsila dot com
Target Milestone: ---
Created attachment 53670
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53670&action=edit
copy of the test case from the comment
Downcasting in constexpr context is broken if derived class is wrapped in
another structure.
Tested locally with g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Reproduced with g++ 11.3.0 in Godbolt (https://godbolt.org/z/fvoWhrh41)
Isn't reproduced with 12.1 or 12.2.
```
struct Base
{
int b = 0;
};
struct Derived : Base
{
int d = 2;
constexpr bool foo(int x)
{
return x > d;
}
};
int main()
{
constexpr auto test = []()
{
struct Dummy
{
Derived derived;
};
Dummy d;
Base & base = d.derived;
static_cast<Derived &>(base).foo(1);
// Derived d;
// Base & base = d;
// static_cast<Derived &>(base).foo(1);
return true;
}();
return test;
}
```