https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112591
Bug ID: 112591
Summary: variant allows for creating multiple empty objects at
same address
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: barry.revzin at gmail dot com
Target Milestone: ---
Consider:
#include <variant>
#include <iostream>
struct Empty { ~Empty() {} };
struct Sub : Empty { std::variant<Empty> e; };
int main() {
Sub v;
Empty* base = &v;
Empty* obj = &std::get<Empty>(v.e);
std::cout
<< "base=" << (void*)base
<< " obj=" << (void*)obj
<< " matches=" << (base == obj)
<< '\n';
}
In C++17 mode on trunk, base and obj have the same address.
In C++20 mode up through 11.4, they have the same address. Since 12.1, they do
not.
If Empty were trivially destructible, they never have the same address.