C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 04 Apr 2026 08:36:18 -0700
On Saturday, 4 April 2026 03:04:27 Pacific Daylight Time Marcin Jaczewski via
Std-Proposals wrote:
> Like:
> ```
> void foo(int i)
> {
> switch(i) { ... }
> }
> void bar(int i)
> {
> for (auto& x: data) for(i);
> }
> ```
>
> change into:
>
> ```
> template<int i>
> void foo()
> {
> //code based on i, like `std::get<i>`
> }
> void bar(int i)
> {
> switch (i){
> case 0:
> for (auto& x: data)
> {
> foo<0>();
> }
> break;
> case 1:
> for (auto& x: data)
> {
> foo<1>();
> }
> break;
> //etc.
> }
> }
> ```

Compilers already do that on their own.

They may not be doing that all the time, though. So if you do need to enforce
this, implementing manually may be necessary.

Do we need a new language construct for this? Especially in the presence of
template for?

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

Received on 2026-04-04 15:36:20