template<class... Ts>
Overloaded struct
Helper type for creating overloaded function objects from lambdas.
Template parameters | |
---|---|
Ts | Variadic template pack of callable types. |
The Overloaded struct is a utility for combining multiple lambda functions or callable objects into a single overloaded function object. This is particularly useful with std::visit and variant types where different alternatives require different handling logic.
The struct inherits from all provided callable types and brings their operator() functions into scope using the 'using' declaration, creating an overload set that can be used for pattern matching on variants.
Example usage:
std::variant<int, std::string, double> var = 42; auto visitor = Overloaded { [](int i) { return "integer: " + std::to_string(i); }, [](const std::string& s) { return "string: " + s; }, [](double d) { return "double: " + std::to_string(d); } }; std::string result = std::visit(visitor, var);