Files
cppdraft_translate/cppdraft/any/nonmembers.md
2025-10-25 03:02:53 +03:00

129 lines
4.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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<class T, class... Args>
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<T>, std::forward<Args>(args)...);
[🔗](#lib:make_any_)
`template<class T, class U, class... Args>
any make_any(initializer_list<U> il, Args&&... args);
`
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7559)
*Effects*: Equivalent to: return any(in_place_type<T>, il, std::forward<Args>(args)...);
[🔗](#lib:any_cast)
`template<class T>
T any_cast(const any& operand);
template<class T>
T any_cast(any& operand);
template<class T>
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<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<T, const U&> is true[.](#5.sentence-1)
For the second overload, is_constructible_v<T, U&> is true[.](#5.sentence-2)
For the third overload, is_constructible_v<T, U> 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<T>(*any_cast<U>(&operand))[.](#6.sentence-1)
For the third overload, static_cast<T>(std::move(*any_cast<U>(&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<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<int>(x) == 5); // cast to value any_cast<int&>(x) = 10; // cast to reference assert(any_cast<int>(x) == 10);
x = "Meow"; // x holds const char* assert(strcmp(any_cast<const char*>(x), "Meow") == 0);
any_cast<const char*&>(x) = "Harry";
assert(strcmp(any_cast<const char*>(x), "Harry") == 0);
x = string("Meow"); // x holds string string s, s2("Jane");
s = move(any_cast<string&>(x)); // move from any assert(s == "Meow");
any_cast<string&>(x) = move(s2); // move to any assert(any_cast<const string&>(x) == "Jane");
string cat("Meow");const any y(cat); // const y holds string assert(any_cast<const string&>(y) == cat);
any_cast<string&>(y); // error: cannot any_cast away const — *end example*]
[🔗](#lib:any_cast_)
`template<class T>
const T* any_cast(const any* operand) noexcept;
template<class T>
T* any_cast(any* operand) noexcept;
`
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7631)
*Mandates*: is_void_v<T> 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<string>(&operand) != nullptr;} — *end example*]