C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: Extension to runtime polymorphism proposed

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 07 Apr 2026 19:50:23 -0700
On Tuesday, 7 April 2026 19:39:58 Pacific Daylight Time Muneem via Std-
Proposals wrote:
> 1. The int example was just an example of how you can assign anything to a
> variant. Like a float to an int, but this applies to assigning an
> std::ofstream to a int also.

This is just nonsense. https://gcc.godbolt.org/z/r4c39q1qr

If you meant something different, please just use Godbolt to show the code you
meant.

> 2. It has to do with runtime polymorphism be Use you sometimes want a
> specific memory location to have a fixed type that's set at runtime. So the
> type itself while may be "polymorphic" in the sense that it's decided at
> runtime, but you still want strict type checking, do that if you assign a
> std::ofstream to an int, you get a exception thrown at you.

What I understand from this is that the type is *like* a std::variant, but one
that keeps the currently selected type, instead of changing.

Well, that's still std::variant, except that it's not the assignment operator:
https://gcc.godbolt.org/z/z36sTYdT4

Reflection could be used to construct a proxy type such that one could write
 variant->op1();
which compiles only if all the variant's possible types have op1(), and thus
transform to the equivalent of

  std::visit([](auto &arg) { arg.op1() }, variant);

So far, the conclusion is we don't need a language construct. We can probably
already do with C++26 reflection, and if not with C++29's.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-04-08 02:50:31