Boost C++ 库

……在世界上最受推崇、设计最精良的 C++ 库项目之一。 Herb SutterAndrei AlexandrescuC++ Coding Standards

类 any - Boost C++ 函数库
PrevUpHomeNext

类 any

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 公有成员函数

  1. any() noexcept;

    后置条件

    this->empty() 为 true。

  2. template<typename ValueType> any(const ValueType & value);

    创建 value 的副本,以便新实例的初始内容在类型和值上都等同于 value

    抛出

    std::bad_alloc 或由所包含类型的复制构造函数引起的任何异常。
  3. any(const any & other);

    复制构造函数,将 other 的内容复制到新实例中,以便任何内容在类型和值上都等同于 other 的内容,如果 other 为空则为空。

    抛出

    可能会因 std::bad_alloc 异常或由所包含类型的复制构造函数引起的任何异常而失败。
  4. any(any && other) noexcept;

    移动构造函数,将 other 的内容移动到新实例中,并使 other 为空。

    后置条件

    other->empty() 为 true

    抛出

    无。
  5. 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 或包含类型的移动构造函数或复制构造函数引起的任何异常。
  6. ~any() noexcept;

    释放实例管理所使用的所有资源。

    抛出

    无。
  7. any & swap(any & rhs) noexcept;

    交换 *thisrhs 的内容。

    返回

    *this

    抛出

    无。
  8. any & operator=(const any & rhs);

    rhs 的内容复制到当前实例中,丢弃先前的内容,以便新内容在类型和值上都等同于 rhs 的内容,如果 rhs.empty() 则为空。

    抛出

    std::bad_alloc 或由所包含类型的复制构造函数引起的任何异常。赋值满足异常安全的强保证。
  9. any & operator=(any && rhs) noexcept;

    rhs 的内容移动到当前实例中,丢弃先前的内容,以便新内容在类型和值上都等同于移动前 rhs 的内容,如果 rhs.empty() 则为空。

    后置条件

    rhs->empty() 为 true

    抛出

    无。
  10. template<typename ValueType> any & operator=(ValueType && rhs);

    转发(forward) rhs,丢弃先前的内容,使新内容在类型和值上都等同于转发前的 rhs

    抛出

    std::bad_alloc 或包含类型的移动构造函数或复制构造函数引起的任何异常。赋值满足强异常安全保证。
  11. bool empty() const noexcept;

    返回

    如果实例为空,则为 true,否则为 false

    抛出

    无。
  12. void clear() noexcept;

    后置条件

    this->empty() 为 true

  13. const boost::typeindex::type_info & type() const noexcept;

    对于在编译时或运行时才知道的类型进行查询非常有用。

    返回

    如果实例非空,则为包含值的 typeid,否则为 typeid(void)


PrevUpHomeNext