std::make_any - cppreference.com (original) (raw)
| Defined in header | ||
|---|---|---|
| template< class T, class... Args > std::any make_any( Args&&... args ); | (1) | (since C++17) |
| template< class T, class U, class... Args > std::any make_any( std::initializer_list<U> il, Args&&... args ); | (2) | (since C++17) |
Constructs an any object containing an object of type T, passing the provided arguments to T's constructor.
[edit] Example
#include #include #include #include #include int main() { auto a0 = std::make_any<std::string>("Hello, std::any!\n"); auto a1 = std::make_any<std::complex>(0.1, 2.3); std::cout << std::any_cast<std::string&>(a0); std::cout << std::any_cast<std::complex&>(a1) << '\n'; using lambda = std::function<void(void)>; // Put a lambda into std::any. Attempt #1 (failed). std::any a2 = [] { std::cout << "Lambda #1.\n"; }; std::cout << "a2.type() = "" << a2.type().name() << ""\n"; // any_cast casts to <void(void)> but actual type is not // a std::function..., but ~ main::{lambda()#1}, and it is // unique for each lambda. So, this throws... try { std::any_cast(a2)(); } catch (std::bad_any_cast const& ex) { std::cout << ex.what() << '\n'; } // Put a lambda into std::any. Attempt #2 (successful). auto a3 = std::make_any([] { std::cout << "Lambda #2.\n"; }); std::cout << "a3.type() = "" << a3.type().name() << ""\n"; std::any_cast(a3)(); }
Possible output:
Hello, std::any! (0.1,2.3) a2.type() = "Z4mainEUlvE_" bad any_cast a3.type() = "St8functionIFvvEE" Lambda #2.