命名空间
变体
操作

标准库头文件 <variant> (C++17)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 通用实用工具 库的一部分。

内容

包含

(C++20)
三路比较运算符 支持[编辑]

(C++17)
类型安全的区分联合
(类模板) [编辑]
(C++17)
占位符类型,用作非默认可构造类型 variant 中的第一个备选方案
(类) [编辑]
在对variant的值进行无效访问时抛出的异常
(类) [编辑]
在编译时获取variant的备选方案列表的大小
(类模板) (变量模板)[编辑]
在编译时获取由其索引指定的备选方案的类型
(类模板) (别名模板)[编辑]
std::variant的哈希支持
(类模板特化) [编辑]

常量

variant处于无效状态时的索引
(常量) [编辑]

函数

(C++17)
使用一个或多个variant持有的参数调用提供的函数对象
(函数模板) [编辑]
检查variant当前是否持有给定的类型
(函数模板) [编辑]
根据索引或类型(如果类型是唯一的)读取变体的值,在错误时抛出异常
(函数模板) [编辑]
(C++17)
根据索引或类型(如果唯一)获取指向指向的variant的值的指针,在错误时返回 null
(函数模板) [编辑]
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
variant对象与其包含的值进行比较
(函数模板) [编辑]
特化了std::swap算法
(函数模板) [编辑]

[编辑] 概要

#include <compare>
 
namespace std {
  // class template variant
  template<class... Types>
    class variant;
 
  // variant helper classes
  template<class T> struct variant_size;                        // not defined
  template<class T> struct variant_size<const T>;
  template<class T>
    inline 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 defined
  template<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...>>;
 
  inline constexpr size_t variant_npos = -1;
 
  // value access
  template<class T, class... Types>
    constexpr bool holds_alternative(const variant<Types...>&) noexcept;
 
  template<size_t I, class... Types>
    constexpr variant_alternative_t<I, variant<Types...>>& get(variant<Types...>&);
  template<size_t I, class... Types>
    constexpr variant_alternative_t<I, variant<Types...>>&& get(variant<Types...>&&);
  template<size_t I, class... Types>
    constexpr const variant_alternative_t<I, variant<Types...>>&
      get(const variant<Types...>&);
  template<size_t I, class... Types>
    constexpr const variant_alternative_t<I, variant<Types...>>&&
      get(const variant<Types...>&&);
 
  template<class T, class... Types>
    constexpr T& get(variant<Types...>&);
  template<class T, class... Types>
    constexpr T&& get(variant<Types...>&&);
  template<class T, class... Types>
    constexpr const T& get(const variant<Types...>&);
  template<class T, class... Types>
    constexpr const T&& get(const variant<Types...>&&);
 
  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 operators
  template<class... Types>
    constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
  template<class... Types>
    constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
  template<class... Types>
    constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
  template<class... Types>
    constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
  template<class... Types>
    constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
  template<class... Types>
    constexpr bool 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...>&);
 
  // visitation
  template<class Visitor, class... Variants>
    constexpr /* see description */ visit(Visitor&&, Variants&&...);
  template<class R, class Visitor, class... Variants>
    constexpr R visit(Visitor&&, Variants&&...);
 
  // class monostate
  struct monostate;
 
  // monostate relational operators
  constexpr bool operator==(monostate, monostate) noexcept;
  constexpr strong_ordering operator<=>(monostate, monostate) noexcept;
 
  // specialized algorithms
  template<class... Types>
    constexpr void swap(variant<Types...>&,
                        variant<Types...>&) noexcept(/* see description */);
 
  // class bad_variant_access
  class bad_variant_access;
 
  // hash support
  template<class T> struct hash;
  template<class... Types> struct hash<variant<Types...>>;
  template<> struct hash<monostate>;
}
 
// deprecated
namespace std {
  template<class T> struct variant_size<volatile T>;
  template<class T> struct variant_size<const volatile T>;
 
  template<size_t I, class T> struct variant_alternative<I, volatile T>;
  template<size_t I, class T> struct variant_alternative<I, const volatile T>;
}

[编辑] 类模板 std::variant

namespace std {
  template<class... Types>
  class variant {
  public:
    // constructors
    constexpr 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>
      constexpr explicit variant(in_place_type_t<T>, Args&&...);
    template<class T, class U, class... Args>
      constexpr explicit variant(in_place_type_t<T>, initializer_list<U>, Args&&...);
 
    template<size_t I, class... Args>
      constexpr explicit variant(in_place_index_t<I>, Args&&...);
    template<size_t I, class U, class... Args>
      constexpr explicit variant(in_place_index_t<I>, initializer_list<U>, Args&&...);
 
    // destructor
    constexpr ~variant();
 
    // assignment
    constexpr variant& operator=(const variant&);
    constexpr variant& operator=(variant&&) noexcept(/* see description */);
 
    template<class T> constexpr variant& operator=(T&&) noexcept(/* see description */);
 
    // modifiers
    template<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 status
    constexpr bool valueless_by_exception() const noexcept;
    constexpr size_t index() const noexcept;
 
    // swap
    constexpr void swap(variant&) noexcept(/* see description */);
  };
}

[编辑] std::monostate

struct monostate{};

[编辑] std::bad_variant_access

class bad_variant_access : public exception {
public:
  // see [exception] for the specification of the special member functions
  const char* what() const noexcept override;
};