Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Class any

boost::any — A class whose instances can hold instances of any type that satisfies ValueType requirements.

Synopsis

// In header: <boost/any.hpp>


class any {
public:
  // construct/copy/destruct
  any();
  any(const any &);
  any(any &&);
  template<typename ValueType> any(const ValueType &);
  template<typename ValueType> any(ValueType &&);
  any & operator=(const any &);
  any & operator=(any &&);
  template<typename ValueType> any & operator=(const ValueType &);
  template<typename ValueType> any & operator=(ValueType &&);
  ~any();

  // modifiers
  any & swap(any &);

  // queries
  bool empty() const;
  const std::type_info & type() const;
};

Description

any public construct/copy/destruct

  1. any();

    Postconditions:

    this->empty()
  2. any(const any & other);

    Effects:

    Copy constructor that copies content of other into new instance, so that any content is equivalent in both type and value to the content of other, or empty if other is empty.

    Throws:

    May fail with a std::bad_alloc exception or any exceptions arising from the copy constructor of the contained type.
  3. any(any && other);

    Effects:

    Move constructor that moves content of other into new instance and leaves other empty.

    Postconditions:

    other->empty()

    Throws:

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

    Effects:

    Makes a copy of value, so that the initial content of the new instance is equivalent in both type and value to value.

    Throws:

    std::bad_alloc or any exceptions arising from the copy constructor of the contained type.
  5. template<typename ValueType> any(ValueType && value);

    Effects:

    Forwards value, so that the initial content of the new instance is equivalent in both type and value to value before the forward.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  6. any & operator=(const any & rhs);

    Effects:

    Copies content of rhs into current instance, discarding previous content, so that the new content is equivalent in both type and value to the content of rhs, or empty if rhs.empty().

    Throws:

    std::bad_alloc or any exceptions arising from the copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
  7. any & operator=(any && rhs);

    Effects:

    Moves content of rhs into current instance, discarding previous content, so that the new content is equivalent in both type and value to the content of rhs before move, or empty if rhs.empty().

    Postconditions:

    rhs->empty()

    Throws:

    Nothing.
  8. template<typename ValueType> any & operator=(const ValueType & rhs);

    Effects:

    Makes a copy of rhs, discarding previous content, so that the new content of is equivalent in both type and value to rhs.

    Throws:

    std::bad_alloc or any exceptions arising from the copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
  9. template<typename ValueType> any & operator=(ValueType && rhs);

    Effects:

    Forwards rhs, discarding previous content, so that the new content of is equivalent in both type and value to rhs before forward.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
  10. ~any();

    Effects:

    Releases any and all resources used in management of instance.

    Throws:

    Nothing.

any modifiers

  1. any & swap(any & rhs);

    Effects:

    Exchange of the contents of *this and rhs.

    Returns:

    *this

    Throws:

    Nothing.

any queries

  1. bool empty() const;

    Returns:

    true if instance is empty, otherwise false.

    Throws:

    Nothing.
  2. const std::type_info & type() const;

    Returns:

    the typeid of the contained value if instance is non-empty, otherwise typeid(void).

    Notes:

    Useful for querying against types known either at compile time or only at runtime.

PrevUpHomeNext