Union safe reviews

WORLD OF SAFES, LLC; UNION SAFE PLUS, SHOP

Специализация: Unfortunately at this point in our guide there is no information about the specialization of the organization. If You are a representative please contact the website and fill in the blanks. This will attract more attention of potential customers. Похожие организации:

Below is a map of Saratov. World of safes, llc; union safe plus, shop the map shows the corresponding marker in the center. To zoom, use the slider at the top left of the map. To switch map mode to "satellite imaging" click the Layers button in the upper right part of the map. Map will help You to build the shortest and fastest route to the address 410000, Saratov city, kutyakova str., 37 traffic! To build a route, click the button in the top left corner. To view the current actual state of traffic congestion - press the "Tube".


Variant: a type-safe union for C++17 (v7).

Variant is the very spice of life,

That gives it all its flavor.

- William Cowper's "The Task", or actually a variant thereof

Lets not make the same mistake we made with std::optional by putting this library into a TS. We waited three years where no substantial feedback or discussion occurred, and then moved it into the IS virtually unchanged. Meanwhile, the C++ community suffered, and we continue to suffer from lack of this essential vocabulary type in interfaces.

The implications of the consensus variant design are well understood and have been explored over several LEWG discussions, over a thousand emails, a joint LEWG/EWG session, and not to mention 12 years of experience with Boost and other libraries. The last major change made to the proposal was non-breaking and added exception throws where previously there was undefined behavior. Since then, all suggested modifications have been cosmetic, rehashes of older discussions, or would be handled just as well by defect resolutions.

The C++ community should not wait three years for a widely useful library that is already done, fits its purpose, and has had such extensive review. There is a low chance that we will regret including variant in C++17, but a high chance that we will regret omitting it.

This proposal attempts to apply the lessons learned from optional (1) . It behaves as below:

Results of the LEWG review in Urbana

The LEWG review in Urbana resulted in the following straw polls that motivated changes in this revision of the paper:

  • Should we use a tuple -like interface instead of the collection of variant -specific functions, is_alternative etc.? SF=8 WF=5 N=2 WA=1 SA=0
  • Consent: variant should be as constexpr as std::optional
  • Consent: The paper should discuss the never-empty guarantee
  • Consent: Expand on variant<int, int> and variant<int, const int> .
  • Visitors are needed for the initial variant in the TS? SF=4 WF=3 N=5 WA=4 SA=0
  • Recursive variants are needed? SF=0 WF=0 N=8 WA=4 SA=2

Results of the LEWG review in Lenexa

In Lenexa, LEWG decided that variant should model a discriminated union.

  • Approval votes on emptiness:
  • empty, queryable state: 12
  • invalid, assignable, UB on read: 13
  • invalid, throws on read: 6
  • double buffer: 5
  • require all members nothrow-move-constructible: 1
  • require either move-noexcept or one-default-construct-noexcept: 0
  • Want to query whether in empty state: SF=4 WF=4 N=4 WA=1 SA=1
  • Should the default constructor lead to the empty state? SF=3 WF=1 N=3 WA=1 SA=5; later SF=2 WF=0 N=2 WA=1 SA=6
  • Should the default constructor try to construct the first element? SF=5 WF=3 N=1 WA=2 SA=2, later SF=6 WF=3 N=0 WA=1 SA=1
  • Should the default constructor search for a default-constructible type and take the first possible one? (no earlier poll), later SF=0 WF=1 N=2 WA=5 SA=3
  • Remove heterogeneous assignment? SF=9 WF=5 N=3 WA=0 SA=1
  • Remove conversions, e.g. variant<int, string> x = "abc9quot;; ? SF=5 WF=4 N=1 WA=1 SA=0
  • Allow variant<string9gt; == const char * and variant<const char *, string> == const char * ? SF=0 WF=2 N=5 WA=3 SA=3
  • Allow variant<string9gt; == variant<const char *> , and variant<A, B, C> == variant<X, Y, Z> ? SF=0 WF=1 N=0 WA=4 SA=8
  • Allow variant<int, const int> , qualified types in general? SF=9 WF=4 N=1 WA=1 SA=1
  • Allow types to be reference types? SF=6 WF=4 N=6 WA=1 SA=0
  • Allow void? SF=6 WF=9 N=2 WA=0 SA=0
  • Provide multi-visitation visit(VISITOR, var1, var2, var3, . ) ? SF=0 WF=7 N=7 WA=1 SA=0
  • Provide binary visitation visit(VISITOR, v1, v2) ? SF=0 WF=1 N=10 WA=1 SA=3
  • Approval vote of visitor return types:
  • common_type : 12
  • require same return type: 13
  • return type of op()() , rest must convert to that: 1
  • variant<return types> : 2
  • variant<return types> if they're different, otherwise single return type: 0
  • no void * data()
  • yes T* get<T9gt;(variant9lt;A, B, C> *) (a la any_cast )
  • Should index() return -1 on empty? (The alternative is to make non-emptiness a precondition.) SF=4 WF=1 N=3 WA=1 SA=2
  • Should variant:: have preconditions that the variant not be empty? SF=4 WF=8 N=2 WA=0 SA=0

