Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:27:11

0001 // Copyright 2018 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 // variant.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header file defines an `absl::variant` type for holding a type-safe
0020 // value of some prescribed set of types (noted as alternative types), and
0021 // associated functions for managing variants.
0022 //
0023 // The `absl::variant` type is a form of type-safe union. An `absl::variant`
0024 // should always hold a value of one of its alternative types (except in the
0025 // "valueless by exception state" -- see below). A default-constructed
0026 // `absl::variant` will hold the value of its first alternative type, provided
0027 // it is default-constructible.
0028 //
0029 // In exceptional cases due to error, an `absl::variant` can hold no
0030 // value (known as a "valueless by exception" state), though this is not the
0031 // norm.
0032 //
0033 // As with `absl::optional`, an `absl::variant` -- when it holds a value --
0034 // allocates a value of that type directly within the `variant` itself; it
0035 // cannot hold a reference, array, or the type `void`; it can, however, hold a
0036 // pointer to externally managed memory.
0037 //
0038 // `absl::variant` is a C++11 compatible version of the C++17 `std::variant`
0039 // abstraction and is designed to be a drop-in replacement for code compliant
0040 // with C++17.
0041 
0042 #ifndef ABSL_TYPES_VARIANT_H_
0043 #define ABSL_TYPES_VARIANT_H_
0044 
0045 #include "absl/base/config.h"
0046 #include "absl/utility/utility.h"
0047 
0048 #ifdef ABSL_USES_STD_VARIANT
0049 
0050 #include <variant>  // IWYU pragma: export
0051 
0052 namespace absl {
0053 ABSL_NAMESPACE_BEGIN
0054 using std::bad_variant_access;
0055 using std::get;
0056 using std::get_if;
0057 using std::holds_alternative;
0058 using std::monostate;
0059 using std::variant;
0060 using std::variant_alternative;
0061 using std::variant_alternative_t;
0062 using std::variant_npos;
0063 using std::variant_size;
0064 using std::variant_size_v;
0065 using std::visit;
0066 ABSL_NAMESPACE_END
0067 }  // namespace absl
0068 
0069 #else  // ABSL_USES_STD_VARIANT
0070 
0071 #include <functional>
0072 #include <new>
0073 #include <type_traits>
0074 #include <utility>
0075 
0076 #include "absl/base/macros.h"
0077 #include "absl/base/port.h"
0078 #include "absl/meta/type_traits.h"
0079 #include "absl/types/internal/variant.h"
0080 
0081 namespace absl {
0082 ABSL_NAMESPACE_BEGIN
0083 
0084 // -----------------------------------------------------------------------------
0085 // absl::variant
0086 // -----------------------------------------------------------------------------
0087 //
0088 // An `absl::variant` type is a form of type-safe union. An `absl::variant` --
0089 // except in exceptional cases -- always holds a value of one of its alternative
0090 // types.
0091 //
0092 // Example:
0093 //
0094 //   // Construct a variant that holds either an integer or a std::string and
0095 //   // assign it to a std::string.
0096 //   absl::variant<int, std::string> v = std::string("abc");
0097 //
0098 //   // A default-constructed variant will hold a value-initialized value of
0099 //   // the first alternative type.
0100 //   auto a = absl::variant<int, std::string>();   // Holds an int of value '0'.
0101 //
0102 //   // variants are assignable.
0103 //
0104 //   // copy assignment
0105 //   auto v1 = absl::variant<int, std::string>("abc");
0106 //   auto v2 = absl::variant<int, std::string>(10);
0107 //   v2 = v1;  // copy assign
0108 //
0109 //   // move assignment
0110 //   auto v1 = absl::variant<int, std::string>("abc");
0111 //   v1 = absl::variant<int, std::string>(10);
0112 //
0113 //   // assignment through type conversion
0114 //   a = 128;         // variant contains int
0115 //   a = "128";       // variant contains std::string
0116 //
0117 // An `absl::variant` holding a value of one of its alternative types `T` holds
0118 // an allocation of `T` directly within the variant itself. An `absl::variant`
0119 // is not allowed to allocate additional storage, such as dynamic memory, to
0120 // allocate the contained value. The contained value shall be allocated in a
0121 // region of the variant storage suitably aligned for all alternative types.
0122 template <typename... Ts>
0123 class variant;
0124 
0125 // swap()
0126 //
0127 // Swaps two `absl::variant` values. This function is equivalent to `v.swap(w)`
0128 // where `v` and `w` are `absl::variant` types.
0129 //
0130 // Note that this function requires all alternative types to be both swappable
0131 // and move-constructible, because any two variants may refer to either the same
0132 // type (in which case, they will be swapped) or to two different types (in
0133 // which case the values will need to be moved).
0134 //
0135 template <
0136     typename... Ts,
0137     absl::enable_if_t<
0138         absl::conjunction<std::is_move_constructible<Ts>...,
0139                           type_traits_internal::IsSwappable<Ts>...>::value,
0140         int> = 0>
0141 void swap(variant<Ts...>& v, variant<Ts...>& w) noexcept(noexcept(v.swap(w))) {
0142   v.swap(w);
0143 }
0144 
0145 // variant_size
0146 //
0147 // Returns the number of alternative types available for a given `absl::variant`
0148 // type as a compile-time constant expression. As this is a class template, it
0149 // is not generally useful for accessing the number of alternative types of
0150 // any given `absl::variant` instance.
0151 //
0152 // Example:
0153 //
0154 //   auto a = absl::variant<int, std::string>;
0155 //   constexpr int num_types =
0156 //       absl::variant_size<absl::variant<int, std::string>>();
0157 //
0158 //   // You can also use the member constant `value`.
0159 //   constexpr int num_types =
0160 //       absl::variant_size<absl::variant<int, std::string>>::value;
0161 //
0162 //   // `absl::variant_size` is more valuable for use in generic code:
0163 //   template <typename Variant>
0164 //   constexpr bool IsVariantMultivalue() {
0165 //       return absl::variant_size<Variant>() > 1;
0166 //   }
0167 //
0168 // Note that the set of cv-qualified specializations of `variant_size` are
0169 // provided to ensure that those specializations compile (especially when passed
0170 // within template logic).
0171 template <class T>
0172 struct variant_size;
0173 
0174 template <class... Ts>
0175 struct variant_size<variant<Ts...>>
0176     : std::integral_constant<std::size_t, sizeof...(Ts)> {};
0177 
0178 // Specialization of `variant_size` for const qualified variants.
0179 template <class T>
0180 struct variant_size<const T> : variant_size<T>::type {};
0181 
0182 // Specialization of `variant_size` for volatile qualified variants.
0183 template <class T>
0184 struct variant_size<volatile T> : variant_size<T>::type {};
0185 
0186 // Specialization of `variant_size` for const volatile qualified variants.
0187 template <class T>
0188 struct variant_size<const volatile T> : variant_size<T>::type {};
0189 
0190 // variant_alternative
0191 //
0192 // Returns the alternative type for a given `absl::variant` at the passed
0193 // index value as a compile-time constant expression. As this is a class
0194 // template resulting in a type, it is not useful for access of the run-time
0195 // value of any given `absl::variant` variable.
0196 //
0197 // Example:
0198 //
0199 //   // The type of the 0th alternative is "int".
0200 //   using alternative_type_0
0201 //     = absl::variant_alternative<0, absl::variant<int, std::string>>::type;
0202 //
0203 //   static_assert(std::is_same<alternative_type_0, int>::value, "");
0204 //
0205 //   // `absl::variant_alternative` is more valuable for use in generic code:
0206 //   template <typename Variant>
0207 //   constexpr bool IsFirstElementTrivial() {
0208 //       return std::is_trivial_v<variant_alternative<0, Variant>::type>;
0209 //   }
0210 //
0211 // Note that the set of cv-qualified specializations of `variant_alternative`
0212 // are provided to ensure that those specializations compile (especially when
0213 // passed within template logic).
0214 template <std::size_t I, class T>
0215 struct variant_alternative;
0216 
0217 template <std::size_t I, class... Types>
0218 struct variant_alternative<I, variant<Types...>> {
0219   using type =
0220       variant_internal::VariantAlternativeSfinaeT<I, variant<Types...>>;
0221 };
0222 
0223 // Specialization of `variant_alternative` for const qualified variants.
0224 template <std::size_t I, class T>
0225 struct variant_alternative<I, const T> {
0226   using type = const typename variant_alternative<I, T>::type;
0227 };
0228 
0229 // Specialization of `variant_alternative` for volatile qualified variants.
0230 template <std::size_t I, class T>
0231 struct variant_alternative<I, volatile T> {
0232   using type = volatile typename variant_alternative<I, T>::type;
0233 };
0234 
0235 // Specialization of `variant_alternative` for const volatile qualified
0236 // variants.
0237 template <std::size_t I, class T>
0238 struct variant_alternative<I, const volatile T> {
0239   using type = const volatile typename variant_alternative<I, T>::type;
0240 };
0241 
0242 // Template type alias for variant_alternative<I, T>::type.
0243 //
0244 // Example:
0245 //
0246 //   using alternative_type_0
0247 //     = absl::variant_alternative_t<0, absl::variant<int, std::string>>;
0248 //   static_assert(std::is_same<alternative_type_0, int>::value, "");
0249 template <std::size_t I, class T>
0250 using variant_alternative_t = typename variant_alternative<I, T>::type;
0251 
0252 // holds_alternative()
0253 //
0254 // Checks whether the given variant currently holds a given alternative type,
0255 // returning `true` if so.
0256 //
0257 // Example:
0258 //
0259 //   absl::variant<int, std::string> foo = 42;
0260 //   if (absl::holds_alternative<int>(foo)) {
0261 //       std::cout << "The variant holds an integer";
0262 //   }
0263 template <class T, class... Types>
0264 constexpr bool holds_alternative(const variant<Types...>& v) noexcept {
0265   static_assert(
0266       variant_internal::UnambiguousIndexOfImpl<variant<Types...>, T,
0267                                                0>::value != sizeof...(Types),
0268       "The type T must occur exactly once in Types...");
0269   return v.index() ==
0270          variant_internal::UnambiguousIndexOf<variant<Types...>, T>::value;
0271 }
0272 
0273 // get()
0274 //
0275 // Returns a reference to the value currently within a given variant, using
0276 // either a unique alternative type amongst the variant's set of alternative
0277 // types, or the variant's index value. Attempting to get a variant's value
0278 // using a type that is not unique within the variant's set of alternative types
0279 // is a compile-time error. If the index of the alternative being specified is
0280 // different from the index of the alternative that is currently stored, throws
0281 // `absl::bad_variant_access`.
0282 //
0283 // Example:
0284 //
0285 //   auto a = absl::variant<int, std::string>;
0286 //
0287 //   // Get the value by type (if unique).
0288 //   int i = absl::get<int>(a);
0289 //
0290 //   auto b = absl::variant<int, int>;
0291 //
0292 //   // Getting the value by a type that is not unique is ill-formed.
0293 //   int j = absl::get<int>(b);     // Compile Error!
0294 //
0295 //   // Getting value by index not ambiguous and allowed.
0296 //   int k = absl::get<1>(b);
0297 
0298 // Overload for getting a variant's lvalue by type.
0299 template <class T, class... Types>
0300 constexpr T& get(variant<Types...>& v) {  // NOLINT
0301   return variant_internal::VariantCoreAccess::CheckedAccess<
0302       variant_internal::IndexOf<T, Types...>::value>(v);
0303 }
0304 
0305 // Overload for getting a variant's rvalue by type.
0306 template <class T, class... Types>
0307 constexpr T&& get(variant<Types...>&& v) {
0308   return variant_internal::VariantCoreAccess::CheckedAccess<
0309       variant_internal::IndexOf<T, Types...>::value>(std::move(v));
0310 }
0311 
0312 // Overload for getting a variant's const lvalue by type.
0313 template <class T, class... Types>
0314 constexpr const T& get(const variant<Types...>& v) {
0315   return variant_internal::VariantCoreAccess::CheckedAccess<
0316       variant_internal::IndexOf<T, Types...>::value>(v);
0317 }
0318 
0319 // Overload for getting a variant's const rvalue by type.
0320 template <class T, class... Types>
0321 constexpr const T&& get(const variant<Types...>&& v) {
0322   return variant_internal::VariantCoreAccess::CheckedAccess<
0323       variant_internal::IndexOf<T, Types...>::value>(std::move(v));
0324 }
0325 
0326 // Overload for getting a variant's lvalue by index.
0327 template <std::size_t I, class... Types>
0328 constexpr variant_alternative_t<I, variant<Types...>>& get(
0329     variant<Types...>& v) {  // NOLINT
0330   return variant_internal::VariantCoreAccess::CheckedAccess<I>(v);
0331 }
0332 
0333 // Overload for getting a variant's rvalue by index.
0334 template <std::size_t I, class... Types>
0335 constexpr variant_alternative_t<I, variant<Types...>>&& get(
0336     variant<Types...>&& v) {
0337   return variant_internal::VariantCoreAccess::CheckedAccess<I>(std::move(v));
0338 }
0339 
0340 // Overload for getting a variant's const lvalue by index.
0341 template <std::size_t I, class... Types>
0342 constexpr const variant_alternative_t<I, variant<Types...>>& get(
0343     const variant<Types...>& v) {
0344   return variant_internal::VariantCoreAccess::CheckedAccess<I>(v);
0345 }
0346 
0347 // Overload for getting a variant's const rvalue by index.
0348 template <std::size_t I, class... Types>
0349 constexpr const variant_alternative_t<I, variant<Types...>>&& get(
0350     const variant<Types...>&& v) {
0351   return variant_internal::VariantCoreAccess::CheckedAccess<I>(std::move(v));
0352 }
0353 
0354 // get_if()
0355 //
0356 // Returns a pointer to the value currently stored within a given variant, if
0357 // present, using either a unique alternative type amongst the variant's set of
0358 // alternative types, or the variant's index value. If such a value does not
0359 // exist, returns `nullptr`.
0360 //
0361 // As with `get`, attempting to get a variant's value using a type that is not
0362 // unique within the variant's set of alternative types is a compile-time error.
0363 
0364 // Overload for getting a pointer to the value stored in the given variant by
0365 // index.
0366 template <std::size_t I, class... Types>
0367 constexpr absl::add_pointer_t<variant_alternative_t<I, variant<Types...>>>
0368 get_if(variant<Types...>* v) noexcept {
0369   return (v != nullptr && v->index() == I)
0370              ? std::addressof(
0371                    variant_internal::VariantCoreAccess::Access<I>(*v))
0372              : nullptr;
0373 }
0374 
0375 // Overload for getting a pointer to the const value stored in the given
0376 // variant by index.
0377 template <std::size_t I, class... Types>
0378 constexpr absl::add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
0379 get_if(const variant<Types...>* v) noexcept {
0380   return (v != nullptr && v->index() == I)
0381              ? std::addressof(
0382                    variant_internal::VariantCoreAccess::Access<I>(*v))
0383              : nullptr;
0384 }
0385 
0386 // Overload for getting a pointer to the value stored in the given variant by
0387 // type.
0388 template <class T, class... Types>
0389 constexpr absl::add_pointer_t<T> get_if(variant<Types...>* v) noexcept {
0390   return absl::get_if<variant_internal::IndexOf<T, Types...>::value>(v);
0391 }
0392 
0393 // Overload for getting a pointer to the const value stored in the given variant
0394 // by type.
0395 template <class T, class... Types>
0396 constexpr absl::add_pointer_t<const T> get_if(
0397     const variant<Types...>* v) noexcept {
0398   return absl::get_if<variant_internal::IndexOf<T, Types...>::value>(v);
0399 }
0400 
0401 // visit()
0402 //
0403 // Calls a provided functor on a given set of variants. `absl::visit()` is
0404 // commonly used to conditionally inspect the state of a given variant (or set
0405 // of variants).
0406 //
0407 // The functor must return the same type when called with any of the variants'
0408 // alternatives.
0409 //
0410 // Example:
0411 //
0412 //   // Define a visitor functor
0413 //   struct GetVariant {
0414 //       template<typename T>
0415 //       void operator()(const T& i) const {
0416 //         std::cout << "The variant's value is: " << i;
0417 //       }
0418 //   };
0419 //
0420 //   // Declare our variant, and call `absl::visit()` on it.
0421 //   // Note that `GetVariant()` returns void in either case.
0422 //   absl::variant<int, std::string> foo = std::string("foo");
0423 //   GetVariant visitor;
0424 //   absl::visit(visitor, foo);  // Prints `The variant's value is: foo'
0425 template <typename Visitor, typename... Variants>
0426 variant_internal::VisitResult<Visitor, Variants...> visit(Visitor&& vis,
0427                                                           Variants&&... vars) {
0428   return variant_internal::
0429       VisitIndices<variant_size<absl::decay_t<Variants> >::value...>::Run(
0430           variant_internal::PerformVisitation<Visitor, Variants...>{
0431               std::forward_as_tuple(std::forward<Variants>(vars)...),
0432               std::forward<Visitor>(vis)},
0433           vars.index()...);
0434 }
0435 
0436 // monostate
0437 //
0438 // The monostate class serves as a first alternative type for a variant for
0439 // which the first variant type is otherwise not default-constructible.
0440 struct monostate {};
0441 
0442 // `absl::monostate` Relational Operators
0443 
0444 constexpr bool operator<(monostate, monostate) noexcept { return false; }
0445 constexpr bool operator>(monostate, monostate) noexcept { return false; }
0446 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
0447 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
0448 constexpr bool operator==(monostate, monostate) noexcept { return true; }
0449 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
0450 
0451 
0452 //------------------------------------------------------------------------------
0453 // `absl::variant` Template Definition
0454 //------------------------------------------------------------------------------
0455 template <typename T0, typename... Tn>
0456 class variant<T0, Tn...> : private variant_internal::VariantBase<T0, Tn...> {
0457   static_assert(absl::conjunction<std::is_object<T0>,
0458                                   std::is_object<Tn>...>::value,
0459                 "Attempted to instantiate a variant containing a non-object "
0460                 "type.");
0461   // Intentionally not qualifying `negation` with `absl::` to work around a bug
0462   // in MSVC 2015 with inline namespace and variadic template.
0463   static_assert(absl::conjunction<negation<std::is_array<T0> >,
0464                                   negation<std::is_array<Tn> >...>::value,
0465                 "Attempted to instantiate a variant containing an array type.");
0466   static_assert(absl::conjunction<std::is_nothrow_destructible<T0>,
0467                                   std::is_nothrow_destructible<Tn>...>::value,
0468                 "Attempted to instantiate a variant containing a non-nothrow "
0469                 "destructible type.");
0470 
0471   friend struct variant_internal::VariantCoreAccess;
0472 
0473  private:
0474   using Base = variant_internal::VariantBase<T0, Tn...>;
0475 
0476  public:
0477   // Constructors
0478 
0479   // Constructs a variant holding a default-initialized value of the first
0480   // alternative type.
0481   constexpr variant() /*noexcept(see 111above)*/ = default;
0482 
0483   // Copy constructor, standard semantics
0484   variant(const variant& other) = default;
0485 
0486   // Move constructor, standard semantics
0487   variant(variant&& other) /*noexcept(see above)*/ = default;
0488 
0489   // Constructs a variant of an alternative type specified by overload
0490   // resolution of the provided forwarding arguments through
0491   // direct-initialization.
0492   //
0493   // Note: If the selected constructor is a constexpr constructor, this
0494   // constructor shall be a constexpr constructor.
0495   //
0496   // NOTE: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r1.html
0497   // has been voted passed the design phase in the C++ standard meeting in Mar
0498   // 2018. It will be implemented and integrated into `absl::variant`.
0499   template <
0500       class T,
0501       std::size_t I = std::enable_if<
0502           variant_internal::IsNeitherSelfNorInPlace<variant,
0503                                                     absl::decay_t<T> >::value,
0504           variant_internal::IndexOfConstructedType<variant, T> >::type::value,
0505       class Tj = absl::variant_alternative_t<I, variant>,
0506       absl::enable_if_t<std::is_constructible<Tj, T>::value>* = nullptr>
0507   constexpr variant(T&& t) noexcept(std::is_nothrow_constructible<Tj, T>::value)
0508       : Base(variant_internal::EmplaceTag<I>(), std::forward<T>(t)) {}
0509 
0510   // Constructs a variant of an alternative type from the arguments through
0511   // direct-initialization.
0512   //
0513   // Note: If the selected constructor is a constexpr constructor, this
0514   // constructor shall be a constexpr constructor.
0515   template <class T, class... Args,
0516             typename std::enable_if<std::is_constructible<
0517                 variant_internal::UnambiguousTypeOfT<variant, T>,
0518                 Args...>::value>::type* = nullptr>
0519   constexpr explicit variant(in_place_type_t<T>, Args&&... args)
0520       : Base(variant_internal::EmplaceTag<
0521                  variant_internal::UnambiguousIndexOf<variant, T>::value>(),
0522              std::forward<Args>(args)...) {}
0523 
0524   // Constructs a variant of an alternative type from an initializer list
0525   // and other arguments through direct-initialization.
0526   //
0527   // Note: If the selected constructor is a constexpr constructor, this
0528   // constructor shall be a constexpr constructor.
0529   template <class T, class U, class... Args,
0530             typename std::enable_if<std::is_constructible<
0531                 variant_internal::UnambiguousTypeOfT<variant, T>,
0532                 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0533   constexpr explicit variant(in_place_type_t<T>, std::initializer_list<U> il,
0534                              Args&&... args)
0535       : Base(variant_internal::EmplaceTag<
0536                  variant_internal::UnambiguousIndexOf<variant, T>::value>(),
0537              il, std::forward<Args>(args)...) {}
0538 
0539   // Constructs a variant of an alternative type from a provided index,
0540   // through value-initialization using the provided forwarded arguments.
0541   template <std::size_t I, class... Args,
0542             typename std::enable_if<std::is_constructible<
0543                 variant_internal::VariantAlternativeSfinaeT<I, variant>,
0544                 Args...>::value>::type* = nullptr>
0545   constexpr explicit variant(in_place_index_t<I>, Args&&... args)
0546       : Base(variant_internal::EmplaceTag<I>(), std::forward<Args>(args)...) {}
0547 
0548   // Constructs a variant of an alternative type from a provided index,
0549   // through value-initialization of an initializer list and the provided
0550   // forwarded arguments.
0551   template <std::size_t I, class U, class... Args,
0552             typename std::enable_if<std::is_constructible<
0553                 variant_internal::VariantAlternativeSfinaeT<I, variant>,
0554                 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0555   constexpr explicit variant(in_place_index_t<I>, std::initializer_list<U> il,
0556                              Args&&... args)
0557       : Base(variant_internal::EmplaceTag<I>(), il,
0558              std::forward<Args>(args)...) {}
0559 
0560   // Destructors
0561 
0562   // Destroys the variant's currently contained value, provided that
0563   // `absl::valueless_by_exception()` is false.
0564   ~variant() = default;
0565 
0566   // Assignment Operators
0567 
0568   // Copy assignment operator
0569   variant& operator=(const variant& other) = default;
0570 
0571   // Move assignment operator
0572   variant& operator=(variant&& other) /*noexcept(see above)*/ = default;
0573 
0574   // Converting assignment operator
0575   //
0576   // NOTE: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r1.html
0577   // has been voted passed the design phase in the C++ standard meeting in Mar
0578   // 2018. It will be implemented and integrated into `absl::variant`.
0579   template <
0580       class T,
0581       std::size_t I = std::enable_if<
0582           !std::is_same<absl::decay_t<T>, variant>::value,
0583           variant_internal::IndexOfConstructedType<variant, T>>::type::value,
0584       class Tj = absl::variant_alternative_t<I, variant>,
0585       typename std::enable_if<std::is_assignable<Tj&, T>::value &&
0586                               std::is_constructible<Tj, T>::value>::type* =
0587           nullptr>
0588   variant& operator=(T&& t) noexcept(
0589       std::is_nothrow_assignable<Tj&, T>::value&&
0590           std::is_nothrow_constructible<Tj, T>::value) {
0591     variant_internal::VisitIndices<sizeof...(Tn) + 1>::Run(
0592         variant_internal::VariantCoreAccess::MakeConversionAssignVisitor(
0593             this, std::forward<T>(t)),
0594         index());
0595 
0596     return *this;
0597   }
0598 
0599 
0600   // emplace() Functions
0601 
0602   // Constructs a value of the given alternative type T within the variant. The
0603   // existing value of the variant is destroyed first (provided that
0604   // `absl::valueless_by_exception()` is false). Requires that T is unambiguous
0605   // in the variant.
0606   //
0607   // Example:
0608   //
0609   //   absl::variant<std::vector<int>, int, std::string> v;
0610   //   v.emplace<int>(99);
0611   //   v.emplace<std::string>("abc");
0612   template <
0613       class T, class... Args,
0614       typename std::enable_if<std::is_constructible<
0615           absl::variant_alternative_t<
0616               variant_internal::UnambiguousIndexOf<variant, T>::value, variant>,
0617           Args...>::value>::type* = nullptr>
0618   T& emplace(Args&&... args) {
0619     return variant_internal::VariantCoreAccess::Replace<
0620         variant_internal::UnambiguousIndexOf<variant, T>::value>(
0621         this, std::forward<Args>(args)...);
0622   }
0623 
0624   // Constructs a value of the given alternative type T within the variant using
0625   // an initializer list. The existing value of the variant is destroyed first
0626   // (provided that `absl::valueless_by_exception()` is false). Requires that T
0627   // is unambiguous in the variant.
0628   //
0629   // Example:
0630   //
0631   //   absl::variant<std::vector<int>, int, std::string> v;
0632   //   v.emplace<std::vector<int>>({0, 1, 2});
0633   template <
0634       class T, class U, class... Args,
0635       typename std::enable_if<std::is_constructible<
0636           absl::variant_alternative_t<
0637               variant_internal::UnambiguousIndexOf<variant, T>::value, variant>,
0638           std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0639   T& emplace(std::initializer_list<U> il, Args&&... args) {
0640     return variant_internal::VariantCoreAccess::Replace<
0641         variant_internal::UnambiguousIndexOf<variant, T>::value>(
0642         this, il, std::forward<Args>(args)...);
0643   }
0644 
0645   // Destroys the current value of the variant (provided that
0646   // `absl::valueless_by_exception()` is false) and constructs a new value at
0647   // the given index.
0648   //
0649   // Example:
0650   //
0651   //   absl::variant<std::vector<int>, int, int> v;
0652   //   v.emplace<1>(99);
0653   //   v.emplace<2>(98);
0654   //   v.emplace<int>(99);  // Won't compile. 'int' isn't a unique type.
0655   template <std::size_t I, class... Args,
0656             typename std::enable_if<
0657                 std::is_constructible<absl::variant_alternative_t<I, variant>,
0658                                       Args...>::value>::type* = nullptr>
0659   absl::variant_alternative_t<I, variant>& emplace(Args&&... args) {
0660     return variant_internal::VariantCoreAccess::Replace<I>(
0661         this, std::forward<Args>(args)...);
0662   }
0663 
0664   // Destroys the current value of the variant (provided that
0665   // `absl::valueless_by_exception()` is false) and constructs a new value at
0666   // the given index using an initializer list and the provided arguments.
0667   //
0668   // Example:
0669   //
0670   //   absl::variant<std::vector<int>, int, int> v;
0671   //   v.emplace<0>({0, 1, 2});
0672   template <std::size_t I, class U, class... Args,
0673             typename std::enable_if<std::is_constructible<
0674                 absl::variant_alternative_t<I, variant>,
0675                 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0676   absl::variant_alternative_t<I, variant>& emplace(std::initializer_list<U> il,
0677                                                    Args&&... args) {
0678     return variant_internal::VariantCoreAccess::Replace<I>(
0679         this, il, std::forward<Args>(args)...);
0680   }
0681 
0682   // variant::valueless_by_exception()
0683   //
0684   // Returns false if and only if the variant currently holds a valid value.
0685   constexpr bool valueless_by_exception() const noexcept {
0686     return this->index_ == absl::variant_npos;
0687   }
0688 
0689   // variant::index()
0690   //
0691   // Returns the index value of the variant's currently selected alternative
0692   // type.
0693   constexpr std::size_t index() const noexcept { return this->index_; }
0694 
0695   // variant::swap()
0696   //
0697   // Swaps the values of two variant objects.
0698   //
0699   void swap(variant& rhs) noexcept(
0700       absl::conjunction<
0701           std::is_nothrow_move_constructible<T0>,
0702           std::is_nothrow_move_constructible<Tn>...,
0703           type_traits_internal::IsNothrowSwappable<T0>,
0704           type_traits_internal::IsNothrowSwappable<Tn>...>::value) {
0705     return variant_internal::VisitIndices<sizeof...(Tn) + 1>::Run(
0706         variant_internal::Swap<T0, Tn...>{this, &rhs}, rhs.index());
0707   }
0708 };
0709 
0710 // We need a valid declaration of variant<> for SFINAE and overload resolution
0711 // to work properly above, but we don't need a full declaration since this type
0712 // will never be constructed. This declaration, though incomplete, suffices.
0713 template <>
0714 class variant<>;
0715 
0716 //------------------------------------------------------------------------------
0717 // Relational Operators
0718 //------------------------------------------------------------------------------
0719 //
0720 // If neither operand is in the `variant::valueless_by_exception` state:
0721 //
0722 //   * If the index of both variants is the same, the relational operator
0723 //     returns the result of the corresponding relational operator for the
0724 //     corresponding alternative type.
0725 //   * If the index of both variants is not the same, the relational operator
0726 //     returns the result of that operation applied to the value of the left
0727 //     operand's index and the value of the right operand's index.
0728 //   * If at least one operand is in the valueless_by_exception state:
0729 //     - A variant in the valueless_by_exception state is only considered equal
0730 //       to another variant in the valueless_by_exception state.
0731 //     - If exactly one operand is in the valueless_by_exception state, the
0732 //       variant in the valueless_by_exception state is less than the variant
0733 //       that is not in the valueless_by_exception state.
0734 //
0735 // Note: The value 1 is added to each index in the relational comparisons such
0736 // that the index corresponding to the valueless_by_exception state wraps around
0737 // to 0 (the lowest value for the index type), and the remaining indices stay in
0738 // the same relative order.
0739 
0740 // Equal-to operator
0741 template <typename... Types>
0742 constexpr variant_internal::RequireAllHaveEqualT<Types...> operator==(
0743     const variant<Types...>& a, const variant<Types...>& b) {
0744   return (a.index() == b.index()) &&
0745          variant_internal::VisitIndices<sizeof...(Types)>::Run(
0746              variant_internal::EqualsOp<Types...>{&a, &b}, a.index());
0747 }
0748 
0749 // Not equal operator
0750 template <typename... Types>
0751 constexpr variant_internal::RequireAllHaveNotEqualT<Types...> operator!=(
0752     const variant<Types...>& a, const variant<Types...>& b) {
0753   return (a.index() != b.index()) ||
0754          variant_internal::VisitIndices<sizeof...(Types)>::Run(
0755              variant_internal::NotEqualsOp<Types...>{&a, &b}, a.index());
0756 }
0757 
0758 // Less-than operator
0759 template <typename... Types>
0760 constexpr variant_internal::RequireAllHaveLessThanT<Types...> operator<(
0761     const variant<Types...>& a, const variant<Types...>& b) {
0762   return (a.index() != b.index())
0763              ? (a.index() + 1) < (b.index() + 1)
0764              : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0765                    variant_internal::LessThanOp<Types...>{&a, &b}, a.index());
0766 }
0767 
0768 // Greater-than operator
0769 template <typename... Types>
0770 constexpr variant_internal::RequireAllHaveGreaterThanT<Types...> operator>(
0771     const variant<Types...>& a, const variant<Types...>& b) {
0772   return (a.index() != b.index())
0773              ? (a.index() + 1) > (b.index() + 1)
0774              : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0775                    variant_internal::GreaterThanOp<Types...>{&a, &b},
0776                    a.index());
0777 }
0778 
0779 // Less-than or equal-to operator
0780 template <typename... Types>
0781 constexpr variant_internal::RequireAllHaveLessThanOrEqualT<Types...> operator<=(
0782     const variant<Types...>& a, const variant<Types...>& b) {
0783   return (a.index() != b.index())
0784              ? (a.index() + 1) < (b.index() + 1)
0785              : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0786                    variant_internal::LessThanOrEqualsOp<Types...>{&a, &b},
0787                    a.index());
0788 }
0789 
0790 // Greater-than or equal-to operator
0791 template <typename... Types>
0792 constexpr variant_internal::RequireAllHaveGreaterThanOrEqualT<Types...>
0793 operator>=(const variant<Types...>& a, const variant<Types...>& b) {
0794   return (a.index() != b.index())
0795              ? (a.index() + 1) > (b.index() + 1)
0796              : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0797                    variant_internal::GreaterThanOrEqualsOp<Types...>{&a, &b},
0798                    a.index());
0799 }
0800 
0801 ABSL_NAMESPACE_END
0802 }  // namespace absl
0803 
0804 namespace std {
0805 
0806 // hash()
0807 template <>  // NOLINT
0808 struct hash<absl::monostate> {
0809   std::size_t operator()(absl::monostate) const { return 0; }
0810 };
0811 
0812 template <class... T>  // NOLINT
0813 struct hash<absl::variant<T...>>
0814     : absl::variant_internal::VariantHashBase<absl::variant<T...>, void,
0815                                               absl::remove_const_t<T>...> {};
0816 
0817 }  // namespace std
0818 
0819 #endif  // ABSL_USES_STD_VARIANT
0820 
0821 namespace absl {
0822 ABSL_NAMESPACE_BEGIN
0823 namespace variant_internal {
0824 
0825 // Helper visitor for converting a variant<Ts...>` into another type (mostly
0826 // variant) that can be constructed from any type.
0827 template <typename To>
0828 struct ConversionVisitor {
0829   template <typename T>
0830   To operator()(T&& v) const {
0831     return To(std::forward<T>(v));
0832   }
0833 };
0834 
0835 }  // namespace variant_internal
0836 
0837 // ConvertVariantTo()
0838 //
0839 // Helper functions to convert an `absl::variant` to a variant of another set of
0840 // types, provided that the alternative type of the new variant type can be
0841 // converted from any type in the source variant.
0842 //
0843 // Example:
0844 //
0845 //   absl::variant<name1, name2, float> InternalReq(const Req&);
0846 //
0847 //   // name1 and name2 are convertible to name
0848 //   absl::variant<name, float> ExternalReq(const Req& req) {
0849 //     return absl::ConvertVariantTo<absl::variant<name, float>>(
0850 //              InternalReq(req));
0851 //   }
0852 template <typename To, typename Variant>
0853 To ConvertVariantTo(Variant&& variant) {
0854   return absl::visit(variant_internal::ConversionVisitor<To>{},
0855                      std::forward<Variant>(variant));
0856 }
0857 
0858 ABSL_NAMESPACE_END
0859 }  // namespace absl
0860 
0861 #endif  // ABSL_TYPES_VARIANT_H_