Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Standard library header <variant> (C++17)

      From cppreference.com
      <cpp‎ |header
       
       
      Standard library headers
       

      This header is part of thegeneral utility library.

      Contents

      Includes

      (C++20)
      Three-way comparison operator support[edit]

      Classes

      (C++17)
      a type-safe discriminated union
      (class template)[edit]
      (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]

      Constants

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

      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]

      [edit]Synopsis

      // mostly freestanding#include <compare> namespace std{// class template varianttemplate<class...Types>class variant; // variant helper classestemplate<class T>struct variant_size;// not definedtemplate<class T>struct variant_size<const T>;template<class T>constexpr size_t variant_size_v= variant_size<T>::value; template<class...Types>struct variant_size<variant<Types...>>; template<size_t I,class T>struct variant_alternative;// not definedtemplate<size_t I,class T>struct variant_alternative<I,const T>;template<size_t I,class T>using variant_alternative_t=typename variant_alternative<I, T>::type; template<size_t I,class...Types>struct variant_alternative<I, variant<Types...>>; inlineconstexpr size_t variant_npos=-1; // value accesstemplate<class T,class...Types>constexprbool holds_alternative(const variant<Types...>&)noexcept; template<size_t I,class...Types>constexpr variant_alternative_t<I, variant<Types...>>& get(    variant<Types...>&);// freestanding-deletedtemplate<size_t I,class...Types>constexpr variant_alternative_t<I, variant<Types...>>&& get(    variant<Types...>&&);// freestanding-deletedtemplate<size_t I,class...Types>constexprconst variant_alternative_t<I, variant<Types...>>& get(const variant<Types...>&);// freestanding-deletedtemplate<size_t I,class...Types>constexprconst variant_alternative_t<I, variant<Types...>>&& get(const variant<Types...>&&);// freestanding-deleted template<class T,class...Types>constexpr T& get(variant<Types...>&);// freestanding-deletedtemplate<class T,class...Types>constexpr T&& get(variant<Types...>&&);// freestanding-deletedtemplate<class T,class...Types>constexprconst T& get(const variant<Types...>&);// freestanding-deletedtemplate<class T,class...Types>constexprconst T&& get(const variant<Types...>&&);// freestanding-deleted template<size_t I,class...Types>constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>> get_if(    variant<Types...>*)noexcept;template<size_t I,class...Types>constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>> get_if(const variant<Types...>*)noexcept; template<class T,class...Types>constexpr add_pointer_t<T> get_if(variant<Types...>*)noexcept;template<class T,class...Types>constexpr add_pointer_t<const T> get_if(const variant<Types...>*)noexcept; // relational operatorstemplate<class...Types>constexprbool operator==(const variant<Types...>&,const variant<Types...>&);template<class...Types>constexprbool operator!=(const variant<Types...>&,const variant<Types...>&);template<class...Types>constexprbool operator<(const variant<Types...>&,const variant<Types...>&);template<class...Types>constexprbool operator>(const variant<Types...>&,const variant<Types...>&);template<class...Types>constexprbool operator<=(const variant<Types...>&,const variant<Types...>&);template<class...Types>constexprbool operator>=(const variant<Types...>&,const variant<Types...>&);template<class...Types>    requires(three_way_comparable<Types>&& ...)constexpr common_comparison_category_t<compare_three_way_result_t<Types>...>  operator<=>(const variant<Types...>&,const variant<Types...>&); // visitationtemplate<class Visitor,class...Variants>constexpr/* see description */ visit(Visitor&&, Variants&&...);template<class R,class Visitor,class...Variants>constexpr R visit(Visitor&&, Variants&&...); // class monostatestruct monostate; // monostate relational operatorsconstexprbool operator==(monostate, monostate)noexcept;constexpr strong_ordering operator<=>(monostate, monostate)noexcept; // specialized algorithmstemplate<class...Types>constexprvoid swap(variant<Types...>&,                      variant<Types...>&)noexcept(/* see description */); // class bad_variant_accessclass bad_variant_access; // hash supporttemplate<class T>struct hash;template<class...Types>struct hash<variant<Types...>>;template<>struct hash<monostate>;} // deprecatednamespace std{template<class T>struct variant_size<volatile T>;template<class T>struct variant_size<constvolatile T>; template<size_t I,class T>struct variant_alternative<I,volatile T>;template<size_t I,class T>struct variant_alternative<I,constvolatile T>;}

      [edit]Class templatestd::variant

      namespace std{template<class...Types>class variant{public:// constructorsconstexpr variant()noexcept(/* see description */);constexpr variant(const variant&);constexpr variant(variant&&)noexcept(/* see description */); template<class T>constexpr variant(T&&)noexcept(/* see description */); template<class T,class...Args>constexprexplicit variant(in_place_type_t<T>, Args&&...);template<class T,class U,class...Args>constexprexplicit variant(in_place_type_t<T>, initializer_list<U>, Args&&...); template<size_t I,class...Args>constexprexplicit variant(in_place_index_t<I>, Args&&...);template<size_t I,class U,class...Args>constexprexplicit variant(in_place_index_t<I>, initializer_list<U>, Args&&...); // destructorconstexpr ~variant(); // assignmentconstexpr variant& operator=(const variant&);constexpr variant& operator=(variant&&)noexcept(/* see description */); template<class T>constexpr variant& operator=(T&&)noexcept(/* see description */); // modifierstemplate<class T,class...Args>constexpr T& emplace(Args&&...);template<class T,class U,class...Args>constexpr T& emplace(initializer_list<U>, Args&&...);template<size_t I,class...Args>constexpr variant_alternative_t<I, variant<Types...>>& emplace(Args&&...);template<size_t I,class U,class...Args>constexpr variant_alternative_t<I, variant<Types...>>& emplace(initializer_list<U>,                                                                   Args&&...); // value statusconstexprbool valueless_by_exception()constnoexcept;constexpr size_t index()constnoexcept; // swapconstexprvoid swap(variant&)noexcept(/* see description */); // visitationtemplate<class Self,class Visitor>constexpr decltype(auto) visit(this Self&&, Visitor&&);template<class R,class Self,class Visitor>constexpr R visit(this Self&&, Visitor&&);};}

      [edit]Classstd::monostate

      namespace std{struct monostate{}; constexprbool  operator==(monostate, monostate)noexcept{returntrue;} constexpr strong_ordering  operator<=>(monostate, monostate)noexcept{return strong_ordering::equal;}}

      [edit]Classstd::bad_variant_access

      namespace std{class bad_variant_access:public exception{public:// see description for the specification of the special member functionsconstexprconstchar* what()constnoexcept override;};}
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/header/variant&oldid=178525"

      [8]ページ先頭

      ©2009-2025 Movatter.jp