boost::any — 一个类,其实例可以容纳满足 ValueType 要求的任何类型的实例。
// In header: <boost/any.hpp> class any { public: // public member functions any() noexcept; template<typename ValueType> any(const ValueType &); any(const any &); any(any &&) noexcept; template<typename ValueType> any(ValueType &&, typename std::enable_if<!std::is_same< any &, ValueType >::value >::type * = 0, typename std::enable_if<!std::is_const< ValueType >::value >::type * = 0); ~any() noexcept; any & swap(any &) noexcept; any & operator=(const any &); any & operator=(any &&) noexcept; template<typename ValueType> any & operator=(ValueType &&); bool empty() const noexcept; void clear() noexcept; const boost::typeindex::type_info & type() const noexcept; };
any
公有成员函数any() noexcept;
后置条件 |
this->empty() 为 true。 |
template<typename ValueType> any(const ValueType & value);
创建 value
的副本,以便新实例的初始内容在类型和值上都等同于 value
。
抛出 |
std::bad_alloc 或由所包含类型的复制构造函数引起的任何异常。 |
any(const any & other);
复制构造函数,将 other
的内容复制到新实例中,以便任何内容在类型和值上都等同于 other
的内容,如果 other
为空则为空。
抛出 |
可能会因 std::bad_alloc 异常或由所包含类型的复制构造函数引起的任何异常而失败。 |
any(any && other) noexcept;
移动构造函数,将 other
的内容移动到新实例中,并使 other
为空。
后置条件 |
other->empty() 为 true |
抛出 |
无。 |
template<typename ValueType> any(ValueType && value, typename std::enable_if<!std::is_same< any &, ValueType >::value >::type * = 0, typename std::enable_if<!std::is_const< ValueType >::value >::type * = 0);
转发 value
,以便新实例的初始内容在转发之前在类型和值上都等同于 value
。
抛出 |
std::bad_alloc 或包含类型的移动构造函数或复制构造函数引起的任何异常。 |
~any() noexcept;
释放实例管理所使用的所有资源。
抛出 |
无。 |
any & swap(any & rhs) noexcept;
交换 *this
和 rhs
的内容。
返回 |
|
抛出 |
无。 |
any & operator=(const any & rhs);
将 rhs
的内容复制到当前实例中,丢弃先前的内容,以便新内容在类型和值上都等同于 rhs
的内容,如果 rhs.empty()
则为空。
抛出 |
std::bad_alloc 或由所包含类型的复制构造函数引起的任何异常。赋值满足异常安全的强保证。 |
any & operator=(any && rhs) noexcept;
将 rhs
的内容移动到当前实例中,丢弃先前的内容,以便新内容在类型和值上都等同于移动前 rhs
的内容,如果 rhs.empty()
则为空。
后置条件 |
|
抛出 |
无。 |
template<typename ValueType> any & operator=(ValueType && rhs);
转发(forward) rhs
,丢弃先前的内容,使新内容在类型和值上都等同于转发前的 rhs
。
抛出 |
std::bad_alloc 或包含类型的移动构造函数或复制构造函数引起的任何异常。赋值满足强异常安全保证。 |
bool empty() const noexcept;
返回 |
如果实例为空,则为 |
抛出 |
无。 |
void clear() noexcept;
后置条件 |
this->empty() 为 true |
const boost::typeindex::type_info & type() const noexcept;
对于在编译时或运行时才知道的类型进行查询非常有用。
返回 |
如果实例非空,则为包含值的 |