...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::anys::unique_any — A class whose instances can hold instances of any type (including non-copyable and non-movable types).
// In header: <boost/any/unique_any.hpp> class unique_any { public: // construct/copy/destruct unique_any(); unique_any(unique_any &&); template<typename T> unique_any(T &&, typename std::enable_if<!std::is_same< T &&, boost::any && >::value >::type * = nullptr); template<typename BoostAny> unique_any(BoostAny &&, typename std::enable_if< std::is_same< BoostAny &&, boost::any && >::value >::type * = nullptr) noexcept; template<typename T, class... Args> explicit unique_any(in_place_type_t< T >, Args &&...); template<typename T, typename U, class... Args> explicit unique_any(in_place_type_t< T >, std::initializer_list< U >, Args &&...); unique_any & operator=(unique_any &&); template<typename T> unique_any & operator=(T &&); ~unique_any(); // public member functions template<typename T, class... Args> std::decay< T >::type & emplace(Args &&...); template<typename T, typename U, class... Args> std::decay< T >::type & emplace(std::initializer_list< U >, Args &&...); void reset() noexcept; void swap(unique_any &) noexcept; bool has_value() const noexcept; const boost::typeindex::type_info & type() const noexcept; };
unique_any
public
construct/copy/destructunique_any();
Postconditions: |
this->has_value() is false. |
unique_any(unique_any && other);
Move constructor that moves content of other
into new instance and leaves other
empty.
Postconditions: |
other->has_value() is false. |
Throws: |
Nothing. |
template<typename T> unique_any(T && value, typename std::enable_if<!std::is_same< T &&, boost::any && >::value >::type * = nullptr);
Forwards value
, so that the content of the new instance has type std::decay_t<T>
and value is the value
before the forward.
Throws: |
std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. |
template<typename BoostAny> unique_any(BoostAny && value, typename std::enable_if< std::is_same< BoostAny &&, boost::any && >::value >::type * = nullptr) noexcept;
Moves the content of
into *this.boost::any
Postconditions: |
|
Throws: |
Nothing. |
template<typename T, class... Args> explicit unique_any(in_place_type_t< T >, Args &&... args);
Inplace constructs T
from forwarded args...
, so that the content of *this
is equivalent in type to std::decay_t<T>
.
Throws: |
std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. |
template<typename T, typename U, class... Args> explicit unique_any(in_place_type_t< T >, std::initializer_list< U > il, Args &&... args);
Inplace constructs T
from li
and forwarded args...
, so that the initial content of *this
is equivalent in type to std::decay_t<T>
.
Throws: |
std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. |
unique_any & operator=(unique_any && rhs);
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 T> unique_any & operator=(T && 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. |
~unique_any();
Releases any and all resources used in management of instance.
Throws: |
Nothing. |
unique_any
public member functionstemplate<typename T, class... Args> std::decay< T >::type & emplace(Args &&... args);
Inplace constructs T
from forwarded args...
, discarding previous content, so that the content of *this
is equivalent in type to std::decay_t<T>
.
Returns: |
reference to the content of |
Throws: |
std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. |
template<typename T, typename U, class... Args> std::decay< T >::type & emplace(std::initializer_list< U > il, Args &&... args);
Inplace constructs T
from li
and forwarded args...
, discarding previous content, so that the content of *this
is equivalent in type to std::decay_t<T>
.
Returns: |
reference to the content of |
Throws: |
std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. |
void reset() noexcept;
Postconditions: |
this->has_value() is false. |
void swap(unique_any & rhs) noexcept;
Exchange of the contents of *this
and rhs
.
Returns: |
|
Throws: |
Nothing. |
bool has_value() const noexcept;
Returns: |
|
Throws: |
Nothing. |
const boost::typeindex::type_info & type() const noexcept;
Useful for querying against types known either at compile time or only at runtime.
Returns: |
the |