Results of the second LEWG review in Lenexa

  • Name of empty state:
  • empty: 0
  • error: 6
  • invalid: 14
  • bad: 5
  • fail: 0
  • partially formed: 4
  • Name of query function:
    • query function: valid 13
    • is_valid 2
    • invalid 1
    • is_invalid 2
    • explicit operator bool 7
    • index() == tuple_not_found 10
  • Upon invalid, should index return a magic value? SF=5, F=3, N=1, A=2, SA=2
  • index() has a precondition of being valid() (otherwise UB) SF=5 F=2 N=0 A=3 SA=3
  • What do we want to call the "empty_t" stand-in type?
    • empty_t 4
    • empty 4
    • one_t 1
    • blank 6
    • blank_t 7
    • monostate 7
    Runoff:
    • blank* 3
    • monostate 8
  • Add assignment from an exact type if the type is unique? Unanimous consent.
  • Add an example of multi-visitation; change visit() to a variadic signature.
  • Keep names in_place_type and in_place_index to be consistent with optional? General consent.

Results of Evening Session review in Kona

  • Do we want P0088R0 + exceptions on invalid (so no undefined behavior)? SF=13, F=15, N=2, A=3, SA=0

Results of LEWG Session in Kona

This addressed items raised by LWG.

  • LEWG accepted (and embraced!) LWG's proposal for the overload resolution mechanism for template <class T> variant operator=(T&9amp;) , using a hypothetical function taking the alternative types.
  • Allow conversion in both construction and assignment. The detailed polls were:
    • keep assignment and construction asymmetrical: SF=0, F=0, N=1, A=7, SA=6
    • restrict assign and construction to alternative types only: SF=2, F=5, N=4, A=3, SA=4
    • allow conversion for construction and assignment: SF=4, F=4, N=3, A=4, SA=0
  • Allow visitation without passing a variant, i.e. visit(Visitor) .
  • Strong consensus to rename valid() to !corrupted_by_exception() (14 votes, runner-up: 7 votes) that was later on changed to !valueless_by_exception() after a discussion and based on a poll on the LWG and LEWG email lists, with 32 responses.

Differences to revision 1 (N4218)

As requested by the LEWG review in Urbana, this revision

  • considerably expands the discussion of why this proposal allows the variant to be empty;
  • explains how duplicate (possibly cv-qualified) types and void as alternatives behave;
  • reuses (and extends, for consistency) the facilities provided by tuple for parameter pack operations; is_alternative does not yet exist as part of tuple and is thus kept;
  • employs the "perfect initialization" approach to for explicit conversions (2) ;
  • changes index() to return -1 (now also known is tuple_not_found ) if !valid() ;
  • adds a visitation interface.

Beyond these requests, this revision

  • discusses the options for relational operators, construction and assignments, with / from a same-type variant , an alternative, and a different variant type;
  • hopefully makes the variant a regular type.

Differences to revision 2 (N4450)

  • Everything requested by LEWG, most notably, variant now models a discriminated union.
  • hash<variant9lt;int9gt;9gt; can now return different values than hash<int9gt; (and it should - presumably it should take the index() into account).
  • Describe template <size_t. > get<I. >(variant) .
  • Remove is_alternative that is not strictly needed to make variant usable (LEWG feedback).
  • Remove std::swap() specialization; the default is just fine.
  • Add obligatory introductory quote.
  • Expanded on disadvantages of double buffering.

Differences to revision 3 (N4516)

  • Added discussion of (semi-) destructive move.
  • Assignment from an alternative types are back.
  • Multi-visitation example added.
  • visit() is now variadic.
  • Implemented several suggestions by Peter Dimov: removed type_list ; reduced probability of !valid() for copy assignment / construction.
  • Renamed to monostate, get_if().

Differences to revision 4 (N4542)

  • Make valid() a visible state for value extraction functions ( get() , visit() ).
  • Move general design discussion into P0086.
  • Remove valid() precondition for copy / move construction from a variant .

Differences to revision 5 (P0088R0)

  • The Kona compromise: f !v.valid() , make get<. >(v) and visit(v) throw.
  • Change the overload resolution mechanism for template <class T> variant::variant(T&9amp;) and template <class T> variant::operator=(T&9amp;) , using a hypothetical function taking the alternative types.
  • Allow conversion in both construction and assignment.
  • Allow visitation without passing a variant, i.e. visit(Visitor) .
  • Rename valid() to !valueless_by_exception() , following the strong recommendation from a L(E)WG poll.
  • Remove tuple_find : it was not relevant for using variant as that already provides index- and type-based accesses; it was a considerable fraction of the proposed wording; it warrants a dedicated design paper, should someone wish to have it.
  • Provide real wording. Fixes:
    • emplaced_ . becomes in_place_ .
    • provide intended basic constexpr support: construction, accessors, destruction,
    • implement all requests from the LWG review at Kona.

