Backport
bpstd::any Class Reference

An object that can hold values of any type via type-erasure. More...

#include <any.hpp>

Public Member Functions

 any () noexcept
 Constructs an any instance that does not contain any value.
 
 any (any &&other) noexcept
 Moves an any instance by moving the stored underlying value. More...
 
 any (const any &other)
 Copies an any instance by copying the stored underlying value. More...
 
template<typename ValueType , typename = enable_if_t<!is_same<decay_t<ValueType>,any>::value && is_copy_constructible<decay_t<ValueType>>::value>>
 any (ValueType &&value)
 Constructs this any using value for the underlying instance. More...
 
template<typename ValueType , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
 any (in_place_type_t< ValueType >, Args &&...args)
 Constructs an 'any' of type ValueType by forwarding args to its constructor. More...
 
template<typename ValueType , typename U , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,std::initializer_list<U>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
 any (in_place_type_t< ValueType >, std::initializer_list< U > il, Args &&...args)
 Constructs an 'any' of type ValueType by forwarding args to its constructor. More...
 
any & operator= (any &&other) noexcept
 Assigns the contents of other to this any. More...
 
any & operator= (const any &other)
 Assigns the contents of other to this any. More...
 
template<typename ValueType , typename = enable_if_t<!is_same<decay_t<ValueType>,any>::value && is_copy_constructible<decay_t<ValueType>>::value>>
any & operator= (ValueType &&value)
 Assigns value to this any. More...
 
void reset () noexcept
 Destroys the underlying stored value, leaving this any empty.
 
void swap (any &other) noexcept
 Swaps the contents of this with other. More...
 
bool has_value () const noexcept
 Checks whether this any contains a value. More...
 
const std::type_info & type () const noexcept
 Gets the type_info for the underlying stored type, or typeid(void) if has_value() returns false. More...
 
template<typename ValueType , typename >
BPSTD_INLINE_VISIBILITY any (ValueType &&value)
 
template<typename ValueType , typename... Args, typename >
BPSTD_INLINE_VISIBILITY any (in_place_type_t< ValueType >, Args &&...args)
 
template<typename ValueType , typename U , typename... Args, typename >
BPSTD_INLINE_VISIBILITY any (in_place_type_t< ValueType >, std::initializer_list< U > il, Args &&...args)
 
template<typename ValueType , typename >
BPSTD_INLINE_VISIBILITY bpstd::any & operator= (ValueType &&value)
 
template<typename ValueType , typename... Args, typename >
BPSTD_INLINE_VISIBILITY bpstd::decay_t< ValueType > & emplace (Args &&...args)
 
template<typename ValueType , typename U , typename... Args, typename >
BPSTD_INLINE_VISIBILITY bpstd::decay_t< ValueType > & emplace (std::initializer_list< U > il, Args &&...args)
 
template<typename ValueType , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
decay_t< ValueType > & emplace (Args &&...args)
 Emplaces a ValueType into this any, destroying the previous value if it contained one. More...
 
template<typename ValueType , typename U , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,std::initializer_list<U>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
decay_t< ValueType > & emplace (std::initializer_list< U > il, Args &&...args)
 Emplaces a ValueType into this any, destroying the previous value if it contained one. More...
 

Friends

template<typename T >
T * any_cast (any *) noexcept
 Attempts to cast an any back to the underlying type T. More...
 
template<typename T >
const friend T * any_cast (const any *) noexcept
 

Detailed Description

An object that can hold values of any type via type-erasure.

The class any describes a type-safe container for single values of any type.

1) An object of class any stores an instance of any type that satisfies the constructor requirements or is empty, and this is referred to as the state of the class any object. The stored instance is called the contained object. Two states are equivalent if they are either both empty or if both are not empty and if the contained objects are equivalent.

2) The non-member any_cast functions provide type-safe access to the contained object.

This implementation uses small-buffer optimization to avoid dynamic memory if the object is below a (4 * sizeof(void*))

Definition at line 84 of file any.hpp.

Constructor & Destructor Documentation

◆ any() [1/5]

BPSTD_INLINE_VISIBILITY bpstd::any::any ( any &&  other)
inlinenoexcept

Moves an any instance by moving the stored underlying value.

Postcondition
other is left valueless
Parameters
otherthe other instance to move

Definition at line 592 of file any.hpp.

◆ any() [2/5]

BPSTD_INLINE_VISIBILITY bpstd::any::any ( const any &  other)
inline

