C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::any::base

From: Mihail Mihaylov <mihail.mihailov_at_[hidden]>
Date: Tue, 16 Apr 2024 14:58:36 +0300
On Tue, Apr 16, 2024 at 2:03 PM Sebastian Wittmeier via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> That ambigious base classes can be only detected at runtime, also hints
> that casting any to a base class is probably quite an expensive operation
> (compared to dynamic_cast).
>
>
>
> Mihail: "Then why would it be good practice, if we transform these
> functions to take in an any* or any&, to force them to perform a widening
> any_cast to polymorphic_value<A> followed by a dynamic_cast<B&> or
> dynamic_cast<C&> respectively?"
>
>
>
> Normally the complicated double casts (any_cast + dynamic_cast) are not
> needed: In many situations you would just transfer a polymorphic_value<A>
> without std::any.
>

We are not discussing cases where there is a common base, only the case of
disjoint types when at least one of them can have descendants.


> But just in case, in those situations (where also totally different types
> are transferred), it would be best practice to chain both casts exactly for
> the following reasons:
>
> - it is more performant (no scan through inheritance hierarchies for each
> cast needed)
>

As far as I know, under many circumstances, *dynamic_cast* can be expensive
too. And yes, it's a runtime cast, which is expected to be more costly, we
can assume that the user has weighed the performance cost.


> - ambiguous base classes A (for B and/or C) would be detected at
> compile-time
>

I don't think that *polymorphic_value<T>* would prevent you from storing in
it a class that has an ambiguous base other than *T*. Besides, "detected at
compile-time" means "forbidden". It should be OK to store in an *any* a
class that uses non-virtual multiple inheritance, as long as no one tries
to cast it to the ambiguous base.

-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

Received on 2024-04-16 11:58:50