C++ Logo

std-proposals

Advanced search

Re: [std-proposals] PROPOSAL: Standardise one cast — a cast’s a cast

From: Jan Schultke <janschultke_at_[hidden]>
Date: Mon, 21 Sep 2026 09:55:30 +0200
On Mon, 21 Sept 2026 at 09:45, Liam Graham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> We have reinterpret_cast, const_cast, dynamic_cast, static_cast.
>
> What does the CPU do? Nothing. It doesn't care
>

It does care. If you have a pointer to a base class subobject and cast that
to a derived type, i.e. cast<Derived*>(&my_base_object):

   - reinterpret_cast might downcast "too far" because it casts to the most
   derived object for polymorphic classes, or do something else nonsensical if
   you're not dealing with polymorphic classes
   - static_cast will downcast properly, but is unchecked and results in UB
   if you don't actually have a surrounding derived object
   - dynamic_cast will perform a runtime check for whether the cast is
   valid and give you a null pointer if it's not
   - std::bit_cast will reinterpret the pointer bits unchanged, whatever
   that does

Received on 2026-09-21 07:55:46