Skip to content
Tutorials Center
My learning

Topics

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.

C++
#include <any>

std::any box = 42;
int n = std::any_cast<int>(box);

Try it

C++ Holding values of different types

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.

C++ Exact types matter

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

C++
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 int is not a long, and a string literal is stored as const char*, not std::string. Store std::string("...") or "..."s when you want a string.
  • Prefer the pointer form of any_cast when 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 any is copied. Use std::variant for a fixed list of types.
  • Needs RTTI: type() and the cast checks rely on run-time type information; builds with -fno-rtti typically cannot use std::any fully.
  • Move-only types are rejected at compile time. Wrap them in std::shared_ptr or use a custom type-erasing class.
  • Comparison: std::any has no ==. Compare the unpacked values after checking type().
  • Good uses: property bags, plugin or scripting interfaces, opaque user-data slots. Bad uses: anything where the set of types is known.