|
|
|||
File indexing completed on 2025-12-16 09:40:51
0001 // Copyright 2023 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // File: overload.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // `absl::Overload` is a functor that provides overloads based on the functors 0020 // with which it is created. This can, for example, be used to locally define an 0021 // anonymous visitor type for `std::visit` inside a function using lambdas. 0022 // 0023 // Before using this function, consider whether named function overloads would 0024 // be a better design. 0025 // 0026 // Note: absl::Overload requires C++17. 0027 // 0028 // Example: 0029 // 0030 // std::variant<std::string, int32_t, int64_t> v(int32_t{1}); 0031 // const size_t result = 0032 // std::visit(absl::Overload{ 0033 // [](const std::string& s) { return s.size(); }, 0034 // [](const auto& s) { return sizeof(s); }, 0035 // }, 0036 // v); 0037 // assert(result == 4); 0038 // 0039 0040 #ifndef ABSL_FUNCTIONAL_OVERLOAD_H_ 0041 #define ABSL_FUNCTIONAL_OVERLOAD_H_ 0042 0043 #include "absl/base/config.h" 0044 #include "absl/meta/type_traits.h" 0045 0046 namespace absl { 0047 ABSL_NAMESPACE_BEGIN 0048 0049 #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \ 0050 ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L 0051 0052 template <typename... T> 0053 struct Overload final : T... { 0054 using T::operator()...; 0055 0056 // For historical reasons we want to support use that looks like a function 0057 // call: 0058 // 0059 // absl::Overload(lambda_1, lambda_2) 0060 // 0061 // This works automatically in C++20 because we have support for parenthesized 0062 // aggregate initialization. Before then we must provide a constructor that 0063 // makes this work. 0064 // 0065 constexpr explicit Overload(T... ts) : T(std::move(ts))... {} 0066 }; 0067 0068 // Before C++20, which added support for CTAD for aggregate types, we must also 0069 // teach the compiler how to deduce the template arguments for Overload. 0070 // 0071 template <typename... T> 0072 Overload(T...) -> Overload<T...>; 0073 0074 #else 0075 0076 namespace functional_internal { 0077 template <typename T> 0078 constexpr bool kDependentFalse = false; 0079 } 0080 0081 template <typename Dependent = int, typename... T> 0082 auto Overload(T&&...) { 0083 static_assert(functional_internal::kDependentFalse<Dependent>, 0084 "Overload is only usable with C++17 or above."); 0085 } 0086 0087 #endif 0088 0089 ABSL_NAMESPACE_END 0090 } // namespace absl 0091 0092 #endif // ABSL_FUNCTIONAL_OVERLOAD_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|