3
for (int i = 0; i < 5; ++i) {
    std::get<i>(tuple);
}

this doesn't compile, since i is not a compile time constant. On How can you iterate over the elements of an std::tuple? and other posts I see answers of recursion, or using std::apply, but those lose the index control. I also don't want to limit myself soley on std::tuple.


Whenever I have to loop over something at compile time I have to stop and think and do weird things especially when I try to achieve non-standard iterations like reverse, custom increment, or involve multiple indices in the same statements such as std::get<i>(tuple) * std::get<i + 1>(tuple).


with what is the closest we can have to a constexpr for (int i = 0; i < 5; ++i)?

Stack Danny
  • 7,754
  • 2
  • 26
  • 55

1 Answers1

6

It is possible to make a constexpr_for<N>(F&& function) implementation that uses std::index_sequence to expand the Size as 0, 1, ... N - 1 onto a templated lambda, which calls the function with a std::integral_constant parameter. This parameter implicitly converts the struct's template argument to size_t via its constexpr operator value_type() const noexcept; operator.

#include <utility>
#include <type_traits>

template<size_t Size, typename F>
constexpr void constexpr_for(F&& function) {
    auto unfold = [&]<size_t... Ints>(std::index_sequence<Ints...>) {
        (std::forward<F>(function)(std::integral_constant<size_t, Ints>{}), ...);
    };

    unfold(std::make_index_sequence<Size>());
}

This enables the std::get<i> behaviour:

auto tuple = std::make_tuple(0ull, 1, 2.0, "3", '4');
constexpr size_t size = std::tuple_size_v<decltype(tuple)>;

constexpr_for<size>([&](auto i) {
    std::cout << std::get<i>(tuple) << ' ';
});
//prints 0 1 2 3 4

with the [&] capture there is access to size so reverse-iterating can be achieved:

constexpr_for<size>([&](auto i) {
    std::cout << std::get<size - i - 1>(tuple) << ' ';
});
//prints 4 3 2 1 0

or e.g. iterate over odd indices by checking against out of bounds attempts:

constexpr_for<size>([&](auto i) {
    constexpr auto idx = (i * 2 + 1);
    if constexpr (idx < size) {
        std::cout << std::get<idx>(tuple) << ' ';
    }
});
//prints 1 3
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • `auto` in functions templates in C++20, but `auto` in lambdas is C++14. Also, you should use `std::integral_constant` instead of creating your own - it's more typical for this problem. If you want to pass the max size, that can be a 2nd parameter (or just... not, since the user provided it to begin with). – Barry Sep 07 '22 at 13:26
  • 1
    thanks for taking a look Barry. I didn't know about `std::integral_constant`, I will use it in this and future implementations. – Stack Danny Sep 07 '22 at 16:47
  • This is *very* spicy. What was screwing with me was `auto apply` for your lambda, since `std::apply` is a thing. Once I separated that out, it made sense how the fold expression was working (I think). – Kevin Anderson Sep 07 '22 at 17:31
  • You don't need to capture `size` either, it's a constant. – Barry Sep 07 '22 at 17:52