Differences to revision 6 (P0088R1)

  • Bring back tuple_not_found that got removed by mistake. Call it variant_npos .
  • Implement wording comments from Jacksonville LWG.
  • Rename tuple_size to variant_size , tuple_element to variant_alternative to clarify that this is not tuple-like. This avoids a clash with structured binding. The committee seems to have changed its common mind regarding these templates, reverting LEWG's decision from the first revision.
  • Do not implicitly force the variant valueless_by_exception if an exception is thrown during emplace / construction; merely state that it might become valueless_by_exception .
  • Reflect LEWG's updated recommended shipping vehicle (was TS, now C++17).

LEWG opted against introducing an explicit additional variant state, representing its invalid (and possibly empty, default constructed) state. This is meant to simplify the variant use: as getting a variant into the invalid state is sufficiently difficult, it was felt that there is no need to regularly check for a variant becoming invalid. This prevents all get<int9gt;(v) calls from being protected by if (v.valid()) .

Visibility of the Invalid State

Accessing an invalid variant's value is undefined behavior, whatever alternative is accessed.

The variant 's invalid state needs to be visible: accessing its contents or visiting it will violate preconditions; users must be able to verify that a variant is not in this state.

When in the invalid state, index() returns variant_npos ; variant provides valid() as a usability feature.

This usually does not need to be checked given how rare the invalid case is. It (generally) keeps a variant with N alternatives as an N-state type.

Empty state and default construction

Default construction of a variant should be allowed, to increase usability for instance in containers. LEWG opted against a variant default-initialized into its invalid state, to make invalid variant s really rare.

Instead, the variant can be initialized with the first alternative (similar to the behavior of initialization of a union ) only if that is default constructible. For cases where this behavior should be explicit, and for cases where no such default constructible alternative exists, there is a separate type monostate that can be used as first alternative, to explicitly enable default construction.

No header called variant exists; testing for this header's existence is thus sufficient.

The insertions and deletions in this section describe the changes to the C++ Working Paper. Grayish background indicates proposed wording. Blue markup indicates changes to the previous revision, with insertions and deletions signaled in an obvious way.

Insert a new element in Table 1, C++ library headers of [general.namespaces], named < experimental/ variant> .

Insert a new section:

A variant object holds and manages the lifetime of a value. If the variant holds a value, that value's type has to be one of the template argument types given to variant . These template arguments are called alternatives.

?.2 Header < experimental/ variant> synopsis [variant.synopsis]

Including this header also makes the following declarations from [tuple.helper] available:

?.3 variant of value types [variant.variant]

Any instance of variant at any given time either holds a value of one of its alternative types, or it holds no value. When an instance of variant holds a value of alternative type T , it means that a value of type T , referred to as the variant object's contained value, is allocated within the storage of the variant object. Implementations are not permitted to use additional storage, such as dynamic memory, to allocate the contained value. The contained value shall be allocated in a region of the variant storage suitably aligned for all types in Types. . It is implementation defined whether over-aligned types are supported.

All types in Types. shall be (possibly cv-qualified) object types, (possibly cv-qualified) void, or references. [Note: Implementations could decide to store references in a reference_ wrapper. — end note]

In the descriptions that follow, let i be in the range [0,sizeof. (Types)) , and T_i be the i th type in Types. .

constexpr variant() noexcept(see below);

Effects: Constructs a variant holding a value-initialized value of type T_0 . Postconditions: valueless_by_exception() is false and index() is 0 . Throws: Any exception thrown by the value initialization of T_0 . Remarks: This function shall be constexpr if and only if the value initialization of the alternative type T_0 would satisfy the requirements for a constexpr function. The expression inside noexcept is equivalent to is_nothrow_default_constructible_v<T_09gt; . This function shall not participate in overload resolution unless is_default_constructible_v<T_09gt; is true . [Note: see also class monostate . — end note]

variant(const variant& w);

Effects: If w holds a value, initializes the variant to hold the same alternative as w and direct-initializes the contained value from the value contained in w with get<j9gt;(w) , where j is w.index() . Otherwise, initializes the variant to not hold a value. Throws: Any exception thrown by direct-initializing any T_i for all i . Remarks: This function shall not participate in overload resolution unless is_copy_constructible_v<T_i9gt; is true for all i .

variant(variant&9amp; w) noexcept(see below);

Effects: If w holds a value, initializes the variant to hold the same alternative as w and direct-initializes the contained value with std::forward<T_j9gt;(get9lt;j9gt;(w)) get<j9gt;(std::move(w)) , where j is w.index() . Otherwise, initializes the variant to not hold a value. Throws: Any exception thrown by move-constructing any T_i for all i . Remarks: The expression inside noexcept is equivalent to the logical AND of is_nothrow_move_constructible_v<T_i9gt; for all i . This function shall not participate in overload resolution unless is_move_constructible_v<T_i9gt; is true for all i .

template <class T> constexpr variant(T&9amp; t) noexcept(see below) ;

