Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Type This struct is Inheriting From?

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?

like image 488
Jonathan Mee Avatar asked Jun 07 '17 13:06

Jonathan Mee


People also ask

What is struct inheritance?

In RemObjects C#, struct s can specify an ancestor, allowing a newly declared struct to inherit the fields and methods of its base struct. Unlike classes, structs are not polymorphic, and members cannot be virtual or overriden.

How do you check if a struct is a type?

Therefore, the correct answer how to determine if a type is a struct is: bool isStruct = type. IsValueType && ! type.

Can a struct inherit from a custom type?

A structure type can't inherit from other class or structure type and it can't be the base of a class. However, a structure type can implement interfaces.

Can a struct inherit from a class in C++?

Yes, struct can inherit from class in C++. In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.


1 Answers

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.

like image 188
nwp Avatar answered Nov 15 '22 02:11

nwp