C++ Reference ยท Containers
C++ std::any class
Reference for std::any: a type-erased container for one copyable value of any type, with any_cast, type, has_value, emplace, reset and its costs and limits.
std::any (C++17, header <any>) can hold one value of any copy-constructible type, and remembers that type at run time. You put a value in by assignment or construction, ask which type it holds with type(), and get it back with std::any_cast<T>. If the cast names the wrong type, it fails loudly instead of reinterpreting memory.
This is type erasure: the any object has one fixed type, but the value inside can change type freely. Small values may be stored inside the object; larger ones are heap-allocated. Because the set of possible types is open, the compiler cannot check your casts, so use std::any only when a closed set (std::variant) or a common base class will not do.
#include <any> std::any box = 42; int n = std::any_cast<int>(box);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
class any; any() noexcept; template<class T> any(T&& value); template<class T> T any_cast(const any& operand); template<class T> T* any_cast(any* operand) noexcept; template<class T, class... Args> any make_any(Args&&... args);
| Name | Description |
|---|---|
value |
The value to store. Its decayed type must be copy-constructible; a copy or move is stored. |
T |
The exact type to retrieve. It must match the stored type (ignoring top-level const and references in the cast). |
operand |
The any to read. The pointer overload returns nullptr instead of throwing. |
Members: has_value, type (returns std::type_info, typeid(void) when empty), emplace<T>, reset, swap.
Returns: the reference or value overloads return the stored object by the requested form; the pointer overload returns a pointer to it or nullptr.
Errors / undefined behavior: the non-pointer any_cast throws std::bad_any_cast when the type does not match or the any is empty. Types that are not copyable (for example std::unique_ptr) cannot be stored.
Usage notes
- The cast type must be exact. An
intis not along, and a string literal is stored asconst char*, notstd::string. Storestd::string("...")or"..."swhen you want a string. - Prefer the pointer form of
any_castwhen the type is uncertain: it is exception-free and combines the check and the access. - Cost: a heap allocation for values that do not fit the small buffer (implementation-defined), plus a copy whenever the
anyis copied. Usestd::variantfor a fixed list of types. - Needs RTTI:
type()and the cast checks rely on run-time type information; builds with-fno-rttitypically cannot usestd::anyfully. - Move-only types are rejected at compile time. Wrap them in
std::shared_ptror use a custom type-erasing class. - Comparison:
std::anyhas no==. Compare the unpacked values after checkingtype(). - Good uses: property bags, plugin or scripting interfaces, opaque user-data slots. Bad uses: anything where the set of types is known.