673 lines
25 KiB
Markdown
673 lines
25 KiB
Markdown
[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 <any> 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<class T, class... Args> any make_any(Args&&... args); template<class T, class U, class... Args> any make_any(initializer_list<U> il, Args&&... args); 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); template<class T>const T* any_cast(const any* operand) noexcept; template<class T> 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<class T> any(T&& value); template<class T, class... Args>explicit any(in_place_type_t<T>, Args&&...); template<class T, class U, class... Args>explicit any(in_place_type_t<T>, initializer_list<U>, Args&&...); ~any(); // [[any.assign]](#assign "22.7.4.3 Assignment"), assignments any& operator=(const any& rhs);
|
||
any& operator=(any&& rhs) noexcept; template<class T> any& operator=(T&& rhs); // [[any.modifiers]](#modifiers "22.7.4.4 Modifiers"), modifierstemplate<class T, class... Args> decay_t<T>& emplace(Args&&...); template<class T, class U, class... Args> decay_t<T>& emplace(initializer_list<U>, 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<T> 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<T>, any_cast<const T&>(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<class T>
|
||
any(T&& value);
|
||
`
|
||
|
||
[5](#cons-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7208)
|
||
|
||
Let VT be decay_t<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<VT> 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<T>(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<class T, class... Args>
|
||
explicit any(in_place_type_t<T>, Args&&... args);
|
||
`
|
||
|
||
[10](#cons-10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7237)
|
||
|
||
Let VT be decay_t<T>[.](#cons-10.sentence-1)
|
||
|
||
[11](#cons-11)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7240)
|
||
|
||
*Constraints*: is_copy_constructible_v<VT> is true andis_constructible_v<VT, Args...> 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>(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<class T, class U, class... Args>
|
||
explicit any(in_place_type_t<T>, initializer_list<U> il, Args&&... args);
|
||
`
|
||
|
||
[16](#cons-16)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7270)
|
||
|
||
Let VT be decay_t<T>[.](#cons-16.sentence-1)
|
||
|
||
[17](#cons-17)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7273)
|
||
|
||
*Constraints*: is_copy_constructible_v<VT> is true andis_constructible_v<VT, initializer_list<U>&, 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>(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<class T>
|
||
any& operator=(T&& rhs);
|
||
`
|
||
|
||
[7](#assign-7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7355)
|
||
|
||
Let VT be decay_t<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<VT> 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<T>(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<class T, class... Args>
|
||
decay_t<T>& emplace(Args&&... args);
|
||
`
|
||
|
||
[1](#modifiers-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7390)
|
||
|
||
Let VT be decay_t<T>[.](#modifiers-1.sentence-1)
|
||
|
||
[2](#modifiers-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7393)
|
||
|
||
*Constraints*: is_copy_constructible_v<VT> is true andis_constructible_v<VT, Args...> 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>(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<class T, class U, class... Args>
|
||
decay_t<T>& emplace(initializer_list<U> il, Args&&... args);
|
||
`
|
||
|
||
[9](#modifiers-9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7434)
|
||
|
||
Let VT be decay_t<T>[.](#modifiers-9.sentence-1)
|
||
|
||
[10](#modifiers-10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7437)
|
||
|
||
*Constraints*: is_copy_constructible_v<VT> is true andis_constructible_v<VT, initializer_list<U>&, 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>(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<class T, class... Args>
|
||
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<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](#nonmembers-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](#nonmembers-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7575)
|
||
|
||
Let U be the type remove_cvref_t<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<T, const U&> is true[.](#nonmembers-5.sentence-1)
|
||
|
||
For the second overload, is_constructible_v<T, U&> is true[.](#nonmembers-5.sentence-2)
|
||
|
||
For the third overload, is_constructible_v<T, U> 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<T>(*any_cast<U>(&operand))[.](#nonmembers-6.sentence-1)
|
||
|
||
For the third overload, static_cast<T>(std::move(*any_cast<U>(&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<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<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](#nonmembers-9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7631)
|
||
|
||
*Mandates*: is_void_v<T> 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<string>(&operand) != nullptr;} â *end example*]
|