Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant_alternative,std::variant_alternative_t

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      Defined in header<variant>
      template<std::size_t I,class T>
      struct variant_alternative;/* undefined */
      (1)(since C++17)
      template<std::size_t I,class...Types>
      struct variant_alternative<I, variant<Types...>>;
      (2)(since C++17)
      template<std::size_t I,class T>class variant_alternative<I,const T>;
      (3)(since C++17)
      template<std::size_t I,class T>

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

      class variant_alternative<I,constvolatile T>;
      (3)(since C++17)
      (deprecated in C++20)

      Provides compile-time indexed access to the types of the alternatives of the possibly cv-qualified variant, combining cv-qualifications of the variant (if any) with the cv-qualifications of the alternative.

      Formally,

      2) meets theTransformationTrait requirements with a member typedeftype equal to the type of the alternative with indexI
      3) meets theTransformationTrait requirements with a member typedeftype that names, respectively,std::add_const_t<std::variant_alternative_t<I,T>>,std::add_volatile_t<std::variant_alternative_t<I,T>>, andstd::add_cv_t<std::variant_alternative_t<I,T>>

      Contents

      [edit]Member types

      Member type Definition
      type the type ofIth alternative of the variant, whereI must be in[0, sizeof...(Types)), otherwise the program is ill-formed.

      [edit]Helper template alias

      template<size_t I,class T>
      using variant_alternative_t=typename variant_alternative<I, T>::type;
      (since C++17)

      [edit]Example

      Run this code
      #include <variant>#include <iostream> using my_variant=std::variant<int,float>;static_assert(std::is_same_v<int,   std::variant_alternative_t<0, my_variant>>);static_assert(std::is_same_v<float, std::variant_alternative_t<1, my_variant>>);// cv-qualification on the variant type propagates to the extracted alternative type.static_assert(std::is_same_v<constint, std::variant_alternative_t<0,const my_variant>>); int main(){std::cout<<"All static assertions passed.\n";}

      Output:

      All static assertions passed.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2974C++17out-of-bounds index resulted in undefined behaviormade ill-formed

      [edit]See also

      obtains the size of thevariant's list of alternatives at compile time
      (class template)(variable template)[edit]
      obtains the type of the specified element
      (class template specialization)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/variant_alternative&oldid=123920"

      [8]ページ先頭

      ©2009-2025 Movatter.jp