Common lambda functions with template parameters

 #include <utility>
 #include <tuple>

 template < typename T, typename U >
 void h(T, U)
 {
 }

 template < typename... T, typename... U >
 void f(std::tuple < T... >  t, std::tuple < U... >  u)
 {
     auto g = [&] < std::size_t... I > (std::index_sequence < I... >)
              {
                  bool const r[]{((void)h(std::get < I >(t), std::get < I >(u)), false)...};
                  (void)r;
             };
     g(std::index_sequence_for < T... >());
 }

 int main()
 {
     f(std::make_tuple(0L, 0LL), std::make_tuple(0UL, 0ULL));
 }

Above compiles with g++ test_templated_lambda.cpp -o test_templated_lambda -std=c++14but does not compile withclang++ test_templated_lambda.cpp -o test_templated_lambda -std=c++14

I know this is a GCC extension ( Using a template parameter in a common lambda ), but is there a way to do this without writing out gas a free function

+4
source share
1 answer

This is not possible without any outside help; generic lambdas is the only form of pattern allowed within the scope, and they cannot be specialized or overloaded (without any external helper, such as P0051R1 overload ).

, , () (b)

[&](auto&& g) {
  g(g, std::integral_constant<std::size_t, 0u>{});
}([&](auto&& g, auto I) {
  h(std::get<I>(t), std::get<I>(u));
  std::get<I + 1 == sizeof...(T)>(std::make_pair(
    [&](auto&& g) { g(g, std::integral_constant<std::size_t, I + 1>{}); },
    [](auto&&){}))(g);
});

.

(std::index_sequence_for), ? :

template<class F, std::size_t... I> auto apply_indexes(F&& f, std::index_sequence<I...>) {
  return std::forward<F>(f)(std::integral_constant<std::size_t, I>{}...);
}

:

auto g = [&](auto... I)
{
  bool const r[]{((void)h(std::get<I>(t), std::get<I>(u)), false)...};
  (void)r;
};
apply_indexes(g, std::index_sequence_for<T...>());
+2

Source: https://habr.com/ru/post/1628469/


All Articles