Let T_j be a type that is determined as follows: build an imaginary function FUN(T_i) for each alternative type T_i . The overload FUN(T_j) selected by regular overload resolution for the expression FUN(std::forward<T9gt;(t)) defines the alternative T_j which is the type of the contained value after construction. Requires: Builds an imaginary function FUN(T_i) for each alternative type T_i . The expression FUN(std::forward<T9gt;(t)) must be valid according to regular overload resolution, otherwise the program is ill-formed. The selected function FUN(T_j) defines the alternative T_j that will be activated by the call to this constructor which is the type of the contained value after construction . [Note: is ill-formed, as both alternative types have an equally viable constructor for the argument. — end note] Effects: Initializes *this to hold the alternative type T_j as selected by the imaginary function overload resolution described above, and direct-initializes the contained value as if direct-non-list-initializing it with std::forward<T9gt;(t) . Postconditions: holds_alternative<T_j9gt;(*this) is true , with T_j selected by the imaginary function overload resolution described above . Throws: Any exception thrown by the initialization of the selected alternative T_j . Remarks: This function shall not participate in overload resolution unless is_same_v<decay_t9lt;T9gt;, variant> is false , unless is_constructible_v<T_j, T&9amp;9gt; is true , and unless the expression FUN(std::forward<T9gt;(t)) (with FUN being the above-mentioned set of imaginary functions) is valid according to regular overload resolution well formed . [Note: is ill-formed, as both alternative types have an equally viable constructor for the argument. — end note] The expression inside noexcept is equivalent to is_nothrow_constructible_v<T_j, T&9amp;9gt; . If T_j 's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

template <class T, class. Args> constexpr explicit variant(in_place_type_t<T9gt;, Args&9amp;. args);

Effects: Initializes the contained value as if constructing an object of type T with the arguments std::forward<Args9gt;(args). . Postcondition: holds_alternative<T9gt;(*this) is true . Throws: Any exception thrown by calling the selected constructor of T . Remarks: This function shall not participate in overload resolution unless there is exactly one occurrence of T in Types. and is_constructible_v<T, Args&9amp;. > is true . If T 's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

template <class T, class U, class. Args> constexpr explicit variant(in_place_type_t<T9gt;, initializer_list<U9gt; il, Args&9amp;. args);

Effects: Initializes the contained value as if constructing an object of type T with the arguments il, std::forward<Args9gt;(args). . Postcondition: holds_alternative<T9gt;(*this) is true . Throws: Any exception thrown by calling the selected constructor of T . Remarks: This function shall not participate in overload resolution unless there is exactly one occurrences of T in Types. and is_constructible_v<T, initializer_list<U9gt;9amp;, Args&9amp;. > is true . If T 's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

template <size_t I, class. Args> constexpr explicit variant(in_place_index_t<I9gt;, Args&9amp;. args);

Effects: Initializes the contained value as if constructing an object of type T_I with the arguments std::forward<Args9gt;(args). . Postcondition: index() is I . Throws: Any exception thrown by calling the selected constructor of T_I . Remarks: This function shall not participate in overload resolution unless I is less than sizeof. (Types) and is_constructible_v<T_I, Args&9amp;. > is true . If T_I 's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

template <size_t I, class U, class. Args> constexpr explicit variant(in_place_index_t<I9gt;, initializer_list<U9gt; il, Args&9amp;. args);

Effects: Initializes the contained value as if constructing an object of type T_I with the arguments il, std::forward<Args9gt;(args). . Postcondition: index() is I . Remarks: This function shall not participate in overload resolution unless I is less than sizeof. (Types) and is_constructible_v<T_I, initializer_list<U9gt;9amp;, Args&9amp;. > is true . If T_I 's selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor.

template <class Alloc>

variant(allocator_arg_t, const Alloc& a);

template <class Alloc>

variant(allocator_arg_t, const Alloc& a, const variant& v);

template <class Alloc>

variant(allocator_arg_t, const Alloc& a, variant&9amp; v);

template <class Alloc, class T>

variant(allocator_arg_t, const Alloc& a, T&9amp; t);

template <class Alloc, class T, class. Args>

variant(allocator_arg_t, const Alloc& a, in_place_type_t<T9gt;, Args&9amp;. args);

template <class Alloc, class T, class U, class. Args>

variant(allocator_arg_t, const Alloc& a, in_place_type_t<T9gt;, initializer_list<U9gt; il, Args&9amp;. args);

template <class Alloc, size_t I, class. Args>

variant(allocator_arg_t, const Alloc& a, in_place_index_t<I9gt;, Args&9amp;. args);

template <class Alloc, size_t I, class U, class. Args>

variant(allocator_arg_t, const Alloc& a, in_place_index_t<I9gt;, initializer_list<U9gt; il, Args&9amp;. args);

Requires: Alloc shall meet the requirements for an Allocator (17.6.3.5). Effects: Equivalent to the preceding constructors except that the contained value is constructed with uses-allocator construction (20.7.7.2).

Effects: If valueless_by_exception() is false , destroys the currently contained value. Remarks: If is_trivially_destructible_v<T_i9gt; == true for all T_i then this destructor shall be a trivial destructor.

