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