C++ Logo

std-proposals

Advanced search

Re: "constexpr for" proposal

From: Gil Shallom <gil.shallom_at_[hidden]>
Date: Sat, 31 Oct 2020 12:19:25 -0700

Thank you, Peter, for the std::apply solution (https://en.cppreference.com/w/cpp/utility/apply#Example). It's very cool.
I agree with Marcin and Arthur that a "constexpr for" allows for an easier to read and more expressible code.
Anyone aware of a reason why p1306 did not include free form compile time for loops?
(It could get the compiler stuck - but so can any other template meta programming bug).

Thank you.

On 31/10/2020 6:01, Arthur O'Dwyer wrote:
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.


–Arthur


Received on 2020-10-31 14:19:27