[any.nonmembers] # 22 General utilities library [[utilities]](./#utilities) ## 22.7 Storage for any type [[any]](any#nonmembers) ### 22.7.5 Non-member functions [any.nonmembers] [🔗](#lib:swap,any) `void swap(any& x, any& y) noexcept; ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7535) *Effects*: Equivalent to x.swap(y)[.](#1.sentence-1) [🔗](#lib:make_any) `template any make_any(Args&&... args); ` [2](#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](#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](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7575) Let U be the type remove_cvref_t[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7578) *Mandates*: For the first overload, is_constructible_v is true[.](#5.sentence-1) For the second overload, is_constructible_v is true[.](#5.sentence-2) For the third overload, is_constructible_v is true[.](#5.sentence-3) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7584) *Returns*: For the first and second overload, static_cast(*any_cast(&operand))[.](#6.sentence-1) For the third overload, static_cast(std​::​move(*any_cast(&operand)))[.](#6.sentence-2) [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7589) *Throws*: bad_any_cast if operand.type() != typeid(remove_reference_t)[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7593) [*Example [1](#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](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7631) *Mandates*: is_void_v is false[.](#9.sentence-1) [10](#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[.](#10.sentence-1) [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7641) [*Example [2](#example-2)*: bool is_string(const any& operand) {return any_cast(&operand) != nullptr;} — *end example*]