Date: Fri, 12 Apr 2024 21:36:07 +0200
So why not store the Derived* as Base* into the std::any in the first place?
std::any myany = make_any<Base*>(&derived);
If you need the Derived* after all, you can always
Derived* derivedptr = dynamic_cast<Derived*>(std::any_cast<Base*>(myany));
or use suitable member functions of Base.
-----Ursprüngliche Nachricht-----
Von:Phil Endecott via Std-Proposals <std-proposals_at_[hidden]>
Gesendet:Fr 12.04.2024 20:51
Betreff:Re: [std-proposals] Extend std::type_info with more information
An:std-proposals_at_[hidden];
CC:Phil Endecott <std_proposals_list_at_[hidden]>;
Mihail Mihaylov wrote:
> It took me a while to realize how significant it is that `any_cast`
> returns a copy instead of a reference. It now seems to me that the
> designers of `std/boost::any` never meant for polymorphic types to be
> stored in it and the question about how to get a base out of the stored
> derived value goes outside of its intended usage.
You can, however, usefully store pointers to polymorphic types in std::any.
Storing a Derived* and asking the any if it contains a Base* is what I wanted
to do.
(Actually something more like a weak_ptr<Derived>, but maybe I
shouldn't mention that, it's just adding another level of complexity into
this discussion.)
Regards, Phil.
Received on 2024-04-12 19:36:13