1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_FUNCTIONAL_OVERLOADED_H_
6 #define BASE_FUNCTIONAL_OVERLOADED_H_
10 // absl::visit() needs to be called with a functor object, such as
13 // std::string operator()(const PackageA& source) {
17 // std::string operator()(const PackageB& source) {
22 // absl::variant<PackageA, PackageB> var = PackageA();
23 // return absl::visit(Visitor(), var);
25 // `Overloaded` enables the above code to be written as:
29 // [](const PackageA& pack) { return "PackageA"; },
30 // [](const PackageB& pack) { return "PackageB"; },
33 // Note: Overloads must be implemented for all the variant options. Otherwise,
34 // there will be a compilation error.
36 // This struct inherits operator() method from all its base classes.
37 // Introduces operator() method from all its base classes into its definition.
38 template <typename... Callables>
39 struct Overloaded : Callables... {
40 using Callables::operator()...;
43 // Uses template argument deduction so that the `Overloaded` struct can be used
44 // without specifying its template argument. This allows anonymous lambdas
45 // passed into the `Overloaded` constructor.
46 template <typename... Callables>
47 Overloaded(Callables...) -> Overloaded<Callables...>;
51 #endif // BASE_FUNCTIONAL_OVERLOADED_H_