|
|
Defined in header <any> | ||
class any; | (since C++17) | |
The classany
describes a type-safe container for single values of anycopy constructible type.
any
stores an instance of any type that satisfies the constructor requirements or is empty, and this is referred to as thestate of the classany
object. The stored instance is called the contained object. Two states are equivalent if they are either both empty or if both are not empty and if the contained objects are equivalent.any_cast
functions provide type-safe access to the contained object.Typically, implementations apply small objects optimization (avoid dynamic allocations) to types for whichstd::is_nothrow_move_constructible istrue.
Contents |
constructs anany object(public member function)[edit] | |
assigns anany object(public member function)[edit] | |
destroys anany object(public member function)[edit] | |
Modifiers | |
change the contained object, constructing the new object directly (public member function)[edit] | |
destroys contained object (public member function)[edit] | |
swaps twoany objects(public member function)[edit] | |
Observers | |
checks if object holds a value (public member function)[edit] | |
returns thetypeid of the contained value(public member function)[edit] |
(C++17) | specializes thestd::swap algorithm (function)[edit] |
(C++17) | type-safe access to the contained object (function template)[edit] |
(C++17) | creates anany object(function template)[edit] |
(C++17) | exception thrown by the value-returning forms ofany_cast on a type mismatch(class)[edit] |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_any | 201606L | (C++17) | std::any |
#include <any>#include <iostream> int main(){std::cout<<std::boolalpha; // any type std::any a=1;std::cout<< a.type().name()<<": "<<std::any_cast<int>(a)<<'\n'; a=3.14;std::cout<< a.type().name()<<": "<<std::any_cast<double>(a)<<'\n'; a=true;std::cout<< a.type().name()<<": "<<std::any_cast<bool>(a)<<'\n'; // bad casttry{ a=1;std::cout<<std::any_cast<float>(a)<<'\n';}catch(conststd::bad_any_cast& e){std::cout<< e.what()<<'\n';} // has value a=2;if(a.has_value())std::cout<< a.type().name()<<": "<<std::any_cast<int>(a)<<'\n'; // reset a.reset();if(!a.has_value())std::cout<<"no value\n"; // pointer to contained data a=3;int* i=std::any_cast<int>(&a);std::cout<<*i<<'\n';}
Possible output:
int: 1double: 3.14bool: truebad any_castint: 2no value3
(C++11) | copyable wrapper of any copy constructible callable object (class template)[edit] |
(C++23) | move-only wrapper of any callable object that supports qualifiers in a given call signature (class template)[edit] |
(C++17) | a type-safe discriminated union (class template)[edit] |
(C++17) | a wrapper that may or may not hold an object (class template)[edit] |
(C++11) | smart pointer with unique object ownership semantics (class template)[edit] |
(C++26) | a wrapper containing dynamically-allocated object with value-like semantics (class template)[edit] |
(C++26) | a polymorphic wrapper containing dynamically-allocated object with value-like semantics (class template)[edit] |