variant& operator=(const variant& rhs);

  • If neither *this nor rhs holds a value, there is no effect. Otherwise
  • if *this holds a value but rhs does not, destroys the value contained in *this and sets *this to not hold a value. Otherwise,
  • if index() == rhs.index() , assigns the value contained in rhs to the value contained in *this . Otherwise,
  • copies the value contained in rhs to a temporary, then destroys any value contained in *this . Sets *this to hold the same alternative index as rhs and initializes the value contained in *this as if direct-non-list-initializing an object of type T_j with std::forward<T_j9gt;(TMP) , with TMP being the temporary and j being rhs.index() .

  • If an exception is thrown during the call to T_j 's copy assignment, the state of the contained value is as defined by the exception safety guarantee of T_j 's copy assignment; index() will be j .
  • If an exception is thrown during the call to T_j 's copy constructor (with j being rhs.index() ), *this will remain unchanged.
  • If an exception is thrown during the call to T_j 's move constructor, the variant will hold no value.
Returns: *this . Postconditions: index() == rhs.index() Remarks: This function shall not participate in overload resolution unless is_copy_constructible_v<T_i9gt; &9amp; is_move_constructible_v<T_i9gt; &9amp; is_copy_assignable_v<T_i9gt; is true for all i .
  • If an exception is thrown during the call to T_j 's copy assignment, the state of the contained value is as defined by the exception safety guarantee of T_j 's copy assignment; index() will be j .
  • If an exception is thrown during the call to T_j 's copy construction (with j being rhs.index() ), *this will remain unchanged.
  • If an exception is thrown during the call to T_j 's move construction, the variant will hold no value.

variant& operator=(variant&9amp; rhs) noexcept(see below);

  • If neither *this nor rhs holds a value, there is no effect. Otherwise
  • if *this holds a value but rhs does not, destroys the value contained in *this and sets *this to not hold a value. Otherwise,
  • if index() == rhs.index() , assigns std::forward<T_j9gt;(get9lt;j9gt;(rhs)) get<j9gt;(std::move(rhs)) to the value contained in *this , with j being index() . Otherwise,
  • destroys any value contained in *this . Sets *this to hold the same alternative index as rhs and initializes the value contained in *this as if direct-non-list-initializing an object of type T_j with std::forward<T_j9gt;(get9lt;j9gt;(rhs)) get<j9gt;(rhs) with j being rhs.index() .

If an exception is thrown during the call to T_j 's move construct or ion (with j being rhs.index() ), the variant will hold no value. If an exception is thrown during the call to T_j 's move assignment, the state of the contained value is as defined by the exception safety guarantee of T_j 's move assignment; index() will be j . Returns: *this . Remarks: This function shall not participate in overload resolution unless is_move_constructible_v<T_i9gt; &9amp; is_move_assignable_v<T_i9gt; is true for all i . The expression inside noexcept is equivalent to: is_nothrow_move_constructible_v<T_i9gt; &9amp; is_nothrow_move_assignable_v<T_i9gt; for all i . If an exception is thrown during the call to T_j 's move construct or ion (with j being rhs.index() ), the variant will hold no value. If an exception is thrown during the call to T_j 's move assignment, the state of the contained value is as defined by the exception safety guarantee of T_j 's move assignment; index() will be j .

template <class T> variant& operator=(T&9amp; t) noexcept(see below);

Let T_j be a type that is determined as follows: build an imaginary function FUN(T_i) for each alternative type T_i . The overload FUN(T_j) selected by regular overload resolution for the expression FUN(std::forward<T9gt;(t)) defines the alternative T_j which is the type of the contained value after assignment. Requires: Builds an imaginary function FUN(T_i) for each alternative type T_i . The expression FUN(std::forward<T9gt;(t)) must be valid according to regular overload resolution, otherwise the program is ill-formed. The selected function FUN(T_j) defines the alternative T_j that will be activated by the assignment which is the type of the contained value after assignment . [Note: is ill-formed, as both alternative types have an equally viable constructor for the argument. — end note] Effects: No effect if decay_t<T9gt;(t) is void . If *this holds a T_j , assigns std::forward<T9gt;(t) to the value contained in *this . Otherwise, destroys any value contained in *this , sets *this to hold the alternative type T_j as selected by the imaginary function overload resolution described above, and direct-initializes the contained value as if direct-non-list-initializing it with std::forward<T9gt;(t) .

If an exception is thrown during the assignment of std::forward<T9gt;(t) to the value contained in *this , the state of the contained value and t are as defined by the exception safety guarantee of the assignment expression; valueless_by_exception() will be false . If an exception is thrown during the initialization of the contained value, the variant object will might not hold a value. Postcondition: holds_alternative<T_j9gt;(*this) is true , with T_j selected by the imaginary function overload resolution described above. Returns: *this . Remarks: This function shall not participate in overload resolution unless is_same_v<decay_t9lt;T9gt;, variant> is false , unless is_assignable_v<T_j & , T&9amp;9gt; &9amp; is_constructible_v<T_j, T&9amp;9gt; is true , and unless the expression FUN(std::forward<T9gt;(t)) (with FUN being the above-mentioned set of imaginary functions) is valid according to regular overload resolution well formed . [Note: is ill-formed, as both alternative types have an equally viable constructor for the argument. — end note] The expression inside noexcept is equivalent to: is_nothrow_assignable_v<T_ i j& , T&9amp;9gt; &9amp; is_nothrow_constructible_v<T_ i j , T&9amp;9gt; for all i . If an exception is thrown during the assignment of std::forward<T9gt;(t) to the value contained in *this , the state of the contained value and t are as defined by the exception safety guarantee of the assignment expression; valueless_by_exception() will be false . If an exception is thrown during the initialization of the contained value, the variant object will might not hold a value.

