C++ Logo

std-proposals

Advanced search

[std-proposals] Fold expression contains 'if constexpr'

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 4 Dec 2022 22:40:25 +0000
I wanted to write the following function containing a fold expression:

template<typename... Types>
auto Func(Types... a)
{
    using std::get;
    decltype(pointers) &p = pointers;

    (if constexpr(requires{ get<Types>(p)->Method(a...); }) return
get<Types>(p)->Method(a...), ...);
}

but since we can't have 'if constexpr' inside a fold expression, I
instead had to write:

template<typename... Types>
auto Func(Types... a)
{
using std::get;

decltype(pointers) &p = pointers;

     if constexpr(requires{get< 0>(p)->Method(a...); })return get<
0>(p)->Method(a...);
else if constexpr(requires{get< 1>(p)->Method(a...); })return get<
1>(p)->Method(a...);
else if constexpr(requires{get< 2>(p)->Method(a...); })return get<
2>(p)->Method(a...);
else if constexpr(requires{get< 3>(p)->Method(a...); })return get<
3>(p)->Method(a...);
else if constexpr(requires{get< 4>(p)->Method(a...); })return get<
4>(p)->Method(a...);
else if constexpr(requires{get< 5>(p)->Method(a...); })return get<
5>(p)->Method(a...);
else if constexpr(requires{get< 6>(p)->Method(a...); })return get<
6>(p)->Method(a...);
else if constexpr(requires{get< 7>(p)->Method(a...); })return get<
7>(p)->Method(a...);
else if constexpr(requires{get< 8>(p)->Method(a...); })return get<
8>(p)->Method(a...);
else if constexpr(requires{get< 9>(p)->Method(a...); })return get<
9>(p)->Method(a...);
else if constexpr(requires{get<10>(p)->Method(a...); })return
get<10>(p)->Method(a...);
else if constexpr(requires{get<11>(p)->Method(a...); })return
get<11>(p)->Method(a...);
else if constexpr(requires{get<12>(p)->Method(a...); })return
get<12>(p)->Method(a...);
else if constexpr(requires{get<13>(p)->Method(a...); })return
get<13>(p)->Method(a...);
else if constexpr(requires{get<14>(p)->Method(a...); })return
get<14>(p)->Method(a...);
else if constexpr(requires{get<15>(p)->Method(a...); })return
get<15>(p)->Method(a...);
}

Is there some how some way that we can allow 'if constexpr' inside a
fold expression? I know that we can have a fold expression containing
a lambda (and that the lambda can contain 'if constexpr'), but in this
example we need 'if constexpr' by itself.

Received on 2022-12-04 22:40:34