Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       
      Defined in header<variant>
      template<class...Types>
      class variant;
      (since C++17)

      The class templatestd::variant represents a type-safeunion.

      An instance ofvariant at any given time either holds a value of one of its alternative types, or in the case of error - no value (this state is hard to achieve, seevalueless_by_exception).

      As with unions, if a variant holds a value of some object typeT, theT object isnested within thevariant object.

      A variant is not permitted to hold references, arrays, or the typevoid.

      A variant is permitted to hold the same type more than once, and to hold differently cv-qualified versions of the same type.

      Consistent with the behavior of unions duringaggregate initialization, a default-constructed variant holds a value of its first alternative, unless that alternative is not default-constructible (in which case the variant is not default-constructible either). The helper classstd::monostate can be used to make such variants default-constructible.

      A program that instantiates the definition ofstd::variant with no template arguments is ill-formed.std::variant<std::monostate> can be used instead.

      If a program declares anexplicit orpartial specialization ofstd::variant, the program is ill-formed, no diagnostic required.

      Contents

      Template parameters

      Types - the types that may be stored in this variant. All types must meet theDestructible requirements (in particular, array types and non-object types are not allowed).

      Member functions

      constructs thevariant object
      (public member function)[edit]
      destroys thevariant, along with its contained value
      (public member function)[edit]
      assigns avariant
      (public member function)[edit]
      Observers
      returns the zero-based index of the alternative held by thevariant
      (public member function)[edit]
      checks if thevariant is in the invalid state
      (public member function)[edit]
      Modifiers
      constructs a value in thevariant, in place
      (public member function)[edit]
      swaps with anothervariant
      (public member function)[edit]
      Visitation
      (C++26)
      calls the provided functor with the argument held by thevariant
      (public member function)[edit]

      Non-member functions

      (C++17)
      calls the provided functor with the arguments held by one or morevariants
      (function template)[edit]
      checks if avariant currently holds a given type
      (function template)[edit]
      reads the value of the variant given the index or the type (if the type is unique), throws on error
      (function template)[edit]
      (C++17)
      obtains a pointer to the value of a pointed-tovariant given the index or the type (if unique), returns null on error
      (function template)[edit]
      (C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
      comparesvariant objects as their contained values
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]

      Helper classes

      (C++17)
      placeholder type for use as the first alternative in avariant of non-default-constructible types
      (class)[edit]
      exception thrown on invalid accesses to the value of avariant
      (class)[edit]
      obtains the size of thevariant's list of alternatives at compile time
      (class template)(variable template)[edit]
      obtains the type of the alternative specified by its index, at compile time
      (class template)(alias template)[edit]
      hash support forstd::variant
      (class template specialization)[edit]

      Helper objects

      index of thevariant in the invalid state
      (constant)[edit]

      Notes

      Feature-test macroValueStdFeature
      __cpp_lib_variant201606L(C++17)std::variant: a type-safe union
      202102L(C++23)
      (DR17)
      std::visit for classes derived fromstd::variant
      202106L(C++23)
      (DR20)
      Fullyconstexprstd::variant
      202306L(C++26)Membervisit

      Example

      Run this code
      #include <cassert>#include <iostream>#include <string>#include <variant> int main(){    std::variant<int,float> v, w;    v=42;// v contains intint i= std::get<int>(v);assert(42== i);// succeeds    w= std::get<int>(v);    w= std::get<0>(v);// same effect as the previous line    w= v;// same effect as the previous line //  std::get<double>(v); // error: no double in [int, float]//  std::get<3>(v);      // error: valid index values are 0 and 1 try{        std::get<float>(w);// w contains int, not float: will throw}catch(conststd::bad_variant_access& ex){std::cout<< ex.what()<<'\n';} usingnamespace std::literals;     std::variant<std::string> x("abc");// converting constructors work when unambiguous    x="def";// converting assignment also works when unambiguous     std::variant<std::string,voidconst*> y("abc");// casts to void const* when passed a char const*assert(std::holds_alternative<voidconst*>(y));// succeeds    y="xyz"s;assert(std::holds_alternative<std::string>(y));// succeeds}

      Possible output:

      std::get: wrong index for variant

      Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2901C++17specialization ofstd::uses_allocator provided,
      butvariant cannot properly support allocators
      specialization removed
      LWG 3990C++17a program could declare an explicit or
      partial specialization ofstd::variant
      the program is ill-formed in this
      case (no diagnostic required)
      LWG 4141C++17the requirement for storage
      allocation was confusing
      the contained object must be
      nested within thevariant object

      See also

      in-place construction tag
      (tag)[edit]
      (C++17)
      a wrapper that may or may not hold an object
      (class template)[edit]
      (C++17)
      objects that hold instances of anyCopyConstructible type
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant&oldid=180938"

      [8]ページ先頭

      ©2009-2025 Movatter.jp