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?
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.
Therefore, the correct answer how to determine if a type is a struct is: bool isStruct = type. IsValueType && ! 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With