Skip to content

Add helper function unpackIntegerSequence

Carsten Gräser requested to merge feature/unpack-integer-sequence into master

This forwards all entries of a given std::integer_sequence<I,i...> as std::integral_constant<I,i>... argument list to a callback.

A classical usage of std::integer_sequence is e.g. to extract tuple values. In use cases like this, one normally has to explicitly write a helper function which is specialized for the std::integer_sequence<I,i...> to be able to extract the i... as variadic list. This can then be used to expand more complicated expressions depending on i.... Using unpackIntegerSequence() improved locality by avoiding these helper functions in favour of in-place unpacking. E.g. std::apply can be implemented as

template<class F, class ArgTuple>
decltype(auto) apply(F&& f, ArgTuple&& args)
{
  auto indices = std::make_index_sequence<std::tuple_size<std::decay_t<ArgTuple>>::value>();
  return unpackIntegerSequence([&](auto... i) {
    return f(std::get<i>(args)...);
  }, indices);
}

Instead of having to write a dedicated helper

template<class F, class ArgTuple, std::size_t... indices>
decltype(auto) applyHelper(F&& f, ArgTuple&& args, std::index_sequence<indices>)

I'm not aware of any existing method from std:: or Dune:: doing the trick.

Merge request reports