Any Component

The any component is currently available in Boost (and has been for quite some time). There is, at the time of this writing, a draft proposal for a standard any type, which has several API distinctions from the Boost.Any implementation, from which it is inspired. Instance, the proposal recommends that an any implementation make a small object optimization to avoid allocations for small and integral types. The MNMLSTC Core any implements this optimization, and attempts to follow the proposal as closely as possible.

The any component resides in <core/any.hpp>.


The any component does not implement the constructors defined in the any proposal. The reason for this is due to:

  • Several libraries do not include the constructors necessary for types that define them
  • In most cases the compiler cannot properly disambiguate which constructor to use, even with the use of type traits. This was mentioned on the LLVM blog, and is mentioned as part of LWG Defect 2255.

Because this component was written for C++11, it was decided to omit these additional allocator aware constructors.

class bad_any_cast

This is the exception thrown when any_cast() fails. It inherits from std::bad_cast.

char const* what() const noexcept

Returns the string “bad any cast”. At some point in the future a more descriptive error may be given.

class any
any(any const&)
any(any&&) noexcept
any() noexcept

The default set of constructors work as one might imagine. The copy constructor is not marked as noexcept as the underlying type may throw an exception, and due to the type erasure performed, the any has no way of enforcing this at compile time.

any(ValueType&& value)

When constructing an any with a given ValueType, it will perform a series of compile time checks to see whether it should perform the small object optimization. If the object is deemed small enough, it will not allocate memory. Otherwise, a new ValueType will be allocated via operator new, and constructed with the given value.

Type value:ValueType&&
Raises:Any exceptions thrown by the copy or move constructor of the given ValueType.
any& operator=(any const&)
any& operator=(any&&) noexcept

Assigns the contents of the incoming any to *this.

any& operator=(ValueType&& value)

Assigns value to *this. If *this already manages a contained object, it will be destroyed after value is assigned.

New in version 1.1: This function was unfortunately omitted from the 1.0 release.

void any::swap(any&) noexcept

Swaps the object contained within the given any with the one contained within *this.

std::type_info const& type() const noexcept

Returns the std::type_info for the type contained within. If the any is empty, it will return typeid(void).

bool empty() const noexcept

If the any does not contain any data (i.e. any::type() returns typeid(void)), it will return true.

void clear() noexcept
Postcondition:any::empty() == true

Destroys the object contained within the any.

ValueType any_cast(any const& operand)
ValueType any_cast(any&& operand)
ValueType any_cast(any& operand)
Returns:*any_cast<add_const_t<remove_reference_t<ValueType>>(&operand) for the first any_cast() signature. For the other overloads, the return type is *any_cast<remove_reference_t<ValueType>>(&operand).

Given a type ValueType, it will attempt to extract the value stored within the given any. ValueType may be either concrete or a reference type. If typeid(remove_reference_t<ValueType>) is not equal to the value returned by any::type(), bad_any_cast is thrown. Some usage examples:

any x(5) // x holds an int
auto y = any_cast<int>(x); // cast to a value
any_cast<int&>(x) = 10; // cast to a reference for mutation.

x = std::string { "Woof" }; // x now holds a string.
auto woof = std::move(any_cast<std::string&>(x)); // move value in x
assert(any_cast<std::string const&>(x) == "");
ValueType const* any_cast(any const* operand)
ValueType* any_cast(any* operand)
Returns:ValueType if operand is not equal to nullptr and typeid(ValueType) is the same as the value returned by any::type(), a pointer to the object managed by operand is returned. Otherwise, nullptr.