Date: Thu, 4 Jul 2024 20:19:14 +0100
If the target type is the most-derived type:
if (dynamic_cast<Target>(src) && typeid(*src) == typeid(Target)) return
dynamic_cast<Target>(src);
On Thursday, July 4, 2024, Phil Endecott via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Dear Experts,
>
> dynamic_cast will both downcast and sidecast. (And upcast, etc.)
>
> Is there some trick that can be used to constrain it to do one but not the
> other?
> If not, should the language provide additional casts that are more
> constrained
> in what they do? (I.e. downcast<T>(expr), upcast<T>(expr),
> sidecast<T>(expr).)
> Would that be feasible with how the current ABIs work?
>
> I ask because, as you can guess, I've just hit an issue where I was getting
> unexpected sidecasting where code was only expecting downcasting. My
> most derived class has multiple inheritance (but not virtual inheritance).
> I
> invoke some function on one of its base classes, and that unexpectedly
> behaves differently because it can cast itself to a sibling base class.
>
> Thoughts anyone?
>
>
> Regards, Phil.
>
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
if (dynamic_cast<Target>(src) && typeid(*src) == typeid(Target)) return
dynamic_cast<Target>(src);
On Thursday, July 4, 2024, Phil Endecott via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Dear Experts,
>
> dynamic_cast will both downcast and sidecast. (And upcast, etc.)
>
> Is there some trick that can be used to constrain it to do one but not the
> other?
> If not, should the language provide additional casts that are more
> constrained
> in what they do? (I.e. downcast<T>(expr), upcast<T>(expr),
> sidecast<T>(expr).)
> Would that be feasible with how the current ABIs work?
>
> I ask because, as you can guess, I've just hit an issue where I was getting
> unexpected sidecasting where code was only expecting downcasting. My
> most derived class has multiple inheritance (but not virtual inheritance).
> I
> invoke some function on one of its base classes, and that unexpectedly
> behaves differently because it can cast itself to a sibling base class.
>
> Thoughts anyone?
>
>
> Regards, Phil.
>
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2024-07-04 19:19:16