https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121180
Bug ID: 121180
Summary: std::any allows for creating multiple empty objects of
the same type at the same address
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: luigighiron at gmail dot com
Target Milestone: ---
The following program demonstrates how std::any can create two objects with the
same type at the same address:
#include<any>
#include<iostream>
struct A{};
struct B:A{};
struct C:A{};
struct D:A{};
struct E:A{};
struct F:A{};
struct G:A{};
struct H:A{};
struct I:A{};
struct J:A{};
struct K:B,C,D,E,F,G,H,I,J{
std::any k;
};
int main(){
K x;
x.k=A{};
std::cout<<"address of A in any: "<<&std::any_cast<A&>(x.k)<<'\n';
std::cout<<"address of A in base class: "<<&(A&)(J&)x<<'\n';
std::cout<<"comparison of addresses:
"<<std::boolalpha<<(&std::any_cast<A&>(x.k)==&(A&)(J&)x)<<'\n';
}
When compiled on 64 bit x86, this prints two identical addresses and then the
comparison results in true. That is, the A base class of J in x has the same
address as the A object stored in x.k.