When I was reading this paper, I saw lambdas being overloaded in the Recursive Lambdas section. I was confused as I've never saw that before and I thought it might be a new keyword in C++ that I didn't hear about before.
Turns out, it is not a keyword, but a helper function/class that just wasn't defined in the paper. I finally found some information on it on cppreference.com std::visit page.
In the example provided, I understood everything that was happening except for one thing:
// helper type for the visitor #4
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
...
int main() {
...
// 4. another type-matching visitor: a class with 3 overloaded operator()'s
std::visit(overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
}, v);
}
As I understand it, using braces (overloaded{...}
) would indicate that the constructor is being called, but there isn't one, and even if it had called the overloaded
function, it has no body.
So, my question is: What's happening here? I found some reference that it is using user-defined deduction guides, but I'm still not clear.
EDIT
The crux of this, is my misinterpretation of:
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
This is not a free template function declaration, but a (tl;dr) user-defined deduction guides, which states what the default template class parameters should be for the constructor.
This is only barely tangentially related to What are Aggregates and PODs and how/why are they special?, which is what this question was marked as a duplicate of, and is only briefly mentioned in an answer entitled What will change for C++20.