template <class T, class. Args> void emplace(Args&9amp;. args);

Effects: Destroys the currently contained value if valueless_by_exception() is false . Then direct-initializes the contained value as if constructing a value of type T with the arguments std::forward<Args9gt;(args). . Equivalent to emplace<I9gt;(std::forward9lt;Args9gt;(args). ) where I is the zero-based index of T in Types. . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value. Postcondition: holds_alternative<T9gt;(*this) is true . Throws: Any exception thrown during the initialization of the contained value. Remarks: This function shall not participate in overload resolution unless is_constructible_v<T, Args&9amp;. > is true , and T occurs exactly once in Types. .

template <class T, class U, class. Args> void emplace(initializer_list<U9gt; il, Args&9amp;. args);

Effects: Destroys the currently contained value if valueless_by_exception() is false . Then direct-initializes the contained value as if constructing an object of type T with the arguments il, std::forward<Args9gt;(args). . Equivalent to emplace<I9gt;(il, std::forward<Args9gt;(args). ) where I is the zero-based index of T in Types. . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value. Postcondition: holds_alternative<T9gt;(*this) is true . Throws: Any exception thrown during the initialization of the contained value . Remarks: This function shall not participate in overload resolution unless is_constructible_v<T, initializer_list<U9gt;9amp;, Args&9amp;. > is true , and T occurs exactly once in Types. .

template <size_t I, class. Args> void emplace(Args&9amp;. args);

Requires: I < sizeof. (Types) Effects: Destroys the currently contained value if valueless_by_exception() is false . Then direct-initializes the contained value as if constructing a value of type T_I with the arguments std::forward<Args9gt;(args). . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value. Postcondition: index() is I . Throws: Any exception thrown during the initialization of the contained value. Remarks: This function shall not participate in overload resolution unless is_constructible_v<T_I, Args&9amp;. > is true . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value.

template <size_t I, class U, class. Args> void emplace(initializer_list<U9gt; il, Args&9amp;. args);

Requires: I < sizeof. (Types) Effects: Destroys the currently contained value if valueless_by_exception() is false . Then direct-initializes the contained value as if constructing an object of type T_I with the arguments il, std::forward<Args9gt;(args). . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value. Postcondition: index() is I . Throws: Any exception thrown during the initialization of the contained value. Remarks: This function shall not participate in overload resolution unless is_constructible_v<T_I, initializer_list<U9gt;9amp;, Args&9amp;. > is true . If an exception is thrown during the initialization of the contained value, the variant will might not hold a value.

constexpr bool valueless_by_exception() const noexcept;

Effects: Returns false if and only if the variant holds a value whether the variant holds a value (returns false ) . [Note: A variant will might not hold a value if an exception is thrown during a type-changing assignment or emplacement. The latter means that even a variant<float,int9gt; can become valueless_by_exception() , for instance by — end note]

constexpr size_t index() const noexcept;

Effects: If valueless_by_exception() is true , returns variant_npos . Otherwise, returns the zero-based index of the currently active alternative of the contained value .

void swap(variant& rhs) noexcept(see below);

  • if valueless_by_exception() &9amp; rhs.valueless_by_exception() no effect. Otherwise,
  • if index() == rhs.index() , calls swap(get<i9gt;(*this), get<i9gt;(rhs)) where i is index() . Otherwise,
  • exchanges values of rhs and *this .

If an exception is thrown during the call to function swap(get<i9gt;(*this), get<i9gt;(rhs)) , the states of the contained values of * this and of rhs are determined by the exception safety guarantee of swap for lvalues of T_i with i being index() . If an exception is thrown during the exchange of the values of *this and rhs , the states of the values of * this and of rhs are determined by the exception safety guarantee of variant 's move constructor and move assignment operator. Throws: Any exception thrown by swap(get<i9gt;(*this), get<i9gt;(rhs)) with i being index() or variant 's move constructor and move assignment operator. Remarks: This function shall not participate in overload resolution unless is_swappable_v<T_i9gt; is true for all i all alternative types satisfy the Swappable requirements (17.6.3.2) with the corresponding alternative in rhs . If an exception is thrown during the call to function swap(get<i9gt;(*this), get<i9gt;(rhs)) , the states of the contained values of * this and of rhs are determined by the exception safety guarantee of swap for lvalues of T_i with i being index() . If an exception is thrown during the exchange of the values of *this and rhs , the states of the values of * this and of rhs are determined by the exception safety guarantee of variant 's move constructor and move assignment operator.

?.4 In-place construction [variant.in_place]

template <class T> constexpr in_place_type_t<T9gt; in_place_type<>;

template <size_t I> constexpr in_place_index_t<I9gt; in_place_index<>;