Copies an any instance by copying the stored underlying value.

Parameters
otherthe other instance to copy

Definition at line 603 of file any.hpp.

◆ any() [3/5]

template<typename ValueType , typename = enable_if_t<!is_same<decay_t<ValueType>,any>::value && is_copy_constructible<decay_t<ValueType>>::value>>
bpstd::any::any ( ValueType &&  value)

Constructs this any using value for the underlying instance.

Parameters
valuethe value to construct this any out of

◆ any() [4/5]

template<typename ValueType , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
bpstd::any::any ( in_place_type_t< ValueType >  ,
Args &&...  args 
)
explicit

Constructs an 'any' of type ValueType by forwarding args to its constructor.

Note
This constructor only participates in overload resolution if ValueType is constructible from args
Parameters
argsthe arguments to forward to ValueType's constructor

◆ any() [5/5]

template<typename ValueType , typename U , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,std::initializer_list<U>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
bpstd::any::any ( in_place_type_t< ValueType >  ,
std::initializer_list< U >  il,
Args &&...  args 
)
explicit

Constructs an 'any' of type ValueType by forwarding args to its constructor.

Note
This constructor only participates in overload resolution if ValueType is constructible from args
Parameters
ilan initializer_list of arguments
argsthe arguments to forward to ValueType's constructor

Member Function Documentation

◆ emplace() [1/2]

template<typename ValueType , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
decay_t<ValueType>& bpstd::any::emplace ( Args &&...  args)

Emplaces a ValueType into this any, destroying the previous value if it contained one.

Template Parameters
ValueTypethe type to construct
Parameters
argsthe arguments to forward to ValueType's constructor
Returns
reference to the constructed value

◆ emplace() [2/2]

template<typename ValueType , typename U , typename... Args, typename = enable_if_t<is_constructible<decay_t<ValueType>,std::initializer_list<U>,Args...>::value && is_copy_constructible<decay_t<ValueType>>::value>>
decay_t<ValueType>& bpstd::any::emplace ( std::initializer_list< U >  il,
Args &&...  args 
)

Emplaces a ValueType into this any, destroying the previous value if it contained one.

Template Parameters
ValueTypethe type to construct
Parameters
argsthe arguments to forward to ValueType's constructor
Returns
reference to the constructed value

◆ has_value()

BPSTD_INLINE_VISIBILITY bool bpstd::any::has_value ( ) const
inlinenoexcept

Checks whether this any contains a value.

Returns
true if this contains a value

Definition at line 807 of file any.hpp.

◆ operator=() [1/3]

BPSTD_INLINE_VISIBILITY bpstd::any & bpstd::any::operator= ( any &&  other)
inlinenoexcept

Assigns the contents of other to this any.

Parameters
otherthe other any to move
Returns
reference to (*this)

Definition at line 669 of file any.hpp.

◆ operator=() [2/3]

BPSTD_INLINE_VISIBILITY bpstd::any & bpstd::any::operator= ( const any &  other)
inline

Assigns the contents of other to this any.

Parameters
otherthe other any to copy
Returns
reference to (*this)

Definition at line 683 of file any.hpp.

◆ operator=() [3/3]

template<typename ValueType , typename = enable_if_t<!is_same<decay_t<ValueType>,any>::value && is_copy_constructible<decay_t<ValueType>>::value>>
any& bpstd::any::operator= ( ValueType &&  value)

Assigns value to this any.

Parameters
valuethe value to assign
Returns
reference to (*this)

◆ swap()

BPSTD_INLINE_VISIBILITY void bpstd::any::swap ( any &  other)
inlinenoexcept

Swaps the contents of this with other.

Postcondition
other contains the old contents of this, and this contains the old contents of other
Parameters
otherthe other any to swap contents with

Definition at line 761 of file any.hpp.

◆ type()

const BPSTD_INLINE_VISIBILITY std::type_info & bpstd::any::type ( ) const
inlinenoexcept

Gets the type_info for the underlying stored type, or typeid(void) if has_value() returns false.

Returns
the typeid of the stored type

Definition at line 814 of file any.hpp.

Friends And Related Function Documentation

◆ any_cast

template<typename T >
T* any_cast ( any *  )
friend

Attempts to cast an any back to the underlying type T.

Template Parameters
Tthe type to cast to
Returns
pointer to the object if successfull, nullptr otherwise

The documentation for this class was generated from the following file: