...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::type_erasure::any
// In header: <boost/type_erasure/any.hpp> template<typename Concept, typename T = _self> class any { public: // construct/copy/destruct any(); template<typename U> any(U &&); template<typename U, typename Map> any(U &&, const static_binding< Map > &); any(const any &); template<typename Concept2, typename Tag2> any(const any< Concept2, Tag2 > &); template<typename Concept2, typename Tag2, typename Map> any(const any< Concept2, Tag2 > &, const static_binding< Map > &); template<typename Concept2, typename Tag2> any(const any< Concept2, Tag2 > &, const binding< Concept > &); template<class... U> explicit any(U &&...); template<class... U> explicit any(const binding< Concept > &, U &&...); any& operator=(const any &); template<typename U> any& operator=(const U &); ~any(); };
The class template any can store any object that models a specific Concept. It dispatches all the functions defined by the Concept to the contained type at runtime.
See Also:
concept_of, placeholder_of, any_cast, is_empty, binding_of, typeid_of
typename Concept
The Concept that the stored type should model.
typename T = _self
A placeholder
specifying which type this is.
any
public
construct/copy/destructany();
Constructs an empty any
.
Except as otherwise noted, all operations on an empty any
result in a bad_function_call
exception. The copy-constructor of an empty any
creates another null any
. The destructor of an empty any
is a no-op. Comparison operators treat all empty anys
as equal. typeid_of
applied to an empty any
returns typeid(void)
.
An any
which does not include relaxed
in its Concept can never be null.
See Also:
Requires: |
relaxed must be in |
Throws: |
Nothing. |
template<typename U> any(U && data);
Constructs an any
to hold a copy of data
. The Concept
will be instantiated with the placeholder T
bound to U.
Note | |
---|---|
This constructor never matches if the argument is an |
Parameters: |
|
||
Requires: |
|
||
Throws: |
std::bad_alloc or whatever that the copy constructor of U throws. |
template<typename U, typename Map> any(U && data, const static_binding< Map > & binding);
Constructs an any
to hold a copy of data
with explicitly specified placeholder bindings.
Note | |
---|---|
This constructor never matches if the argument is an |
Parameters: |
|
||||
Requires: |
|
||||
Throws: |
std::bad_alloc or whatever that the copy constructor of U throws. |
any(const any & other);
Copies an any
.
Parameters: |
|
||
Requires: |
|
||
Throws: |
std::bad_alloc or whatever that the copy constructor of the contained type throws. |
template<typename Concept2, typename Tag2> any(const any< Concept2, Tag2 > & other);
Upcasts from an any
with stricter requirements to an any
with weaker requirements.
Parameters: |
|
||
Requires: |
After substituting |
||
Throws: |
std::bad_alloc or whatever that the copy constructor of the contained type throws. |
template<typename Concept2, typename Tag2, typename Map> any(const any< Concept2, Tag2 > & other, const static_binding< Map > & binding);
Constructs an any
from another any
.
Parameters: |
|
||||
Requires: |
After substituting placeholders according to |
||||
Throws: |
std::bad_alloc or whatever that the copy constructor of the contained type throws. |
template<typename Concept2, typename Tag2> any(const any< Concept2, Tag2 > & other, const binding< Concept > & binding);
Constructs an any
from another any
.
Warning | |
---|---|
This constructor is potentially dangerous, as it cannot check at compile time whether the arguments match. |
Parameters: |
|
||||
Requires: |
The type stored in |
||||
Postconditions: |
binding_of(*this) == |
||||
Throws: |
std::bad_alloc or whatever that the copy constructor of the contained type throws. |
template<class... U> explicit any(U &&... arg);
Calls a constructor of the contained type. The bindings will be deduced from the arguments.
Note | |
---|---|
This constructor is never chosen if any other constructor can be called instead. |
Parameters: |
|
||
Requires: |
At least one of the arguments must by an any with the same The bindings of all the arguments that are any's, must be the same. |
||
Throws: |
std::bad_alloc or whatever that the constructor of the contained type throws. |
template<class... U> explicit any(const binding< Concept > & binding, U &&... arg);
Calls a constructor of the contained type.
Parameters: |
|
||||
Requires: |
The contained type of every argument that is an any, must be the same as that specified by |
||||
Postconditions: |
binding_of(*this) == |
||||
Throws: |
std::bad_alloc or whatever that the constructor of the contained type throws. |
any& operator=(const any & other);
Assigns to an any
.
If an appropriate overload of assignable
is not available and relaxed
is in Concept
, falls back on constructing from other
.
Throws: |
Whatever the assignment operator of the contained type throws. When falling back on construction, throws std::bad_alloc or whatever the copy constructor of the contained type throws. In this case assignment provides the strong exception guarantee. When calling the assignment operator of the contained type, the exception guarantee is whatever the contained type provides. |
template<typename U> any& operator=(const U & other);
Assigns to an any
.
If an appropriate overload of assignable
is not available and relaxed
is in Concept
, falls back on constructing from other
.
Throws: |
Whatever the assignment operator of the contained type throws. When falling back on construction, throws std::bad_alloc or whatever the copy constructor of the contained type throws. In this case assignment provides the strong exception guarantee. When calling an assignment operator of the contained type, the exception guarantee is whatever the contained type provides. |
~any();
Requires: |
|