Template specializations of in_place_type_t are empty structure types used as unique types to disambiguate constructor overloading. They signal (through the template parameter) the alternative to be constructed. Specifically, variant has a constructor with in_place_type_t<T9gt; as the first argument followed by an argument pack; this indicates that T should be constructed in-place (as if by a call to a placement new expression) with the forwarded argument pack as parameters. If a variant 's Types. has multiple occurrences of T , in_place_index_t must be used.

Template specializations of in_place_index_t are empty structure types used as unique types to disambiguate constructor overloading, and signaling (through the template parameter) the alternative to be constructed. Specifically, variant has a constructor with in_place_index_t<I9gt; as the first argument followed by an argument pack; this indicates that T_I should be constructed in-place (as if by a call to a placement new expression) with the forwarded argument pack as parameters.

template <class T, class. Types> constexpr bool holds_alternative(const variant<Types. >9amp; v) noexcept;

Requires: The type T occurs exactly once in Types. . Otherwise, the program is ill-formed. Returns: true if index() is equal to the zero-based index of T in Types. .

template <size_t I, class. Types>

constexpr tuple_element_t variant_alternative_t <I, variant<Types. >9gt;9amp; get(variant<Types. >9amp; v);

template <size_t I, class. Types>

constexpr tuple_element_t variant_alternative_t <I, variant<Types. >9gt;9amp;9amp; get(variant<Types. >9amp;9amp; v); // Note A

template <size_t I, class. Types>

constexpr tuple_element_t variant_alternative_t <I, variant<Types. >9gt; const& get(const variant<Types. >9amp; v); // Note B

template <size_t I, class. Types>

constexpr tuple_element_t variant_alternative_t <I, variant<Types. >9gt; const&9amp; get(const variant<Types. >9amp;9amp; v); // Notes A and B

Requires: I < sizeof. (Types) , and T_I is not (possibly cv-qualified) void . Otherwise the program is ill-formed. Effects: If v.index() is I , returns a reference to the object stored in the variant. Otherwise, throws an exception of type bad_variant_access . [Note A: if T_I is some reference type X& , the return type is X& , not X&9amp; . However, if the element type is a non-reference type T , the return type is T&9amp; . — end note] [Note B: Constness is shallow. If T_I is some reference type X& , the return type is X& , not const X& . However, if the element type is non-reference type T , the return type is const T& . This is consistent with how constness is defined to work for member variables of reference type. — end note]

template <class T, class. Types> constexpr T& get(variant<Types. >9amp; v);

template <class T, class. Types> constexpr T&9amp; get(variant<Types. >9amp;9amp; v);

template <class T, class. Types> constexpr const T& get(const variant<Types. >9amp; v);

template <class T, class. Types> constexpr const T&9amp; get(const variant<Types. >9amp;9amp; v);

Requires: The type T occurs exactly once in Types. , and T is not (possibly cv-qualified) void . Otherwise, the program is ill-formed. Effects: If v holds a value of type T , returns a reference to that value. Otherwise, throws an exception of type bad_variant_access .

template <size_t I, class. Types>

constexpr add_pointer_t< tuple_element_t variant_alternative_t <I, variant<Types. >9gt;9gt; get_if(variant<Types. >* v) noexcept;

template <size_t I, class. Types>

constexpr add_pointer_t<const tuple_element_t variant_alternative_t <I, variant<Types. >9gt;9gt; get_if(const variant<Types. >* v) noexcept;

Requires: I < sizeof. (Types) and T_I is not (possibly cv-qualified) void ; otherwise the program is ill-formed. Returns: A pointer to the value stored in the variant, if v != nullptr and v->index() == I . Otherwise, returns nullptr .

template <class T, class. Types>

constexpr add_pointer_t<T9gt; get_if(variant<Types. >* v) noexcept;

template <class T, class. Types>

constexpr add_pointer_t<const T> get_if(const variant<Types. >* v) noexcept;

Requires: The type T occurs exactly once in Types. , and T is not (possibly cv-qualified) void . Otherwise, the program is ill-formed. Effects: Equivalent to return get_if<i9gt;(v) with i being the zero-based index of T in Types. .

?.6 Relational operators [variant.relops]

