Familiar Template Syntax IILEs

A lot has already been said in the blogosphere about the use of immediately-invoked lambda expressions (IILEs) for initialization, and they’re certainly very useful.

In C++20, P0428 gives us “familiar template syntax” for lambdas. Now, instead of writing a regular generic lambda:

auto add = [] (auto x, auto y) {
  return x + y;
};

we have the option to use “familiar template syntax” to name the template arguments:

auto add = []  (T x, T y) {
  return x + y;
};

This has several uses — see the paper for motivations listed there — but one I want to draw attention to in particular: when we use an FTSL as an IILE, it can simplify code.

Examples, you say? I have two for you.

First, consider the “possible implementation” of std::apply listed on cppreference.com:

namespace detail {
  template  
  constexpr decltype(auto) apply_impl(F&& f, Tuple&& t,
                                      std::index_sequence) {
    return std::invoke(std::forward(f),
                       std::get(std::forward(t))...);
  } 
} // namespace detail   

template  
constexpr decltype(auto) apply(F&& f, Tuple&& t) {
    return detail::apply_impl(
      std::forward(f), std::forward(t),
      std::make_index_sequence>>{}); 
}

With an FTSIILE, that can become:

template 
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
  return [&]  (std::index_sequence) {
      return std::invoke(std::forward(f), 
                         std::get(std::forward(t))...);
}(std::make_index_sequence>>{});
}

With an FTSIILE, we avoid having to forward multiple times: we can capture by reference into the lambda instead, and do the forwarding once, inside. The result for small functions like this is about half as much code, better encapsulated. No need to declare a helper any more just to destructure template arguments.

Here’s another quick example, from Jonathan Boccara’s recent blog post about STL Algorithms on Tuples. In that post, Jonathan presents for_each2, a function template that applies a binary function over two tuples:

template 
F for_each2_impl(Tuple1&& t1, Tuple2&& t2, F&& f, std::index_sequence)
{
  return (void)std::initializer_list{
    (std::forward(f)(std::get(std::forward(t1)),
                        std::get(std::forward(t2))),0)...}, f;
}
 
template 
constexpr decltype(auto) for_each2(Tuple1&& t1, Tuple2&& t2, F&& f)
{
  return for_each2_impl(std::forward(t1), std::forward(t2),
                        std::forward(f),
                        std::make_index_sequence>::value>{});
}

There is a small problem in the original code here: it’s not right to call std::forward<F>(f) in for_each2_impl and potentially move multiple times from the same callable. But that’s not too important; the function rewritten in C++20 could look like this:

template 
constexpr decltype(auto) for_each2(Tuple1&& t1, Tuple2&& t2, F&& f)
{
  return [&]  (std::index_sequence) {
      return (std::invoke(f, std::get(std::forward(t1)),
                          std::get(std::forward(t2))), ...), f;
    }(std::make_index_sequence>::value>{});
}

Again, less std::forward boilerplate, better encapsulation, less code overall, and the generated code is identical.

So there you have it. Plain IILEs can improve initialization in regular code with less fuss that it would take to extract a small function, and FTSIILEs can now improve template code in the same way, removing the need for separate functions that previously existed only to destructure template arguments and that necessitated more forwarding boilerplate.

Published
Categorized as C++