any class

对象 any 存储满足构造函数要求的类型的实例,或没有值。 无论是具有存储实例还是没有值,都称为 any 对象的状态

存储实例称为包含的值。 如果两个 any 对象都没有值,或者两个对象具有包含的值且这些值相同,则两个对象具有相同的状态。

语法

class any;

成员

构造函数

名称 描述
any 构造 any 类型的对象。

函数

名称 描述
emplace 设置 any 值。
has_value 如果 any 具有值,则返回 true
reset 重置 any
swap 交换两个 any 对象。
type 返回 any 类型。

运算符

名称 描述
operator= any 替换为另一个 any 的副本。

any

构造 any 类型的对象。 还包括一个析构函数。

constexpr any() noexcept;
any(const any& other);
any(any&& other) noexcept;
template <class T>
    any(T&& value);
template <class T, class... Args>
    explicit any(in_place_type_t<T>, Args&&...);
template <class T, class U, class... Args>
    explicit any(in_place_type_t<T>, initializer_list<U>, Args&&...);

~any();

emplace

设置 any 值。

template <class T, class... Args>
    decay_t<T>& emplace(Args&& ...);
template <class T, class U, class... Args>
    decay_t<T>& emplace(initializer_list<U>, Args&&...);

has_value

如果 any 对象具有值,则返回 true

bool has_value() const noexcept;

operator=

any 内容替换为另一个 any 的副本。

any& operator=(const any& right);
any& operator=(any&& right) noexcept;
template <class T>
    any& operator=(T&& right);

参数

right
要复制到 any 中的 any

reset

重置 any

void reset() noexcept;

swap

交换两个 any 对象。

void swap(any& rhs) noexcept;

type

返回 any 类型。

const type_info& type() const noexcept;

要求

标头<any>:

命名空间std

标准:C++17(至少使用 /std:c++17 进行编译。)

另请参阅

<any>
any_cast
make_any
swap
bad_any_cast