...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::any — A class whose instances can hold instances of any type that satisfies ValueType requirements.
// 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
public member functionsany() noexcept;
Postconditions: |
this->empty() is true. |
template<typename ValueType> any(const ValueType & value);
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. |
any(const any & other);
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. |
any(any && other) noexcept;
Move constructor that moves content of other
into new instance and leaves other
empty.
Postconditions: |
other->empty() is true |
Throws: |
Nothing. |
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);
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. |
~any() noexcept;
Releases any and all resources used in management of instance.
Throws: |
Nothing. |
any & swap(any & rhs) noexcept;
Exchange of the contents of *this
and rhs
.
Returns: |
|
Throws: |
Nothing. |
any & operator=(const any & rhs);
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. |
any & operator=(any && rhs) noexcept;
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: |
|
Throws: |
Nothing. |
template<typename ValueType> any & operator=(ValueType && rhs);
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. |
bool empty() const noexcept;
Returns: |
|
Throws: |
Nothing. |
void clear() noexcept;
Postconditions: |
this->empty() is true |
const boost::typeindex::type_info & type() const noexcept;
Useful for querying against types known either at compile time or only at runtime.
Returns: |
the |