3

In Timur Doumler's talk at CppCon 2022, "C++ Lambda Idioms", at around 45:19 he is discussing the lambda overload trick and says something like

In C++17, we had to write a deduction guide to make this work -- which is like two more lines of code -- but since C++20 we have [I don't know what he is saying] so we don't have to do that any more ...

Why is a deduction guide no longer required to create a lambda overload set in C++20?

jwezorek
  • 8,592
  • 1
  • 29
  • 46

1 Answers1

7

For the following class template:

template<class... Ts> 
struct S : Ts... { };

If we want the following initialization to work in C++17:

S s{
  [](int) { },
  [](double) { }
};

We have to manually add a deduction guide for it since the compiler cannot deduce the type of Ts...

template<class... Ts>
S(Ts...) -> S<Ts...>;

Thanks to the introduction of P1816 (Class Template Argument Deduction for aggregates), we no longer need to explicitly provide a deduction guide for the aggregates type S in C++20, the compiler will automatically deduce the type of Ts... for us, which is the type of those lambdas.

That's what the video is about

"but since C++20 we have CTAD (Class template argument deduction) for aggregates..."

康桓瑋
  • 33,481
  • 5
  • 40
  • 90