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

2.6 KiB

[any.class.general]

22 General utilities library [utilities]

22.7 Storage for any type [any]

22.7.4 Class any [any.class]

22.7.4.1 General [any.class.general]

namespace std {class any {public:// [any.cons], construction and destructionconstexpr any() noexcept;

any(const any& other); any(any&& other) noexcept; template any(T&& value); template<class T, class... Args>explicit any(in_place_type_t, Args&&...); template<class T, class U, class... Args>explicit any(in_place_type_t, initializer_list, Args&&...); ~any(); // [any.assign], assignments any& operator=(const any& rhs); any& operator=(any&& rhs) noexcept; template any& operator=(T&& rhs); // [any.modifiers], modifierstemplate<class T, class... Args> decay_t& emplace(Args&&...); template<class T, class U, class... Args> decay_t& emplace(initializer_list, Args&&...); void reset() noexcept; void swap(any& rhs) noexcept; // [any.observers], observersbool has_value() const noexcept; const type_info& type() const noexcept; };}

1

#

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 of the class any object.

The stored instance is called the contained value.

Two states are equivalent if either they both have no value, or they both have a value and the contained values are equivalent.

2

#

The non-member any_cast functions provide type-safe access to the contained value.

3

#

Implementations should avoid the use of dynamically allocated memory for a small contained value.

However, any such small-object optimization shall only be applied to types T for whichis_nothrow_move_constructible_v is true.

[Example 1:

A contained value of type int could be stored in an internal buffer, not in separately-allocated memory.

— end example]