I am reading the book "C++17 - The Complete Guide", by Nicolai M. Josuttis.
Chapter 14 is entitled "Using variadic using declarations". I don't understand the sample code in it:
// Part 1: "inherit" all function call operators of passed based types:
template<typename... Ts>
struct overload : Ts...
{
using Ts::operator()...;
}
// Part 2: base types are deduced from passed arguments
// >>>> What is it ?? -> see answer from Ted Lyngmo
// EDIT: That's also based on the fact that overload can use
// aggregate initialization, and so that overload can be
// instancied with a constructor with any number and types
// (but we have to help it to deduce the types).
template<typename... Ts>
overload(Ts...) -> overload<Ts...>;
// Part 3
auto twice = overload {
[](std::string& s) { s += s; }
[](auto& v) { v *= 2; }
};
The code has 3 parts:
part 1: I understand we declare a class which will have finally 3 function call operators.
part 2: I don't understand this one... - what are we declaring here? Could you explain the syntax, especially overload(Ts...)
?
part 3: We use aggregate initialization to initialize base classes function call operators.
If I have to read a previous chapter again, let me know which one!
template<typename... Ts> overload(Ts...) -> overload<Ts...>;
what are we declaring here ?
This is a user-defined deduction guide. It will be used when the class template overload
is instantiated without explicitly specifying any template parameters. It then helps to deduce the class to be overload<Ts...>
.
In this context:
auto twice = overload {
[](std::string& s) { s += s; },
[](auto& v) { v *= 2; }
};
... it makes twice
of type
overload<decltype(lambda1), decltype(lambda2)>
In C++20, the implicitly generated deduction guides would be enough but in C++17 this deduction guide is needed for this type of instantiation.