5

So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type:

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

Which is constructed as an r-value here:

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);

I'm trying to figure out how this works. What is the type that overloaded inherits from here? It seems like an array of lambdas but I don't see how that would have an operator(). Can someone explain how the inheritance is working here?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 3
    If you're over 10k you can see I just tried this question: https://stackoverflow.com/q/44414238/2642059 @BaummitAugen helped me recognize the inheritance. So I think I'm asking the right question now. – Jonathan Mee Jun 07 '17 at 13:52

1 Answers1

6

overloaded inherits from each lambda individually and each lambda has a call operator. You therefore create a struct that has all call operators in one overload set. As long as they are not ambiguous the right one will automatically be picked.

You can imagine the variadic template to expand to

struct overloaded :
    // inherits from
    decltype([](auto arg) { std::cout << arg << ' '; }),
    decltype([](double arg) { std::cout << std::fixed << arg << ' '; }),
    decltype([](const std::string& arg) { std::cout << std::quoted(arg) << ' '; })

    // has three operator()s
    {
        using decltype([](auto arg) { std::cout << arg << ' '; })::operator();
        using decltype([](double arg) { std::cout << std::fixed << arg << ' '; })::operator();
        using decltype([](const std::string& arg) { std::cout << std::quoted(arg) << ' '; })::operator();
    };

Except in real code it wouldn't work because the lambdas with the same body would still have different types.

It creates 1 overloaded type with multiple inheritance per instantiation.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
nwp
  • 9,623
  • 5
  • 38
  • 68
  • Obviously you understand this topic better than I do... So am I creating one `overloaded` object with multiple inheritance? Or perhaps 3 `overloaded` objects? – Jonathan Mee Jun 07 '17 at 13:56
  • @JonathanMee You're passing one object which has three operators that in turn call one of three lambdas you derive from. In the end, you could just create a type and provide those operators yourself, this is just a shorthand for the example. – Bartek Banachewicz Jun 07 '17 at 14:03
  • @BartekBanachewicz That does not seem to align with what `nwp` is saying. He seems to think that I'm passing 3 individual lambdas. – Jonathan Mee Jun 07 '17 at 14:10
  • @JonathanMee I have no idea how you got that impression. – Bartek Banachewicz Jun 07 '17 at 14:12
  • Thanks, I now understand that the `using Ts::operator()...` exposes on a per-lambda basis the `operator()` for overloading. So it seems like the first line of the definition is sufficient, what is the purpose of this line: `template overloaded(Ts...) -> overloaded` – Jonathan Mee Jun 07 '17 at 17:16
  • 1
    @JonathanMee That is a [deduction guide](https://stackoverflow.com/questions/40951697/what-are-template-deduction-guides-in-c17#40951769) telling the compiler that if an `overloaded` is constructed with syntax left of the `->` that it should be deduced to the type right of the `->` which saves you from having to specify the template arguments. – nwp Jun 07 '17 at 17:28