I used to keep pretty good track of what's been happening in c++, at least as C++11 was being formalized, and I've read a good fraction of Effective Modern C++, but...
I can't make heads or tails out of half of the code in that example. C++ has become extremely hard to grok.
What's even going on there? Inheriting from a variadic list of classes? `using` a variadic list of parent functions, I guess? The second line looks like it's defining a constructor function for the overloaded() struct, but it's not implemented anywhere?
In the end there's magic code that looks like pattern matching, but I have no idea how they got there.
The first line defines 'struct overloaded' derived from all of its template parameters. Template parameters are supposed to be functors (lambdas), so we use their operator()'s in 'overloaded'.
The second line defines a template deduction guide for the 'overloaded' constructor, which says: take types from the constructor arguments and use them as the class template parameters.
The idea is to be able to construct 'overloaded' like this:
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }
if you used this like
overloaded<X, Y, Z> foo;
it would expand to
struct overloaded : X, Y, Z {
using X::operator(), Y::operator(), Z::operator();
} foo;
EDIT: Removed stuff about the function since I was only guessing and Alexeiz answered it. The only thing I’ll add is that you can construct structs like this:
struct Foo { int x; float y; };
Foo foo {1, 2.3};
overloaded is constructed from the three lambdas in the same way. I guess the deduction guide is needed because the compiler cannot deduce the template parameters from that initializer form, so the deduction guide tells it to use the types of the lambdas for the types of the template parameters.
More information on deduction guides: http://en.cppreference.com/w/cpp/language/class_template_arg... (specifically, look for “Class template argument deduction of aggregates typically requires deduction guide” in the notes section for an example very like the overloaded one)
I can't make heads or tails out of half of the code in that example. C++ has become extremely hard to grok.
I mean, take these two lines:
What's even going on there? Inheriting from a variadic list of classes? `using` a variadic list of parent functions, I guess? The second line looks like it's defining a constructor function for the overloaded() struct, but it's not implemented anywhere?In the end there's magic code that looks like pattern matching, but I have no idea how they got there.