C++ Logo

std-proposals

Advanced search

Re: "constexpr for" proposal

From: Peter C++ <peter.cpp_at_[hidden]>
Date: Sat, 31 Oct 2020 09:40:14 +0100
You know that you could use std::apply and a fold expression in a lambda passed to it to solve that?

Regards
Peter

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

Received on 2020-10-31 03:40:21