forked from cplusplus/fundamentals-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.html
285 lines (224 loc) · 12.6 KB
/
any.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<cxx-clause id="any">
<h1>Class <code>any</code></h1>
<p>
This section describes components that C++ programs may use to perform operations on objects of a discriminated type.
</p>
<p>
<cxx-note>The discriminated type may contain values of different types but does not attempt conversion between them,
i.e. <code>5</code> is held strictly as an <code>int</code> and is not implicitly convertible either to <code>"5"</code> or to <code>5.0</code>.
This indifference to interpretation but awareness of type effectively allows safe, generic containers of single values, with no scope for surprises from ambiguous conversions.</cxx-note>
</p>
<cxx-section id="any.synop">
<h1>Header <experimental/any> synopsis</h1>
<pre><code>namespace std {
namespace experimental {
inline namespace fundamentals_v2 {
class bad_any_cast : public bad_cast
{
public:
virtual const char* what() const noexcept;
};
class any
{
public:
<cxx-ref insynopsis="" to="any.cons"></cxx-ref>
any() noexcept;
any(const any& other);
any(any&& other) noexcept;
template <class ValueType>
any(ValueType&& value);
~any();
<cxx-ref insynopsis="" to="any.assign"></cxx-ref>
any& operator=(const any& rhs);
any& operator=(any&& rhs) noexcept;
template <class ValueType>
any& operator=(ValueType&& rhs);
<cxx-ref insynopsis="" to="any.modifiers"></cxx-ref>
void clear() noexcept;
void swap(any& rhs) noexcept;
<cxx-ref insynopsis="" to="any.observers"></cxx-ref>
bool empty() const noexcept;
const type_info& type() const noexcept;
};
<cxx-ref insynopsis="" to="any.nonmembers"></cxx-ref>
void swap(any& x, any& y) noexcept;
template<class ValueType>
ValueType any_cast(const any& operand);
template<class ValueType>
ValueType any_cast(any& operand);
template<class ValueType>
ValueType any_cast(any&& operand);
template<class ValueType>
const ValueType* any_cast(const any* operand) noexcept;
template<class ValueType>
ValueType* any_cast(any* operand) noexcept;
} // namespace fundamentals_v2
} // namespace experimental
} // namespace std</code></pre>
</cxx-section>
<cxx-section id="any.bad_any_cast">
<h1>Class <code>bad_any_cast</code></h1>
<p>
Objects of type <code>bad_any_cast</code> are thrown by a failed <code> any_cast</code>.
</p>
</cxx-section>
<cxx-section id="any.class">
<h1>Class <code>any</code></h1>
<p>
An object of class <code>any</code> stores an instance of any type that satisfies the constructor requirements or is empty,
and this is referred to as the <dfn>state</dfn> of the class <code>any</code> object.
The stored instance is called the <dfn>contained object</dfn>.
Two states are equivalent if they are either both empty or if both are not empty and if the contained objects are equivalent.
</p>
<p>
The non-member <code>any_cast</code> functions provide type-safe access to the contained object.
</p>
<p>
Implementations should avoid the use of dynamically allocated memory for a small contained object.
<cxx-example class="inline">where the object constructed is holding only an int.</cxx-example>
Such small-object optimization shall only be applied to types <code>T</code> for which
<code>is_nothrow_move_constructible_v<T></code> is true.
</p>
<cxx-section id="any.cons">
<h1><code>any</code> construct/destruct</h1>
<cxx-function>
<cxx-signature>any() noexcept;</cxx-signature>
<cxx-postconditions><code>this->empty()</code>.</cxx-postconditions>
</cxx-function>
<cxx-function>
<cxx-signature>any(const any& other);</cxx-signature>
<cxx-effects>Constructs an object of type <code>any</code> with an equivalent state as <code>other</code>.</cxx-effects>
<cxx-throws>Any exceptions arising from calling the selected constructor of the contained object.</cxx-throws>
</cxx-function>
<cxx-function>
<cxx-signature>any(any&& other) noexcept;</cxx-signature>
<cxx-effects>Constructs an object of type <code>any</code> with a state equivalent to the original state of <code>other</code>.</cxx-effects>
<cxx-postconditions><code>other</code> is left in a valid but otherwise unspecified state.</cxx-postconditions>
</cxx-function>
<cxx-function>
<cxx-signature>template<class ValueType>
any(ValueType&& value);</cxx-signature>
<p>Let <code>T</code> be equal to <code>decay_t<ValueType></code>.</p>
<cxx-requires><code>T</code> shall satisfy the <code>CopyConstructible</code> requirements, except for the requirements for <code>MoveConstructible</code>.
If <code>is_copy_constructible_v<T></code> is false, the program is ill-formed.</cxx-requires>
<cxx-effects>If <code>is_constructible_v<T, ValueType&&></code> is true, constructs an object of type <code>any</code> that contains an object of type <code>T</code> direct-initialized with <code>std::forward<ValueType>(value)</code>. Otherwise, constructs an object of type <code>any</code> that contains an object of type <code>T</code> direct-initialized with <code>value</code>.</cxx-effects>
<cxx-remarks>This constructor shall not participate in overload resolution if <code>decay_t<ValueType></code> is the same type as <code>any</code>.</cxx-remarks>
<cxx-throws>Any exception thrown by the selected constructor of <code>T</code>.</cxx-throws>
</cxx-function>
<cxx-function>
<cxx-signature>~any();</cxx-signature>
<cxx-effects><code>clear()</code>.</cxx-effects>
</cxx-function>
</cxx-section>
<cxx-section id="any.assign">
<h1><code>any</code> assignments</h1>
<cxx-function>
<cxx-signature>any& operator=(const any& rhs);</cxx-signature>
<cxx-effects><code>any(rhs).swap(*this)</code>.
No effects if an exception is thrown.</cxx-effects>
<cxx-returns><code>*this</code>.</cxx-returns>
<cxx-throws>Any exceptions arising from the copy constructor of the contained object.</cxx-throws>
</cxx-function>
<cxx-function>
<cxx-signature>any& operator=(any&& rhs) noexcept;</cxx-signature>
<cxx-effects><code>any(std::move(rhs)).swap(*this)</code>.</cxx-effects>
<cxx-returns><code>*this</code>.</cxx-returns>
<cxx-postconditions>The state of <code>*this</code> is equivalent to the original state of <code>rhs</code>
and <code>rhs</code> is left in a valid but otherwise unspecified state.</cxx-postconditions>
</cxx-function>
<cxx-function>
<cxx-signature>template<class ValueType>
any& operator=(ValueType&& rhs);</cxx-signature>
<p>Let <code>T</code> be equal to <code>decay_t<ValueType></code>.</p>
<cxx-requires><code>T</code> shall satisfy the <code>CopyConstructible</code> requirements.
If <code>is_copy_constructible_v<T></code> is false, the program is ill-formed.</cxx-requires>
<cxx-effects>Constructs an object <code>tmp</code> of type <code>any</code> that contains an object of type <code>T</code> direct-initialized with <code>std::forward<ValueType>(rhs)</code>, and <code>tmp.swap(*this)</code>.
No effects if an exception is thrown.</cxx-effects>
<cxx-returns><code>*this</code>.</cxx-returns>
<cxx-remarks> This operator shall not participate in overload resolution if <code>decay_t<ValueType></code> is the same type as <code>any</code>.</cxx-remarks>
<cxx-throws>Any exception thrown by the selected constructor of <code>T</code>.</cxx-throws>
</cxx-function>
</cxx-section>
<cxx-section id="any.modifiers">
<h1><code>any</code> modifiers</h1>
<cxx-function>
<cxx-signature>void clear() noexcept;</cxx-signature>
<cxx-effects>If not empty, destroys the contained object.</cxx-effects>
<cxx-postconditions><code>empty() == true</code>.</cxx-postconditions>
</cxx-function>
<cxx-function>
<cxx-signature>void swap(any& rhs) noexcept;</cxx-signature>
<cxx-effects>Exchange the states of <code>*this</code> and <code> rhs</code>.</cxx-effects>
</cxx-function>
</cxx-section>
<cxx-section id="any.observers">
<h1><code>any</code> observers</h1>
<cxx-function>
<cxx-signature>bool empty() const noexcept;</cxx-signature>
<cxx-returns><code>true</code> if <code>*this</code> has no contained object, otherwise <code> false</code>.</cxx-returns>
</cxx-function>
<cxx-function>
<cxx-signature>const type_info& type() const noexcept;</cxx-signature>
<cxx-returns>If <code>*this</code> has a contained object of type T, <code>typeid(T)</code>;
otherwise <code>typeid(void)</code>.</cxx-returns>
<p><cxx-note>Useful for querying against types known either at compile time or only at runtime.</cxx-note></p>
</cxx-function>
</cxx-section>
</cxx-section>
<cxx-section id="any.nonmembers">
<h1><a name="Non-member">Non-member</a> functions</h1>
<cxx-function>
<cxx-signature>void swap(any& x, any& y) noexcept;</cxx-signature>
<cxx-effects><code>x.swap(y)</code>.</cxx-effects>
</cxx-function>
<cxx-function>
<cxx-signature>template<class ValueType>
ValueType any_cast(const any& operand);</cxx-signature>
<cxx-signature>template<class ValueType>
ValueType any_cast(any& operand);</cxx-signature>
<cxx-signature>template<class ValueType>
ValueType any_cast(any&& operand);</cxx-signature>
<cxx-requires><code>is_reference_v<ValueType></code> is true or <code>is_copy_constructible_v<ValueType></code> is true.
Otherwise the program is ill-formed.</cxx-requires>
<cxx-returns>For the first form, <code>*any_cast<add_const_t<remove_reference_t<ValueType>>>(&operand)</code>.
For the second form, <code>*any_cast<remove_reference_t<ValueType>>(&operand)</code>.
For the third form, if <code>is_move_constructible_v<ValueType></code> is <code>true</code> and <code>is_lvalue_reference_v<ValueType></code> is <code>false</code>, <code>std::move(*any_cast<remove_reference_t<ValueType>>(&operand))</code>, otherwise, <code>*any_cast<remove_reference_t<ValueType>>(&operand)</code>.</cxx-returns>
<cxx-throws><code>bad_any_cast</code> if <code>operand.type() != typeid(remove_reference_t<ValueType>)</code>.</cxx-throws>
<cxx-example>
<pre><code>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</code></pre>
</cxx-example>
</cxx-function>
<cxx-function>
<cxx-signature>template<class ValueType>
const ValueType* any_cast(const any* operand) noexcept;</cxx-signature>
<cxx-signature>template<class ValueType>
ValueType* any_cast(any* operand) noexcept;</cxx-signature>
<cxx-returns>If <code>operand != nullptr && <w-br></w-br>operand->type() == typeid(ValueType)</code>,
a pointer to the object contained by <code>operand</code>,
otherwise <code>nullptr</code>.</cxx-returns>
<cxx-example>
<pre><code>bool is_string(const any& operand) {
return any_cast<string>(&operand) != nullptr;
}</code></pre>
</cxx-example>
</cxx-function>
</cxx-section>
</cxx-clause>