C++ Logo

std-proposals

Advanced search

[std-proposals] Downcasting without Sidecasting

From: Phil Endecott <std_proposals_list_at_[hidden]>
Date: Thu, 04 Jul 2024 18:53:37 +0100
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.

Received on 2024-07-04 17:53:39