A class that contains a type erased value. More...
Public Member Functions | |
any () noexcept=default | |
Constructs any with no value. More... | |
template<typename T > | |
any (T &&actual) noexcept | |
Constructs any with a value. More... | |
any (const any &other) | |
Copy constructor. | |
any (any &&other) | |
Move constructor. Must not throw. | |
any & | operator= (const any &other) |
Copy assignment. | |
any & | operator= (any &&other) |
Move assignment. The value of other is removed. | |
operator bool () const | |
Checks if any is in a proper state. More... | |
template<typename T > | |
bool | is () const |
Checks if the value of any is a certain type. More... | |
template<typename T > | |
const Decayed< T > & | as () const |
Casts any to access the contained value. More... | |
template<typename T > | |
Decayed< T > & | as () |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T > | |
auto | any_cast (const any &object) -> decltype(object.as< T >()) |
Helper function to cast any . More... | |
any
is a class that can contain most type of variables assuming they are copy constructible. This is similar to other dynamic typing languages where you could assign any type to a variable. The closest C++ alternative to this behaviour would be using a void*
but any
does this in a safer manner without having void
pointers.
Example:
Output:
hello world ! 1500
|
defaultnoexcept |
Constructs any
with no value. Accessing it with its empty state is undefined behaviour.
|
inlinenoexcept |
Constructs any
with a value. After construction, any
holds the value contained and can be accessed with is with the type of the variable provided. The type passed must be copy constructible or else a compiler error will be thrown.
actual | Value to construct any with. |
|
inline |
|
inline |
Checks if the value of any
is a certain type. A specific gotcha with this function is thinking the value of a string literal is std::string
, it is actually const char*
. This function should be called before calling as.
T | Type to check for. |
true
if the internal value is of type T
, false
otherwise.
|
inlineexplicit |
|
related |