std::any
std::any is a value that can hold a single copy-constructible object of any type, or hold nothing. The type is erased; you recover it with std::any_cast. Prefer std::variant when the set of types is known.
Detailed Explanation
A plugin returns a setting that might be an int, a string, a colour, or a type the plugin author has not invented yet. You cannot write a variant for an open set. std::any stores the object and a description of its type. Readers ask for a type. If they guess wrong, the cast throws or returns a null pointer.
any is a value. Copying it copies the contained object (and therefore requires the contained type to be copy-constructible). Moving it moves the contained object when it can. Empty is any{} or after reset(). has_value() reports whether something is stored. type() returns a const std::type_info& you can compare to typeid(T).
std::any_cast<T>(a) by value or by reference throws std::bad_any_cast on mismatch. std::any_cast<T>(&a) on a pointer to any returns T* or nullptr. Use the pointer form when a miss is normal.
Small-object optimisation is common: implementations store tiny types inside the any object and heap-allocate large ones. You should still treat any as heavier than a variant. Each copy may allocate. Each cast does a typeid comparison. A vector<any> is a last resort for heterogeneous bags, not a general-purpose list.
When to use any:
- A property map (
std::map<std::string, std::any>) for loosely typed configuration. - A type-erased callback argument you do not own the type of.
- Interfacing with a C API that handed you a
void*plus a type tag you then wrap.
When not to:
- A closed set of types:
variant. - A maybe-missing
T:optional<T>. - A maybe-missing heap object you own:
unique_ptr<T>. - A polymorphic hierarchy you control: a base pointer or
unique_ptr<Base>.
any does not let you visit all possible types. There is no std::visit for any. You must know, or store alongside it, what you put in. That is the whole cost of an open set.
The contained type must be copy-constructible in C++17. Move-only types (unique_ptr) cannot go in any. If you need move-only type erasure, write a custom wrapper or use a unique_ptr to a base.
any is not a replacement for templates. A template still generates typed code. any pushes the type check to runtime. That is sometimes the requirement. It is never the default.
Code Examples
#include <any>
#include <iostream>
#include <string>
int main() {
std::any setting = 42;
std::cout << std::any_cast<int>(setting) << '\n'; // prints: 42
setting = std::string{"dark"};
if (auto* s = std::any_cast<std::string>(&setting)) {
std::cout << *s << '\n'; // prints: dark
}
try {
std::cout << std::any_cast<int>(setting) << '\n';
} catch (const std::bad_any_cast&) {
std::cout << "wrong type\n"; // prints: wrong type
}
}#include <any>
#include <map>
#include <string>
int main() {
std::map<std::string, std::any> cfg;
cfg["port"] = 8080;
cfg["name"] = std::string{"api"};
int port = std::any_cast<int>(cfg.at("port"));
(void)port;
}Common Gotchas
`any_cast<T>` on a value throws
a miss is not a default-constructed `T`. Use the pointer overload when the type is uncertain.
Copies can allocate
assigning a large `std::string` into an `any`, then copying the `any`, copies the string. Store `shared_ptr<T>` inside the `any` if many readers should share one object.
Move-only types are not allowed
`any a = std::make_unique<int>(1);` does not compile. Erase move-only types with your own virtual base.
`type()` on an empty `any` is `typeid(void)`
do not assume it equals the last stored type after `reset()`.