C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A type trait for detecting virtual base classes

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Fri, 29 Sep 2023 11:11:09 -0400
On Fri, Sep 29, 2023 at 10:48 AM Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Friday, 29 September 2023 06:36:11 PDT Arthur O'Dwyer via Std-Proposals
> wrote:
> > One high-level concern which I *think* is moot but it would be nice to
> say
> > so explicitly: Conversion from D* to B* requires dereferencing B* when D
> is
> > a virtual base of B. Is that a "when and only when"? Or are there any
> > other situations where that conversion might require dereferencing B*,
> that
> > we should be thinking about?
> > - Things that can require inspecting a vtable include dynamic_cast,
> typeid,
> > and accesses through `int D::*` pointers-to-data-member that happen to
> > point into a virtual base.
> [...]
> > - Things that do pointer conversions include static_pointer_cast,
> > dynamic_pointer_cast, etc.
>
> dynamic_pointer_cast, by its very name, is like those above: it already
> dereferences on its own.
>
> But static_pointer_cast should be documented to either not need it or to
> do
> the necessary locking, both of which are what this trait is for.
>

static_pointer_cast works only on shared_ptr, never on weak_ptr. So it
already knows that the pointer value it's casting will be either valid or
null, never dangling; so it doesn't suffer from the weak_ptr(weak_ptr<Y>&&)
problem.
As I said, I'm pretty sure there's no problems in the examples I *did*
list. I'm more concerned that there might be problematic examples I *didn't*
list.

–Arthur

Received on 2023-09-29 15:11:23