C++ Logo

std-proposals

Advanced search

Re: "constexpr for" proposal

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sat, 31 Oct 2020 12:56:45 +0100
sob., 31 paź 2020 o 09:41 Peter C++ via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> You know that you could use std::apply and a fold expression in a lambda passed to it to solve that?
>
> Regards
> Peter
>

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.


> sent from a mobile device so please excuse strange words due to autocorrection.
> Peter Sommerlad
> peter.cpp_at_[hidden]
> +41-79-432 23 32
>
> > On 31 Oct 2020, at 03:13, Gil Shallom via Std-Proposals <std-proposals_at_[hidden]> wrote:
> >
> > 
> > Hi all,
> >
> > I have a proposal to add "constexpr for" in addition to the existing "constexpr if".
> >
> > For example, I've written an C++17 "constexpr if" update to Nicolai M. Josutti's print_tuple as follows below.
> > (http://cppstdlib.com/code/util/printtuple.hpp.html)
> >
> > The code before the proposal:
> >
> > template <std::size_t Idx, typename... Args>
> > std::ostream& print_tuple(std::ostream& os, const std::tuple<Args...>& t)
> > {
> > if constexpr (Idx < sizeof...(Args))
> > {
> > os << std::get<Idx>(t) << (Idx+1<sizeof...(Args) ? "," : "");
> > print_tuple<Idx+1>(os, t);
> > }
> > return os;
> > }
> >
> > template <typename... Args>
> > std::ostream& print_tuple(std::ostream& os, const std::tuple<Args...>& t)
> > {
> > print_tuple<0>(os, t);
> > return os;
> > }
> >
> > The code after the "constexpr for" proposal:
> >
> > template <typename... Args>
> > std::ostream& print_tuple(std::ostream& os, const std::tuple<Args...>& t)
> > {
> > for constexpr (std::size_t idx=0; idx<sizeof...(Args); ++idx)
> > {
> > os << std::get<idx>(t) << (idx+1<sizeof...(Args) ? "," : "");
> > }
> > return os;
> > }
> >
> > Conclusion:
> > 1. The syntax in the new proposal is easier to understand.
> > 2. The compiler implementation can hopefully build on foundations laid down by constexpr if.
> >
> > Thank you for reading and hope you find my proposal useful,
> >
> > Gil
> >
> > p.s.
> > 1. Naturally, "constexpr while", "constepxr do while", etc... can also be added to the proposal.
> > 2. I'd be happy to follow with an initial proposal draft.
> > --
> > Std-Proposals mailing list
> > Std-Proposals_at_[hidden]
> > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-10-31 06:57:00