C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 10 Apr 2026 08:16:16 -0700
On Friday, 10 April 2026 03:28:26 Pacific Daylight Time Muneem via Std-
Proposals wrote:
> It will throw an expression since its a runtime_index, hence the
> instantiation code runs at runtime, but if it was a compile time index then
> it would be a compiler error, since the instansitation code could be run at
> compile time. A way to implement it would be to provide two blocks of code
> in the definition of select() or operator[]():
> one runs if std::is_constant_evaluated() is true, and the other if false.
> see this:https://en.cppreference.com/w/cpp/types/is_constant_evaluated.html

How far does the compiler check that the type can be compiled?

int^ x = {1,2.5,"C++"}.select(runtime_index); // happens to be 2
// hundreds or thousands of lines here
x = 2;

Can this be returned?

int ^f(auto &list, int runtime_index) { return list.select(runtime_index); }

The solution I can see to this is that the error happens on the "x = 2" line,
not on selection.

With reflection, I think this could be implemented without a language change,
though. Something like:

using List = std::variant<int, double, std::string>;
variant_element_ref<int> f(const V &list, int runtime_index)
{
    return v.select(runtime_index);
}

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

Received on 2026-04-10 15:16:23