C++ Logo

std-proposals

Advanced search

Re: "constexpr for" proposal

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sat, 31 Oct 2020 09:01:40 -0400
On Sat, Oct 31, 2020 at 7:57 AM Marcin Jaczewski via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
> For a long time I wanted to have something like that, and if correctly
> defined it can give a lot more than `std::apply`.
> If constexpr loops are defined in "code block copy paste" then we
> could allow very interesting things:
> ```
> switch (i)
> {
> for constexpr (constexpr int i = 0; i < X; ++i)
> {
> case i: get<i>(); break; //`for` break but work correctly there.
> case i + X: set<i>(); break;
> }
> }
> ```
> This will allow for the first time interaction of `switch` with
> variadic templates that currently is very troublesome to do.
> Another thing is `break` and `continue`, it should not break loops but
> work as `goto` between iterations of it.
> This mean code like:
> ```
> for constexpr (constexpr int i = 0; i < X; ++i)
> {
> get<i>();
> break;
> }
> ```
> will instantiate up to `get<X-1>()` but will call only `get<0>()` as
> rest will be dead code.
>

See also https://quuxplusone.github.io/blog/2019/02/28/expansion-statements/

–Arthur

Received on 2020-10-31 08:01:53