[any] # 22 General utilities library [[utilities]](./#utilities) ## 22.7 Storage for any type [any] ### [22.7.1](#general) General [[any.general]](any.general) [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7020) Subclause [any] describes components that C++ programs may use to perform operations on objects of a discriminated type[.](#general-1.sentence-1) [2](#general-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7023) [*Note [1](#general-note-1)*: The discriminated type can contain values of different types but does not attempt conversion between them, i.e., 5 is held strictly as an int and is not implicitly convertible either to "5" or to 5.0[.](#general-2.sentence-1) This indifference to interpretation but awareness of type effectively allows safe, generic containers of single values, with no scope for surprises from ambiguous conversions[.](#general-2.sentence-2) — *end note*] ### [22.7.2](#synop) Header synopsis [[any.synop]](any.synop) namespace std {// [[any.bad.any.cast]](#bad.any.cast "22.7.3 Class bad_­any_­cast"), class bad_any_castclass bad_any_cast; // [[any.class]](#class "22.7.4 Class any"), class anyclass any; // [[any.nonmembers]](#nonmembers "22.7.5 Non-member functions"), non-member functionsvoid swap(any& x, any& y) noexcept; template any make_any(Args&&... args); template any make_any(initializer_list il, Args&&... args); template T any_cast(const any& operand); template T any_cast(any& operand); template T any_cast(any&& operand); templateconst T* any_cast(const any* operand) noexcept; template T* any_cast(any* operand) noexcept;} ### [22.7.3](#bad.any.cast) Class bad_any_cast [[any.bad.any.cast]](any.bad.any.cast) [🔗](#lib:bad_any_cast) namespace std {class bad_any_cast : public bad_cast {public:// see [[exception]](exception "17.9.3 Class exception") for the specification of the special member functionsconst char* what() const noexcept override; };} [1](#bad.any.cast-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7077) Objects of type bad_any_cast are thrown by a failed[any_cast](#lib:any_cast "22.7.5 Non-member functions [any.nonmembers]")[.](#bad.any.cast-1.sentence-1) [🔗](#lib:what,bad_any_cast) `const char* what() const noexcept override; ` [2](#bad.any.cast-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7087) *Returns*: An implementation-defined ntbs[.](#bad.any.cast-2.sentence-1) ### [22.7.4](#class) Class any [[any.class]](any.class) #### [22.7.4.1](#class.general) General [[any.class.general]](any.class.general) namespace std {class any {public:// [[any.cons]](#cons "22.7.4.2 Construction and destruction"), construction and destructionconstexpr any() noexcept; any(const any& other); any(any&& other) noexcept; template any(T&& value); templateexplicit any(in_place_type_t, Args&&...); templateexplicit any(in_place_type_t, initializer_list, Args&&...); ~any(); // [[any.assign]](#assign "22.7.4.3 Assignment"), assignments any& operator=(const any& rhs); any& operator=(any&& rhs) noexcept; template any& operator=(T&& rhs); // [[any.modifiers]](#modifiers "22.7.4.4 Modifiers"), modifierstemplate decay_t& emplace(Args&&...); template decay_t& emplace(initializer_list, Args&&...); void reset() noexcept; void swap(any& rhs) noexcept; // [[any.observers]](#observers "22.7.4.5 Observers"), observersbool has_value() const noexcept; const type_info& type() const noexcept; };} [1](#class.general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7138) An object of class any stores an instance of any type that meets the constructor requirements or it has no value, and this is referred to as the [*state*](#def:state "22.7.4.1 General [any.class.general]") of the class any object[.](#class.general-1.sentence-1) The stored instance is called the [*contained value*](#def:contained_value,any "22.7.4.1 General [any.class.general]")[.](#class.general-1.sentence-2) Two states are equivalent if either they both have no value, or they both have a value and the contained values are equivalent[.](#class.general-1.sentence-3) [2](#class.general-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7144) The non-member any_cast functions provide type-safe access to the contained value[.](#class.general-2.sentence-1) [3](#class.general-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7147) Implementations should avoid the use of dynamically allocated memory for a small contained value[.](#class.general-3.sentence-1) However, any such small-object optimization shall only be applied to types T for whichis_nothrow_move_constructible_v is true[.](#class.general-3.sentence-2) [*Example [1](#class.general-example-1)*: A contained value of type int could be stored in an internal buffer, not in separately-allocated memory[.](#class.general-3.sentence-3) — *end example*] #### [22.7.4.2](#cons) Construction and destruction [[any.cons]](any.cons) [🔗](#lib:any,constructor) `constexpr any() noexcept; ` [1](#cons-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7164) *Postconditions*: has_value() is false[.](#cons-1.sentence-1) [🔗](#lib:any,constructor_) `any(const any& other); ` [2](#cons-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7175) *Effects*: If other.has_value() is false, constructs an object that has no value[.](#cons-2.sentence-1) Otherwise, equivalent to any(in_place_type, any_cast(other)) where T is the type of the contained value[.](#cons-2.sentence-2) [3](#cons-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7181) *Throws*: Any exceptions arising from calling the selected constructor for the contained value[.](#cons-3.sentence-1) [🔗](#lib:any,constructor__) `any(any&& other) noexcept; ` [4](#cons-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7192) *Effects*: If other.has_value() is false, constructs an object that has no value[.](#cons-4.sentence-1) Otherwise, constructs an object of type any that contains either the contained value of other, or contains an object of the same type constructed from the contained value of other considering that contained value as an rvalue[.](#cons-4.sentence-2) [🔗](#lib:any,constructor___) `template any(T&& value); ` [5](#cons-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7208) Let VT be decay_t[.](#cons-5.sentence-1) [6](#cons-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7211) *Constraints*: VT is not the same type as any,VT is not a specialization of in_place_type_t, and is_copy_constructible_v is true[.](#cons-6.sentence-1) [7](#cons-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7217) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#cons-7.sentence-1) [8](#cons-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7221) *Effects*: Constructs an object of type any that contains an object of type VT direct-initialized with std​::​forward(value)[.](#cons-8.sentence-1) [9](#cons-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7225) *Throws*: Any exception thrown by the selected constructor of VT[.](#cons-9.sentence-1) [🔗](#lib:any,constructor____) `template explicit any(in_place_type_t, Args&&... args); ` [10](#cons-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7237) Let VT be decay_t[.](#cons-10.sentence-1) [11](#cons-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7240) *Constraints*: is_copy_constructible_v is true andis_constructible_v is true[.](#cons-11.sentence-1) [12](#cons-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7245) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#cons-12.sentence-1) [13](#cons-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7249) *Effects*: Direct-non-list-initializes the contained value of type VT with std​::​forward(args)...[.](#cons-13.sentence-1) [14](#cons-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7254) *Postconditions*: *this contains a value of type VT[.](#cons-14.sentence-1) [15](#cons-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7258) *Throws*: Any exception thrown by the selected constructor of VT[.](#cons-15.sentence-1) [🔗](#lib:any,constructor_____) `template explicit any(in_place_type_t, initializer_list il, Args&&... args); ` [16](#cons-16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7270) Let VT be decay_t[.](#cons-16.sentence-1) [17](#cons-17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7273) *Constraints*: is_copy_constructible_v is true andis_constructible_v&, Args...> is true[.](#cons-17.sentence-1) [18](#cons-18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7278) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#cons-18.sentence-1) [19](#cons-19) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7282) *Effects*: Direct-non-list-initializes the contained value of type VT with il, std​::​forward(​args)...[.](#cons-19.sentence-1) [20](#cons-20) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7287) *Postconditions*: *this contains a value[.](#cons-20.sentence-1) [21](#cons-21) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7291) *Throws*: Any exception thrown by the selected constructor of VT[.](#cons-21.sentence-1) [🔗](#lib:any,destructor) `~any(); ` [22](#cons-22) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7302) *Effects*: As if by reset()[.](#cons-22.sentence-1) #### [22.7.4.3](#assign) Assignment [[any.assign]](any.assign) [🔗](#lib:operator=,any) `any& operator=(const any& rhs); ` [1](#assign-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7315) *Effects*: As if by any(rhs).swap(*this)[.](#assign-1.sentence-1) No effects if an exception is thrown[.](#assign-1.sentence-2) [2](#assign-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7320) *Returns*: *this[.](#assign-2.sentence-1) [3](#assign-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7324) *Throws*: Any exceptions arising from the copy constructor for the contained value[.](#assign-3.sentence-1) [🔗](#lib:operator=,any_) `any& operator=(any&& rhs) noexcept; ` [4](#assign-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7335) *Effects*: As if by any(std​::​move(rhs)).swap(*this)[.](#assign-4.sentence-1) [5](#assign-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7339) *Postconditions*: The state of *this is equivalent to the original state of rhs[.](#assign-5.sentence-1) [6](#assign-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7343) *Returns*: *this[.](#assign-6.sentence-1) [🔗](#lib:operator=,any__) `template any& operator=(T&& rhs); ` [7](#assign-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7355) Let VT be decay_t[.](#assign-7.sentence-1) [8](#assign-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7358) *Constraints*: VT is not the same type as any andis_copy_constructible_v is true[.](#assign-8.sentence-1) [9](#assign-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7363) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#assign-9.sentence-1) [10](#assign-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7367) *Effects*: Constructs an object tmp of type any that contains an object of type VT direct-initialized with std​::​forward(rhs), and tmp.swap(*this)[.](#assign-10.sentence-1) No effects if an exception is thrown[.](#assign-10.sentence-2) [11](#assign-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7372) *Returns*: *this[.](#assign-11.sentence-1) [12](#assign-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7376) *Throws*: Any exception thrown by the selected constructor of VT[.](#assign-12.sentence-1) #### [22.7.4.4](#modifiers) Modifiers [[any.modifiers]](any.modifiers) [🔗](#lib:emplace,any) `template decay_t& emplace(Args&&... args); ` [1](#modifiers-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7390) Let VT be decay_t[.](#modifiers-1.sentence-1) [2](#modifiers-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7393) *Constraints*: is_copy_constructible_v is true andis_constructible_v is true[.](#modifiers-2.sentence-1) [3](#modifiers-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7398) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#modifiers-3.sentence-1) [4](#modifiers-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7402) *Effects*: Calls reset()[.](#modifiers-4.sentence-1) Then direct-non-list-initializes the contained value of type VT with std​::​forward(args)...[.](#modifiers-4.sentence-2) [5](#modifiers-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7408) *Postconditions*: *this contains a value[.](#modifiers-5.sentence-1) [6](#modifiers-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7412) *Returns*: A reference to the new contained value[.](#modifiers-6.sentence-1) [7](#modifiers-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7416) *Throws*: Any exception thrown by the selected constructor of VT[.](#modifiers-7.sentence-1) [8](#modifiers-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7420) *Remarks*: If an exception is thrown during the call to VT's constructor,*this does not contain a value, and any previously contained value has been destroyed[.](#modifiers-8.sentence-1) [🔗](#lib:emplace,any_) `template decay_t& emplace(initializer_list il, Args&&... args); ` [9](#modifiers-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7434) Let VT be decay_t[.](#modifiers-9.sentence-1) [10](#modifiers-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7437) *Constraints*: is_copy_constructible_v is true andis_constructible_v&, Args...> is true[.](#modifiers-10.sentence-1) [11](#modifiers-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7442) *Preconditions*: VT meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#modifiers-11.sentence-1) [12](#modifiers-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7446) *Effects*: Calls reset()[.](#modifiers-12.sentence-1) Then direct-non-list-initializes the contained value of type VT with il, std​::​forward(args)...[.](#modifiers-12.sentence-2) [13](#modifiers-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7451) *Postconditions*: *this contains a value[.](#modifiers-13.sentence-1) [14](#modifiers-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7455) *Returns*: A reference to the new contained value[.](#modifiers-14.sentence-1) [15](#modifiers-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7459) *Throws*: Any exception thrown by the selected constructor of VT[.](#modifiers-15.sentence-1) [16](#modifiers-16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7463) *Remarks*: If an exception is thrown during the call to VT's constructor,*this does not contain a value, and any previously contained value has been destroyed[.](#modifiers-16.sentence-1) [🔗](#lib:reset,any) `void reset() noexcept; ` [17](#modifiers-17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7476) *Effects*: If has_value() is true, destroys the contained value[.](#modifiers-17.sentence-1) [18](#modifiers-18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7480) *Postconditions*: has_value() is false[.](#modifiers-18.sentence-1) [🔗](#lib:swap,any) `void swap(any& rhs) noexcept; ` [19](#modifiers-19) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7492) *Effects*: Exchanges the states of *this and rhs[.](#modifiers-19.sentence-1) #### [22.7.4.5](#observers) Observers [[any.observers]](any.observers) [🔗](#lib:has_value,any) `bool has_value() const noexcept; ` [1](#observers-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7505) *Returns*: true if *this contains an object, otherwise false[.](#observers-1.sentence-1) [🔗](#lib:type,any) `const type_info& type() const noexcept; ` [2](#observers-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7516) *Returns*: typeid(T) if *this has a contained value of type T, otherwise typeid(void)[.](#observers-2.sentence-1) [3](#observers-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7521) [*Note [1](#observers-note-1)*: Useful for querying against types known either at compile time or only at runtime[.](#observers-3.sentence-1) — *end note*] ### [22.7.5](#nonmembers) Non-member functions [[any.nonmembers]](any.nonmembers) [🔗](#lib:swap,any_) `void swap(any& x, any& y) noexcept; ` [1](#nonmembers-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7535) *Effects*: Equivalent to x.swap(y)[.](#nonmembers-1.sentence-1) [🔗](#lib:make_any) `template any make_any(Args&&... args); ` [2](#nonmembers-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7547) *Effects*: Equivalent to: return any(in_place_type, std​::​forward(args)...); [🔗](#lib:make_any_) `template any make_any(initializer_list il, Args&&... args); ` [3](#nonmembers-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7559) *Effects*: Equivalent to: return any(in_place_type, il, std​::​forward(args)...); [🔗](#lib:any_cast) `template T any_cast(const any& operand); template T any_cast(any& operand); template T any_cast(any&& operand); ` [4](#nonmembers-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7575) Let U be the type remove_cvref_t[.](#nonmembers-4.sentence-1) [5](#nonmembers-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7578) *Mandates*: For the first overload, is_constructible_v is true[.](#nonmembers-5.sentence-1) For the second overload, is_constructible_v is true[.](#nonmembers-5.sentence-2) For the third overload, is_constructible_v is true[.](#nonmembers-5.sentence-3) [6](#nonmembers-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7584) *Returns*: For the first and second overload, static_cast(*any_cast(&operand))[.](#nonmembers-6.sentence-1) For the third overload, static_cast(std​::​move(*any_cast(&operand)))[.](#nonmembers-6.sentence-2) [7](#nonmembers-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7589) *Throws*: bad_any_cast if operand.type() != typeid(remove_reference_t)[.](#nonmembers-7.sentence-1) [8](#nonmembers-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7593) [*Example [1](#nonmembers-example-1)*: any x(5); // x holds int assert(any_cast(x) == 5); // cast to value any_cast(x) = 10; // cast to reference assert(any_cast(x) == 10); x = "Meow"; // x holds const char* assert(strcmp(any_cast(x), "Meow") == 0); any_cast(x) = "Harry"; assert(strcmp(any_cast(x), "Harry") == 0); x = string("Meow"); // x holds string string s, s2("Jane"); s = move(any_cast(x)); // move from any assert(s == "Meow"); any_cast(x) = move(s2); // move to any assert(any_cast(x) == "Jane"); string cat("Meow");const any y(cat); // const y holds string assert(any_cast(y) == cat); any_cast(y); // error: cannot any_cast away const — *end example*] [🔗](#lib:any_cast_) `template const T* any_cast(const any* operand) noexcept; template T* any_cast(any* operand) noexcept; ` [9](#nonmembers-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7631) *Mandates*: is_void_v is false[.](#nonmembers-9.sentence-1) [10](#nonmembers-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7635) *Returns*: If operand != nullptr && operand->type() == typeid(T) is true, a pointer to the object contained by operand; otherwise, nullptr[.](#nonmembers-10.sentence-1) [11](#nonmembers-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7641) [*Example [2](#nonmembers-example-2)*: bool is_string(const any& operand) {return any_cast(&operand) != nullptr;} — *end example*]