C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow downcasting at compile time if Derived has no extra member objects

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 22 Feb 2023 20:31:46 -0500
On Wed, Feb 22, 2023 at 7:52 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> To give an example where this would be useful, let's say that I want
> to use a 'std::binary_semaphore' as though it were a Lockable. The
> following code works fine with every C++ compiler:

No, absolutely not.

Ignoring the fact that this is a bad idea because that's not how
semaphores work (more on that in the other thread), this also isn't
how the type system works.

What you're talking about is analogous with Rust traits, but Rust
traits are not *types* and are not treated as types. You can't use the
type mechanism for this; you'd have to build some other mechanism the
way Rust did (or C++ does using traits classes in templates).

Received on 2023-02-23 01:32:39