template <class. Types> constexpr bool operator==(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Requires: get<i9gt;(v) == get<i9gt;(w) is a valid expression returning a type that is convertible to bool , for all i . Effects: Equivalent to return (v.valueless_by_exception() &9amp; w.valueless_by_exception()) || (v.index() == w.index() &9amp; get<i9gt;(v) == get<i9gt;(w)) with i being v.index() , otherwise false .

template <class. Types> constexpr bool operator!=(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Returns Effects : Equivalent to return !(v == w) .

template <class. Types> constexpr bool operator<(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Requires: get<i9gt;(v) < get<i9gt;(w) is a valid expression returning a type that is convertible to bool , for all i . Effects: Equivalent to return (v.index() < w.index()) || (v.index() == w.index() &9amp; !v.valueless_by_exception() &9amp; get<i9gt;(v) < get<i9gt;(w)) with i being v.index() , otherwise false .

template <class. Types> constexpr bool operator>(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Returns Effects : Equivalent to return w < v .

template <class. Types> constexpr bool operator<=(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Returns Effects : Equivalent to return !(v > w) .

template <class. Types> constexpr bool operator>=(const variant<Types. >9amp; v, const variant<Types. >9amp; w);

Returns Effects : Equivalent to return !(v < w) .

template <class Visitor, class. Variants>

constexpr see below visit(Visitor&9amp; vis, Variants&9amp;. vars);

Requires: The expression in the Effects element must be a valid expression of the same type and value category, for all combinations of alternative types of all variants. Otherwise, the program is ill-formed. Effects: Let is. be vars.index(). . Returns INVOKE(forward<Visitor9gt;(vis), get<is9gt;(forward9lt;Variants9gt;(vars)). ); . Remarks: The return type is the common_type common type of all possible INVOKE expressions of the Effects element. Throws: bad_variant_access if any variant in vars is valueless_by_exception() . Complexity: For sizeof. (Variants) <= 1 , the invocation of the callable object must be implemented in constant time, i.e. it must not depend on sizeof. (Types) . For sizeof. (Variants) > 1 , the invocation of the callable object has no complexity requirements.

?.8 Class monostate [variant.monostate]

The class monostate can serve as a first alternative type for a variant to make the variant type default constructible.

?.9 monostate relational operators [variant.monostate.relops]

constexpr bool operator<(monostate, monostate) noexcept

constexpr bool operator>(monostate, monostate) noexcept

constexpr bool operator<=(monostate, monostate) noexcept

constexpr bool operator>=(monostate, monostate) noexcept

constexpr bool operator==(monostate, monostate) noexcept

constexpr bool operator!=(monostate, monostate) noexcept

[Note: monostate object have only a single state; they thus always compare equal.— end note]

?.10 Specialized algorithms [variant.specalg]

template <class. Types> void swap(variant<Types. >9amp; v, variant<Types. >9amp; w) noexcept(see below);

Effects: Calls Equivalent to v.swap(w) . Remarks: The expression inside noexcept is equivalent to noexcept(v.swap(w)) .

?.11 Class bad_variant_access [variant.bad_variant_access]

Objects of type bad_variant_access are thrown to report invalid accesses to the value of a variant object.

Effects: Constructs a bad_variant_access object. Postconditions: what() returns an implementation-defined NTBS.

const char* what() const noexcept override;

Returns: an implementation-defined NTBS.

?.12 tuple interface to class template variant variant helper classes [variant.tuple] [variant.helper]

template <class T> struct variant_size;

Remarks: All specializations of variant_size<T9gt; shall meet the UnaryTypeTrait requirements (20.10.1) with a BaseCharacteristic of integral_constant<size_t, N> for some N .

template <class T> class variant_size<const T>;

template <class T> class variant_size<volatile T>;

template <class T> class variant_size<const volatile T>;

Let VS denote variant_size<T9gt; of the cv-unqualified type T . Then each of the three templates shall meet the UnaryTypeTrait requirements (20.10.1) with a BaseCharacteristic of integral_constant<size_t, VS::value> .

template <class. Types>

struct tuple_size variant_size <variant9lt;Types. >9gt;

: integral_constant<size_t, sizeof. (Types)> < >;

template <size_t I, class T> class variant_alternative<I, const T>;

template <size_t I, class T> class variant_alternative<I, volatile T>;

template <size_t I, class T> class variant_alternative<I, const volatile T>;

Let VA denote variant_alternative<I, T> of the cv-unqualified type T . Then each of the three templates shall meet the TransformationTrait requirements (20.10.1) with a member typedef type that names the following type:

  • for the first specialization, add_const_t<VA::type9gt; ,
  • for the second specialization, add_volatile_t<VA::type9gt; , and
  • for the third specialization, add_cv_t<VA::type9gt; .

tuple_element variant_alternative <I, variant<Types. >9gt;::type

Requires: I < sizeof. (Types) Value: The type T_I .

template <class. Types> struct hash< experimental:: variant<Types. >9gt;;

The template specialization hash<T9gt; shall meet the requirements of class template hash (C++14 §20.9.13) for all T in Types. . The template specialization hash< experimental:: variant<Types. >9gt; shall meet the requirements of class template hash .

template <9gt; struct hash< experimental:: monostate>;

The template specialization hash< experimental:: monostate> shall meet the requirements of class template hash .

?.14 Allocator-related traits [variant.traits]

template <class. Types, class Alloc>

struct uses_allocator< experimental:: variant<Types. >, Alloc> : true_type < >;

Requires: Alloc shall be an Allocator (17.6.3.5). [Note: Specialization of this trait informs other library components that variant can be constructed with an allocator, even though it does not have a nested allocator_type . — end note]

A variant has proven to be a useful tool. This paper proposes the necessary ingredients.

Thank you, Nevin ":-)" Liber, for bringing sanity to this proposal. Agustín K-ballo Bergé and Antony Polukhin provided very valuable feedback, criticism and suggestions. Thanks also to Vincenzo Innocente and Philippe Canal for their comments.

1. Working Draft, Technical Specification on C++ Extensions for Library Fundamentals. N4335

2. Improving pair and tuple, revision 2. N4064