[#bsl-variant] = xref:bsl.adoc[bsl]::variant :relfileprefix: ../ :mrdocs: == Synopsis Declared in `<bslstl_variant.h>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- template< class t_HEAD, class... t_TAIL> class variant; ---- == Type Aliases [cols="1,4"] |=== | Name| Description | xref:bsl/variant/allocator_type.adoc[`allocator_type`] | Type alias to the allocator type used by `variant`. |=== == Member Functions [cols="1,4"] |=== | Name| Description | xref:bsl/variant/2constructor-0e.adoc[`variant`] [.small]#[constructor]# | Constructors | xref:bsl/variant/operator_assign-07.adoc[`operator=`] | Assignment operators | xref:bsl/variant/emplace-03.adoc[`emplace`] | `emplace` overloads | xref:bsl/variant/get_allocator.adoc[`get_allocator`] | Return a copy of this object's allocator. At least one alternative of this `variant` object shall be allocator‐aware; in C++03, due to language limitations, this method can be called even if no alternative is allocator‐aware, but returns a non‐allocator type in that case. | xref:bsl/variant/index.adoc[`index`] | Return the index of the alternative currently managed by this `variant` object, or `bsl::variant_npos` if this object is valueless by exception. This method differs from the standard in the following way: * `constexpr` is not implemented This is because no constructors are currently constexpr and there is no way to test the constexpr property of this function. | xref:bsl/variant/swap.adoc[`swap`] | Efficiently exchange the value of this object with the value of the specified `other` object. This method provides the no‐throw exception‐safety guarantee if the two swapped objects contain the same alternative and if that alternative `t_TYPE` provides that guarantee. If `*this` and `other` do not have the same active alternative and this method exits via an exception, either or both `variant` objects may be left in a valueless state or with an contained value in a moved‐from state. The behavior is undefined unless `*this` has the same allocator as `other`. All alternatives shall be move constructible and swappable. For simplicity of implementation, this method differs from the standard in the following ways: * `constexpr` is not implemented * `noexcept` specification is not implemented | xref:bsl/variant/valueless_by_exception.adoc[`valueless_by_exception`] | Return `false` if there is an alternative object currently managed by this `variant` object, and `true` otherwise. A `variant` object can become valueless by exception if the creation of an alternative object exits via an exception, or if it is copied or assigned from another `variant` object that is valueless by exception. This method differs from the standard in the following way: * `constexpr` is not implemented This is because no constructors are currently constexpr and there is no way to test the constexpr property of this function. | xref:bsl/variant/2conversion-0e.adoc[`operator BloombergLP::bslmf::NestedTraitDeclaration<variant, IsBitwiseMoveable, (BloombergLP::bslstl::Variant_IsBitwiseMoveableAll<t_HEAD, t_TAIL...>::value)>`] | Nested trait declaration for `IsBitwiseMoveable`. | xref:bsl/variant/2conversion-02.adoc[`operator BloombergLP::bslmf::NestedTraitDeclaration<variant, UsesAllocatorArgT, (BloombergLP::bslstl::Variant_UsesBslmaAllocatorAny<t_HEAD, t_TAIL...>::value)>`] | Nested trait declaration for `UsesAllocatorArgT`. | xref:bsl/variant/2conversion-0b.adoc[`operator BloombergLP::bslmf::NestedTraitDeclaration<variant, UsesBslmaAllocator, (BloombergLP::bslstl::Variant_UsesBslmaAllocatorAny<t_HEAD, t_TAIL...>::value)>`] | Nested trait declaration for `UsesBslmaAllocator`. |=== == Friends [cols="1,4"] |=== | Name| Description | `xref:BloombergLP/bslstl/Variant_Base.adoc[BloombergLP::bslstl::Variant_Base]` | This component‐private struct defines the data representation of `bsl::variant` and contains the implementations of its special member functions (which must be defaulted in `bsl::variant` itself). This class also contains implementations of additional constructors and methods needed for the correct functionality of the `variant` class hierarchy. |=== == Non-Member Functions [cols="1,4"] |=== | Name| Description | xref:bsl/get-002.adoc[`get`] | See the first `get` overload above. | xref:bsl/get-035.adoc[`get`] | See the first `get` overload above. | xref:bsl/get-06.adoc[`get`] | See the first `get` overload above. | xref:bsl/get-0b5.adoc[`get`] | Return a reference to the alternative object at index (template parameter) `t_INDEX` in the specified `obj`. If `t_INDEX` is not the index of the currently active alternative, throw an exception of type `bad_variant_access`. `t_INDEX` shall be a valid index for the variant type of `obj`. | xref:bsl/get-0e3.adoc[`get`] | See the first `get` overload above. | xref:bsl/get-0e6.adoc[`get`] | See the first `get` overload above. | xref:bsl/get-0ea.adoc[`get`] | Return a reference to the alternative object with type (template parameter) `t_TYPE` in the specified `obj`. If `t_TYPE` is not the type of the currently active alternative, throw an exception of type `bad_variant_access`. `t_TYPE` shall appear exactly once in the variant's list of alternatives. | xref:bsl/get-0ee.adoc[`get`] | See the first `get` overload above. | xref:bsl/get_if-06.adoc[`get_if`] | Return a pointer to the alternative object with type (template parameter) `t_TYPE` in the specified `obj`, or a null pointer if `obj` itself is a null pointer or if `t_TYPE` is not the type of the currently active alternative. `t_TYPE` shall appear exactly once in the variant's list of alternatives. | xref:bsl/get_if-09.adoc[`get_if`] | Return a pointer to the alternative object with type (template parameter) `t_TYPE` in the specified `obj`, or a null pointer if `obj` itself is a null pointer or if `t_TYPE` is not the type of the currently active alternative. `t_TYPE` shall appear exactly once in the variant's list of alternatives. | xref:bsl/get_if-0a.adoc[`get_if`] | Return a pointer to the alternative at index `t_INDEX`, or null. | xref:bsl/get_if-0d.adoc[`get_if`] | Return a pointer to the alternative object with index (template parameter) `t_INDEX` in the specified `obj`, or a null pointer if `obj` itself is a null pointer or if `t_INDEX` is not the index of the currently active alternative. `t_INDEX` shall be a valid alternative index. | xref:bsl/holds_alternative.adoc[`holds_alternative`] | Return `true` if the specified `obj` currently holds the (template parameter) `t_TYPE` alternative, and `false` otherwise. `t_TYPE` shall appear exactly once in the variant's list of alternatives. | xref:bsl/operator_not_eq-023.adoc[`operator!=`] | Return `true` if the specified `lhs` and `rhs` have different active alternatives or only one holds an alternative, or if they have the same active alternative and the contained values compare unequal; otherwise, return `false`. All alternatives shall support `operator!=`. | xref:bsl/operator_lt-01d.adoc[`operator<`] | Return `true` if the index of the active alternative in the specified `lhs` is less than that of the specified `rhs`, or if both have the same active alternative and the contained value of `lhs` compares less than that of `rhs`, or if `lhs` is valueless by exception and `rhs` is not; otherwise, return `false`. All alternatives shall support `operator<`. | xref:bsl/operator_le-00.adoc[`operator<=`] | Return `true` if the index of the active alternative in the specified `lhs` is less than that of the specified `rhs`, or if both have the same active alternative and the contained value of `lhs` compares less than or equal to that of `rhs`, or if `lhs` is valueless by exception; otherwise, return `false`. All alternatives shall support `operator<=`. | xref:bsl/operator_3way-09.adoc[`operator<=>`] | If `lhs` and `rhs` are not valueless by exception and hold the same alternative `i`, return `get(lhs) <=> get(rhs)` . If `lhs` and `rhs` are not valueless by exception and hold the aalternatives `i` and `j` respectively, return `i<=>j`. Return `strong_ordering::equal` if both variants are valueless by exception. Return `strong_ordering::less` if `lhs` is valueless by exception. Return `strong_ordering::greater` if `rhs` is valueless by exception. | xref:bsl/operator_eq-058.adoc[`operator==`] | Return `true` if the specified `lhs` and `rhs` are both valueless by exception or if they have the same active alternative and their contained values compare equal; otherwise, return `false`. All alternatives shall support `operator==`. | xref:bsl/operator_gt-014.adoc[`operator>`] | Return `true` if the index of the active alternative in the specified `lhs` is greater than that of the specified `rhs`, or if both have the same active alternative and the contained value of `lhs` compares greater than that of `rhs`, or if `rhs` is valueless by exception and `lhs` is not; otherwise, return `false`. All alternatives shall support `operator>`. | xref:bsl/operator_ge-07a.adoc[`operator>=`] | Return `true` if the index of the active alternative in the specified `lhs` is greater than that of the specified `rhs`, or if both have the same active alternative and the contained value of `lhs` compares greater than or equal to that of `rhs`, or if `rhs` is valueless by exception; otherwise, return `false`. All alternatives shall support `operator>=`. | xref:bsl/swap-0f5.adoc[`swap`] | Efficiently exchange the values of the specified `lhs` and `rhs` objects. This method provides the no‐throw guarantee if the two variant objects being swapped have the same active alternative and that alternative provides that guarantee; otherwise, this method provides the basic guarantee. If `lhs` and `rhs` do not contain the same alternative or they have unequal allocators, and an exception is thrown during the swap, either or both variant objects may be left in a valueless state or with an alternative in a moved‐from state. All alternatives shall be move constructible and swappable. For simplicity of implementation, this function differs from the standard in the following : * constraints are not implemented * constexpr is not implemented |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#