%!TEX root = std.tex \rSec0[utilities]{General utilities library} \rSec1[utilities.general]{General} \pnum This Clause describes utilities that are generally useful in \Cpp programs; some of these utilities are used by other elements of the \Cpp standard library. These utilities are summarized in Table~\ref{tab:util.lib.summary}. \begin{libsumtab}{General utilities library summary}{tab:util.lib.summary} \ref{utility} & Utility components & \tcode{} \\ \rowsep \ref{intseq} & Compile-time integer sequences & \tcode{} \\ \rowsep \ref{pairs} & Pairs & \tcode{} \\ \rowsep \ref{tuple} & Tuples & \tcode{} \\ \rowsep \ref{optional} & Optional objects & \tcode{} \\ \rowsep \ref{variant} & Variants & \tcode{} \\ \rowsep \ref{any} & Storage for any type & \tcode{} \\ \rowsep \ref{bitset} & Fixed-size sequences of bits & \tcode{} \\ \rowsep \ref{memory} & Memory & \tcode{} \\ & & \tcode{} \\ \rowsep \ref{smartptr} & Smart pointers & \tcode{} \\ \rowsep \ref{mem.res} & Memory resources & \tcode{} \\ \rowsep \ref{allocator.adaptor} & Scoped allocators & \tcode{} \\ \rowsep \ref{function.objects} & Function objects & \tcode{} \\ \rowsep \ref{meta} & Type traits & \tcode{} \\ \rowsep \ref{ratio} & Compile-time rational arithmetic & \tcode{} \\ \rowsep \ref{time} & Time utilities & \tcode{} \\ & & \tcode{} \\ \rowsep \ref{type.index} & Type indexes & \tcode{} \\ \rowsep \ref{execpol} & Execution policies & \tcode{} \\ \end{libsumtab} \rSec1[utility]{Utility components} \pnum This subclause contains some basic function and class templates that are used throughout the rest of the library. \indextext{\idxhdr{utility}}% \indexlibrary{\idxhdr{utility}}% \indexlibrary{\idxcode{rel_ops}}% \rSec2[utility.syn]{Header \tcode{} synopsis} \begin{codeblock} #include // see \ref{initializer_list.syn} namespace std { // \ref{operators}, operators namespace rel_ops { template bool operator!=(const T&, const T&); template bool operator> (const T&, const T&); template bool operator<=(const T&, const T&); template bool operator>=(const T&, const T&); } // \ref{utility.swap}, swap template void swap(T& a, T& b) noexcept(@\seebelow@); template void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v); // \ref{utility.exchange}, exchange template T exchange(T& obj, U&& new_val); // \ref{forward}, forward/move template constexpr T&& forward(remove_reference_t& t) noexcept; template constexpr T&& forward(remove_reference_t&& t) noexcept; template constexpr remove_reference_t&& move(T&&) noexcept; template constexpr conditional_t< !is_nothrow_move_constructible_v && is_copy_constructible_v, const T&, T&&> move_if_noexcept(T& x) noexcept; // \ref{utility.as_const}, \tcode{as_const} template constexpr add_const_t& as_const(T& t) noexcept; template void as_const(const T&&) = delete; // \ref{declval}, declval template add_rvalue_reference_t declval() noexcept; // as unevaluated operand @% \indexlibrary{\idxcode{index_sequence}}% \indexlibrary{\idxcode{make_index_sequence}}% \indexlibrary{\idxcode{index_sequence_for}}% @ // \ref{intseq}, Compile-time integer sequences template struct integer_sequence; template using index_sequence = integer_sequence; template using make_integer_sequence = integer_sequence; template using make_index_sequence = make_integer_sequence; template using index_sequence_for = make_index_sequence; // \ref{pairs}, class template \tcode{pair} template struct pair; // \ref{pairs.spec}, pair specialized algorithms template constexpr bool operator==(const pair&, const pair&); template constexpr bool operator< (const pair&, const pair&); template constexpr bool operator!=(const pair&, const pair&); template constexpr bool operator> (const pair&, const pair&); template constexpr bool operator>=(const pair&, const pair&); template constexpr bool operator<=(const pair&, const pair&); template void swap(pair& x, pair& y) noexcept(noexcept(x.swap(y))); template constexpr @\seebelow@ make_pair(T1&&, T2&&); // \ref{pair.astuple}, tuple-like access to pair template class tuple_size; template class tuple_element; template struct tuple_size>; template struct tuple_element<0, pair>; template struct tuple_element<1, pair>; template constexpr tuple_element_t>& get(pair&) noexcept; template constexpr tuple_element_t>&& get(pair&&) noexcept; template constexpr const tuple_element_t>& get(const pair&) noexcept; template constexpr const tuple_element_t>&& get(const pair&&) noexcept; template constexpr T1& get(pair& p) noexcept; template constexpr const T1& get(const pair& p) noexcept; template constexpr T1&& get(pair&& p) noexcept; template constexpr const T1&& get(const pair&& p) noexcept; template constexpr T2& get(pair& p) noexcept; template constexpr const T2& get(const pair& p) noexcept; template constexpr T2&& get(pair&& p) noexcept; template constexpr const T2&& get(const pair&& p) noexcept; // \ref{pair.piecewise}, pair piecewise construction struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; inline constexpr piecewise_construct_t piecewise_construct{}; template class tuple; // defined in \tcode{} (\ref{tuple.syn}) // in-place construction struct in_place_t { explicit in_place_t() = default; }; inline constexpr in_place_t in_place{}; template struct in_place_type_t { explicit in_place_type_t() = default; }; template inline constexpr in_place_type_t in_place_type{}; template struct in_place_index_t { explicit in_place_index_t() = default; }; template inline constexpr in_place_index_t in_place_index{}; @ \indexlibrary{\idxcode{chars_format}}% \indexlibrarymember{scientific}{chars_format}% \indexlibrarymember{fixed}{chars_format}% \indexlibrarymember{hex}{chars_format}% \indexlibrarymember{general}{chars_format}% @ // floating-point format for primitive numerical conversion enum class chars_format { scientific = @\unspec@, fixed = @\unspec@, hex = @\unspec@, general = fixed | scientific }; @ \indexlibrary{\idxcode{to_chars_result}}% \indexlibrarymember{ptr}{to_chars_result}% \indexlibrarymember{ec}{to_chars_result} @ // \ref{utility.to.chars}, primitive numerical output conversion struct to_chars_result { char* ptr; error_code ec; }; to_chars_result to_chars(char* first, char* last, @\seebelow@ value, int base = 10); to_chars_result to_chars(char* first, char* last, float value); to_chars_result to_chars(char* first, char* last, double value); to_chars_result to_chars(char* first, char* last, long double value); to_chars_result to_chars(char* first, char* last, float value, chars_format fmt); to_chars_result to_chars(char* first, char* last, double value, chars_format fmt); to_chars_result to_chars(char* first, char* last, long double value, chars_format fmt); to_chars_result to_chars(char* first, char* last, float value, chars_format fmt, int precision); to_chars_result to_chars(char* first, char* last, double value, chars_format fmt, int precision); to_chars_result to_chars(char* first, char* last, long double value, chars_format fmt, int precision); @ \indexlibrary{\idxcode{from_chars_result}}% \indexlibrarymember{ptr}{from_chars_result}% \indexlibrarymember{ec}{from_chars_result} @ // \ref{utility.from.chars}, primitive numerical input conversion struct from_chars_result { const char* ptr; error_code ec; }; from_chars_result from_chars(const char* first, const char* last, @\seebelow@& value, int base = 10); from_chars_result from_chars(const char* first, const char* last, float& value, chars_format fmt = chars_format::general); from_chars_result from_chars(const char* first, const char* last, double& value, chars_format fmt = chars_format::general); from_chars_result from_chars(const char* first, const char* last, long double& value, chars_format fmt = chars_format::general); } \end{codeblock} \pnum The header \tcode{} defines several types and function templates that are described in this Clause. It also defines the template \tcode{pair} and various function templates that operate on \tcode{pair} objects. \pnum The type \tcode{chars_format} is a bitmask type~(\ref{bitmask.types}) with elements \tcode{scientific}, \tcode{fixed}, and \tcode{hex}. \rSec2[operators]{Operators} \pnum To avoid redundant definitions of \tcode{operator!=} out of \tcode{operator==} and operators \tcode{>}, \tcode{<=}, and \tcode{>=} out of \tcode{operator<}, the library provides the following: \indexlibrary{\idxcode{operator"!=}}% \begin{itemdecl} template bool operator!=(const T& x, const T& y); \end{itemdecl} \begin{itemdescr} \pnum \requires Type \tcode{T} is \tcode{EqualityComparable} (Table~\ref{tab:equalitycomparable}). \pnum \returns \tcode{!(x == y)}. \end{itemdescr} \indexlibrary{\idxcode{operator>}}% \begin{itemdecl} template bool operator>(const T& x, const T& y); \end{itemdecl} \begin{itemdescr} \pnum \requires Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{tab:lessthancomparable}). \pnum \returns \tcode{y < x}. \end{itemdescr} \indexlibrary{\idxcode{operator<=}}% \begin{itemdecl} template bool operator<=(const T& x, const T& y); \end{itemdecl} \begin{itemdescr} \pnum \requires Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{tab:lessthancomparable}). \pnum \returns \tcode{!(y < x)}. \end{itemdescr} \indexlibrary{\idxcode{operator>=}}% \begin{itemdecl} template bool operator>=(const T& x, const T& y); \end{itemdecl} \begin{itemdescr} \pnum \requires Type \tcode{T} is \tcode{LessThanComparable} (Table~\ref{tab:lessthancomparable}). \pnum \returns \tcode{!(x < y)}. \end{itemdescr} \pnum In this library, whenever a declaration is provided for an \tcode{operator!=}, \tcode{operator>}, \tcode{operator>=}, or \tcode{operator<=}, and requirements and semantics are not explicitly provided, the requirements and semantics are as specified in this Clause. \rSec2[utility.swap]{\tcode{swap}} \indexlibrary{\idxcode{swap}}% \begin{itemdecl} template void swap(T& a, T& b) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true} and \tcode{is_move_assignable_v} is \tcode{true}. The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_move_constructible_v && is_nothrow_move_assignable_v \end{codeblock} \pnum \requires Type \tcode{T} shall be \tcode{MoveConstructible} (Table~\ref{tab:moveconstructible}) and \tcode{MoveAssignable} (Table~\ref{tab:moveassignable}). \pnum \effects Exchanges values stored in two locations. \end{itemdescr} \indexlibrary{\idxcode{swap}}% \begin{itemdecl} template void swap(T (&a)[N], T (&b)[N]) noexcept(is_nothrow_swappable_v); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_swappable_v} is \tcode{true}. \pnum \requires \tcode{a[i]} shall be swappable with~(\ref{swappable.requirements}) \tcode{b[i]} for all \tcode{i} in the range \range{0}{N}. \pnum \effects As if by \tcode{swap_ranges(a, a + N, b)}. \end{itemdescr} \rSec2[utility.exchange]{\tcode{exchange}} \indexlibrary{\idxcode{exchange}}% \begin{itemdecl} template T exchange(T& obj, U&& new_val); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} T old_val = std::move(obj); obj = std::forward(new_val); return old_val; \end{codeblock} \end{itemdescr} \rSec2[forward]{Forward/move helpers} \pnum The library provides templated helper functions to simplify applying move semantics to an lvalue and to simplify the implementation of forwarding functions. \indextext{signal-safe!\idxcode{forward}}% \indextext{signal-safe!\idxcode{move}}% \indextext{signal-safe!\idxcode{move_if_noexcept}}% All functions specified in this subclause are signal-safe~(\ref{csignal.syn}). \indexlibrary{\idxcode{forward}}% \begin{itemdecl} template constexpr T&& forward(remove_reference_t& t) noexcept; template constexpr T&& forward(remove_reference_t&& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{static_cast(t)}. \pnum \remarks If the second form is instantiated with an lvalue reference type, the program is ill-formed. \pnum \begin{example} \begin{codeblock} template shared_ptr factory(A1&& a1, A2&& a2) { return shared_ptr(new T(std::forward(a1), std::forward(a2))); } struct A { A(int&, const double&); }; void g() { shared_ptr sp1 = factory(2, 1.414); // error: 2 will not bind to \tcode{int\&} int i = 2; shared_ptr sp2 = factory(i, 1.414); // OK } \end{codeblock} In the first call to \tcode{factory}, \tcode{A1} is deduced as \tcode{int}, so 2 is forwarded to \tcode{A}'s constructor as an rvalue. In the second call to \tcode{factory}, \tcode{A1} is deduced as \tcode{int\&}, so \tcode{i} is forwarded to \tcode{A}'s constructor as an lvalue. In both cases, \tcode{A2} is deduced as \tcode{double}, so 1.414 is forwarded to \tcode{A}'s constructor as an rvalue. \end{example} \end{itemdescr} \indexlibrary{\idxcode{move}!function}% \begin{itemdecl} template constexpr remove_reference_t&& move(T&& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{static_cast\&\&>(t)}. \pnum \begin{example} \begin{codeblock} template shared_ptr factory(A1&& a1) { return shared_ptr(new T(std::forward(a1))); } struct A { A(); A(const A&); // copies from lvalues A(A&&); // moves from rvalues }; void g() { A a; shared_ptr sp1 = factory(a); // ``\tcode{a}\!'' binds to \tcode{A(const A\&)} shared_ptr sp1 = factory(std::move(a)); // ``\tcode{a}\!'' binds to \tcode{A(A\&\&)} } \end{codeblock} In the first call to \tcode{factory}, \tcode{A1} is deduced as \tcode{A\&}, so \tcode{a} is forwarded as a non-const lvalue. This binds to the constructor \tcode{A(const A\&)}, which copies the value from \tcode{a}. In the second call to \tcode{factory}, because of the call \tcode{std::move(a)}, \tcode{A1} is deduced as \tcode{A}, so \tcode{a} is forwarded as an rvalue. This binds to the constructor \tcode{A(A\&\&)}, which moves the value from \tcode{a}. \end{example} \end{itemdescr} \indexlibrary{\idxcode{move_if_noexcept}}% \begin{itemdecl} template constexpr conditional_t< !is_nothrow_move_constructible_v && is_copy_constructible_v, const T&, T&&> move_if_noexcept(T& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{std::move(x)}. \end{itemdescr} \rSec2[utility.as_const]{Function template \tcode{as_const}} \indexlibrary{\idxcode{as_const}}% \begin{itemdecl} template constexpr add_const_t& as_const(T& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{t}. \end{itemdescr} \rSec2[declval]{Function template \tcode{declval}} \pnum The library provides the function template \tcode{declval} to simplify the definition of expressions which occur as unevaluated operands (Clause~\ref{expr}). \indexlibrary{\idxcode{declval}}% \begin{itemdecl} template add_rvalue_reference_t declval() noexcept; // as unevaluated operand \end{itemdecl} \begin{itemdescr} \pnum \remarks If this function is odr-used~(\ref{basic.def.odr}), the program is ill-formed. \pnum \remarks The template parameter \tcode{T} of \tcode{declval} may be an incomplete type. \end{itemdescr} \pnum \begin{example} \begin{codeblock} template decltype(static_cast(declval())) convert(From&&); \end{codeblock} declares a function template \tcode{convert} which only participates in overloading if the type \tcode{From} can be explicitly converted to type \tcode{To}. For another example see class template \tcode{common_type}~(\ref{meta.trans.other}). \end{example} \rSec2[utility.to.chars]{Primitive numeric output conversion} \pnum All functions named \tcode{to_chars} convert \tcode{value} into a character string by successively filling the range \range{first}{last}, where \range{first}{last} is required to be a valid range. If the member \tcode{ec} of the return value is such that the value, when converted to \tcode{bool}, is \tcode{false}, the conversion was successful and the member \tcode{ptr} is the one-past-the-end pointer of the characters written. Otherwise, the member \tcode{ec} has the value \tcode{errc::value_too_large}, the member \tcode{ptr} has the value \tcode{last}, and the contents of the range \range{first}{last} are unspecified. \pnum The functions that take a floating-point \tcode{value} but not a \tcode{precision} parameter ensure that the string representation consists of the smallest number of characters such that there is at least one digit before the radix point (if present) and parsing the representation using the corresponding \tcode{from_chars} function recovers \tcode{value} exactly. \begin{note} This guarantee applies only if \tcode{to_chars} and \tcode{from_chars} are executed on the same implementation. \end{note} \pnum The functions taking a \tcode{chars_format} parameter determine the conversion specifier for \tcode{printf} as follows: The conversion specifier is \tcode{f} if \tcode{fmt} is \tcode{chars_format::fixed}, \tcode{e} if \tcode{fmt} is \tcode{chars_format::scientific}, \tcode{a} (without leading \tcode{"0x"} in the result) if \tcode{fmt} is \tcode{chars_format::hex}, and \tcode{g} if \tcode{fmt} is \tcode{chars_format::general}. \indexlibrary{\idxcode{to_chars}}% \begin{itemdecl} to_chars_result to_chars(char* first, char* last, @\seebelow@ value, int base = 10); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{base} has a value between 2 and 36 (inclusive). \pnum \effects The value of \tcode{value} is converted to a string of digits in the given base (with no redundant leading zeroes). Digits in the range 10..35 (inclusive) are represented as lowercase characters \tcode{a}..\tcode{z}. If \tcode{value} is less than zero, the representation starts with a minus sign. \pnum \throws Nothing. \pnum \remarks The implementation shall provide overloads for all signed and unsigned integer types and \tcode{char} as the type of the parameter \tcode{value}. \end{itemdescr} \indexlibrary{\idxcode{to_chars}}% \begin{itemdecl} to_chars_result to_chars(char* first, char* last, float value); to_chars_result to_chars(char* first, char* last, double value); to_chars_result to_chars(char* first, char* last, long double value); \end{itemdecl} \begin{itemdescr} \pnum \effects \tcode{value} is converted to a string in the style of \tcode{printf} in the \tcode{"C"} locale. The conversion specifier is \tcode{f} or \tcode{e}, chosen according to the requirement for a shortest representation (see above); a tie is resolved in favor of \tcode{f}. \pnum \throws Nothing. \end{itemdescr} \indexlibrary{\idxcode{to_chars}}% \begin{itemdecl} to_chars_result to_chars(char* first, char* last, float value, chars_format fmt); to_chars_result to_chars(char* first, char* last, double value, chars_format fmt); to_chars_result to_chars(char* first, char* last, long double value, chars_format fmt); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{fmt} has the value of one of the enumerators of \tcode{chars_format}. \pnum \effects \tcode{value} is converted to a string in the style of \tcode{printf} in the \tcode{"C"} locale. \pnum \throws Nothing. \end{itemdescr} \indexlibrary{\idxcode{to_chars}}% \begin{itemdecl} to_chars_result to_chars(char* first, char* last, float value, chars_format fmt, int precision); to_chars_result to_chars(char* first, char* last, double value, chars_format fmt, int precision); to_chars_result to_chars(char* first, char* last, long double value, chars_format fmt, int precision); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{fmt} has the value of one of the enumerators of \tcode{chars_format}. \pnum \effects \tcode{value} is converted to a string in the style of \tcode{printf} in the \tcode{"C"} locale with the given precision. \pnum \throws Nothing. \end{itemdescr} \xref ISO C 7.21.6.1 \rSec2[utility.from.chars]{Primitive numeric input conversion} \pnum All functions named \tcode{from_chars} analyze the string \range{first}{last} for a pattern, where \range{first}{last} is required to be a valid range. If no characters match the pattern, \tcode{value} is unmodified, the member \tcode{ptr} of the return value is \tcode{first} and the member \tcode{ec} is equal to \tcode{errc::invalid_argument}. Otherwise, the characters matching the pattern are interpreted as a representation of a value of the type of \tcode{value}. The member \tcode{ptr} of the return value points to the first character not matching the pattern, or has the value \tcode{last} if all characters match. If the parsed value is not in the range representable by the type of \tcode{value}, \tcode{value} is unmodified and the member \tcode{ec} of the return value is equal to \tcode{errc::result_out_of_range}. Otherwise, \tcode{value} is set to the parsed value and the member \tcode{ec} is set such that the conversion to \tcode{bool} yields \tcode{false}. \indexlibrary{\idxcode{from_chars}}% \begin{itemdecl} from_chars_result from_chars(const char* first, const char* last, @\seebelow@&@\itcorr[-1]@ value, int base = 10); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{base} has a value between 2 and 36 (inclusive). \pnum \effects The pattern is the expected form of the subject sequence in the \tcode{"C"} locale for the given nonzero base, as described for \tcode{strtol}, except that no \tcode{"0x"} or \tcode{"0X"} prefix shall appear if the value of \tcode{base} is 16, and except that a minus sign is the only sign that may appear, and only if \tcode{value} has a signed type. \pnum \throws Nothing. \pnum \remarks The implementation shall provide overloads for all signed and unsigned integer types and \tcode{char} as the referenced type of the parameter \tcode{value}. \end{itemdescr} \indexlibrary{\idxcode{from_chars}}% \begin{itemdecl} from_chars_result from_chars(const char* first, const char* last, float& value, chars_format fmt = chars_format::general); from_chars_result from_chars(const char* first, const char* last, double& value, chars_format fmt = chars_format::general); from_chars_result from_chars(const char* first, const char* last, long double& value, chars_format fmt = chars_format::general); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{fmt} has the value of one of the enumerators of \tcode{chars_format}. \pnum \effects The pattern is the expected form of the subject sequence in the \tcode{"C"} locale, as described for \tcode{strtod}, except that \begin{itemize} \item the only sign that may appear is a minus sign; \item if \tcode{fmt} has \tcode{chars_format::scientific} set but not \tcode{chars_format::fixed}, the otherwise optional exponent part shall appear; \item if \tcode{fmt} has \tcode{chars_format::fixed} set but not \tcode{chars_format::scientific}, the optional exponent part shall not appear; and \item if \tcode{fmt} is \tcode{chars_format::hex}, the prefix \tcode{"0x"} or \tcode{"0X"} is assumed. \begin{example} The string \tcode{0x123} is parsed to have the value \tcode{0} with remaining characters \tcode{x123}. \end{example} \end{itemize} In any case, the resulting \tcode{value} is one of at most two floating-point values closest to the value of the string matching the pattern. \pnum \throws Nothing. \end{itemdescr} \xref ISO C 7.22.1.3, 7.22.1.4 \rSec1[intseq]{Compile-time integer sequences} \rSec2[intseq.general]{In general} \pnum The library provides a class template that can represent an integer sequence. When used as an argument to a function template the parameter pack defining the sequence can be deduced and used in a pack expansion. \begin{note} The \tcode{index_sequence} alias template is provided for the common case of an integer sequence of type \tcode{size_t}; see also \ref{tuple.apply}. \end{note} \rSec2[intseq.intseq]{Class template \tcode{integer_sequence}} \indexlibrary{\idxcode{integer_sequence}}% \begin{codeblock} namespace std { template struct integer_sequence { using value_type = T; static constexpr size_t size() noexcept { return sizeof...(I); } }; } \end{codeblock} \pnum \tcode{T} shall be an integer type. \rSec2[intseq.make]{Alias template \tcode{make_integer_sequence}} \indexlibrary{\idxcode{make_integer_sequence}}% \begin{itemdecl} template using make_integer_sequence = integer_sequence; \end{itemdecl} \begin{itemdescr} \pnum If \tcode{N} is negative the program is ill-formed. The alias template \tcode{make_integer_sequence} denotes a specialization of \tcode{integer_sequence} with \tcode{N} template non-type arguments. The type \tcode{make_integer_sequence} denotes the type \tcode{integer_sequence}. \begin{note} \tcode{make_integer_sequence} denotes the type \tcode{integer_sequence} \end{note} \end{itemdescr} \rSec1[pairs]{Pairs} \rSec2[pairs.general]{In general} \pnum The library provides a template for heterogeneous pairs of values. The library also provides a matching function template to simplify their construction and several templates that provide access to \tcode{pair} objects as if they were \tcode{tuple} objects (see~\ref{tuple.helper} and~\ref{tuple.elem}).% \indexlibrary{\idxcode{pair}}% \indextext{\idxcode{pair}!tuple interface to}% \indextext{\idxcode{tuple}!and pair@and \tcode{pair}}% \rSec2[pairs.pair]{Class template \tcode{pair}} \indexlibrary{\idxcode{pair}}% \begin{codeblock} namespace std { template struct pair { using first_type = T1; using second_type = T2; T1 first; T2 second; pair(const pair&) = default; pair(pair&&) = default; @\EXPLICIT@ constexpr pair(); @\EXPLICIT@ constexpr pair(const T1& x, const T2& y); template @\EXPLICIT@ constexpr pair(U1&& x, U2&& y); template @\EXPLICIT@ constexpr pair(const pair& p); template @\EXPLICIT@ constexpr pair(pair&& p); template pair(piecewise_construct_t, tuple first_args, tuple second_args); pair& operator=(const pair& p); template pair& operator=(const pair& p); pair& operator=(pair&& p) noexcept(@\seebelow@); template pair& operator=(pair&& p); void swap(pair& p) noexcept(@\seebelow@); }; template pair(T1, T2) -> pair; } \end{codeblock} \pnum Constructors and member functions of \tcode{pair} shall not throw exceptions unless one of the element-wise operations specified to be called for that operation throws an exception. \pnum The defaulted move and copy constructor, respectively, of \tcode{pair} shall be a constexpr function if and only if all required element-wise initializations for copy and move, respectively, would satisfy the requirements for a constexpr function. The destructor of \tcode{pair} shall be a trivial destructor if \tcode{(is_trivially_destructible_v \&\& is_trivially_destructible_v)} is \tcode{true}. \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} @\EXPLICIT@ constexpr pair(); \end{itemdecl} \begin{itemdescr} \pnum \effects Value-initializes \tcode{first} and \tcode{second}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_default_construct\-ible_v} is \tcode{true} and \tcode{is_default_constructible_v} is \tcode{true}. \begin{note} This behavior can be implemented by a constructor template with default template arguments. \end{note} The constructor is explicit if and only if either \tcode{first_type} or \tcode{second_type} is not implicitly default-constructible. \begin{note} This behavior can be implemented with a trait that checks whether a \tcode{const first_type\&} or a \tcode{const second_type\&} can be initialized with \tcode{\{\}}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} @\EXPLICIT@ constexpr pair(const T1& x, const T2& y); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes \tcode{first} with \tcode{x} and \tcode{second} with \tcode{y}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_copy_construct\-ible_v} is \tcode{true} and \tcode{is_copy_constructible_v} is \tcode{true}. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr pair(U1&& x, U2&& y); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes \tcode{first} with \tcode{std::forward(x)} and \tcode{second} with \tcode{std::forward(y)}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr pair(const pair& p); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes members from the corresponding members of the argument. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr pair(pair&& p); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes \tcode{first} with \tcode{std::forward(p.first)} and \tcode{second} with \tcode{std::forward(\brk{}p.second)}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{pair}!constructor}% \begin{itemdecl} template pair(piecewise_construct_t, tuple first_args, tuple second_args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. \pnum \effects Initializes \tcode{first} with arguments of types \tcode{Args1...} obtained by forwarding the elements of \tcode{first_args} and initializes \tcode{second} with arguments of types \tcode{Args2...} obtained by forwarding the elements of \tcode{second_args}. (Here, forwarding an element \tcode{x} of type \tcode{U} within a \tcode{tuple} object means calling \tcode{std::forward(x)}.) This form of construction, whereby constructor arguments for \tcode{first} and \tcode{second} are each provided in a separate \tcode{tuple} object, is called \defn{piecewise construction}. \end{itemdescr} \indexlibrarymember{operator=}{pair}% \begin{itemdecl} pair& operator=(const pair& p); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns \tcode{p.first} to \tcode{first} and \tcode{p.second} to \tcode{second}. \pnum \remarks This operator shall be defined as deleted unless \tcode{is_copy_assignable_v} is \tcode{true} and \tcode{is_copy_assignable_v} is \tcode{true}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{pair}% \begin{itemdecl} template pair& operator=(const pair& p); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns \tcode{p.first} to \tcode{first} and \tcode{p.second} to \tcode{second}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{is_assignable_v} is \tcode{true} and \tcode{is_assignable_v} is \tcode{true}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{pair}% \begin{itemdecl} pair& operator=(pair&& p) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns to \tcode{first} with \tcode{std::forward(p.first)} and to \tcode{second} with\\ \tcode{std::forward(p.second)}. \pnum \remarks This operator shall be defined as deleted unless \tcode{is_move_assignable_v} is \tcode{true} and \tcode{is_move_assignable_v} is \tcode{true}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_move_assignable_v && is_nothrow_move_assignable_v \end{codeblock} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{pair}% \begin{itemdecl} template pair& operator=(pair&& p); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns to \tcode{first} with \tcode{std::forward(p.first)} and to \tcode{second} with\\ \tcode{std::forward(p.second)}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{is_assignable_v} is \tcode{true} and \tcode{is_assignable_v} is \tcode{true}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{swap}{pair}% \begin{itemdecl} void swap(pair& p) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{first} shall be swappable with~(\ref{swappable.requirements}) \tcode{p.first} and \tcode{second} shall be swappable with \tcode{p.second}. \pnum \effects Swaps \tcode{first} with \tcode{p.first} and \tcode{second} with \tcode{p.second}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_swappable_v && is_nothrow_swappable_v \end{codeblock} \end{itemdescr} \rSec2[pairs.spec]{Specialized algorithms} \indexlibrarymember{operator==}{pair}% \begin{itemdecl} template constexpr bool operator==(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{x.first == y.first \&\& x.second == y.second}. \end{itemdescr} \indexlibrarymember{operator<}{pair}% \begin{itemdecl} template constexpr bool operator<(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{x.first < y.first || (!(y.first < x.first) \&\& x.second < y.second)}. \end{itemdescr} \indexlibrarymember{operator"!=}{pair}% \begin{itemdecl} template constexpr bool operator!=(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(x == y)}. \end{itemdescr} \indexlibrarymember{operator>}{pair}% \begin{itemdecl} template constexpr bool operator>(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{y < x}. \end{itemdescr} \indexlibrarymember{operator>=}{pair}% \begin{itemdecl} template constexpr bool operator>=(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(x < y)}. \end{itemdescr} \indexlibrarymember{operator<=}{pair}% \begin{itemdecl} template constexpr bool operator<=(const pair& x, const pair& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(y < x)}. \end{itemdescr} \indexlibrary{\idxcode{swap}!\idxcode{pair}}% \begin{itemdecl} template void swap(pair& x, pair& y) noexcept(noexcept(x.swap(y))); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{x.swap(y)}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_swappable_v} is \tcode{true} and \tcode{is_swappable_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{make_pair}}% \begin{itemdecl} template constexpr pair make_pair(T1&& x, T2&& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{pair(std::forward(x), std::forward(y))}, where \tcode{V1} and \tcode{V2} are determined as follows: Let \tcode{Ui} be \tcode{decay_t} for each \tcode{Ti}. If \tcode{Ui} is a specialization of \tcode{reference_wrapper}, then \tcode{Vi} is \tcode{Ui::type\&}, otherwise \tcode{Vi} is \tcode{Ui}. \end{itemdescr} \pnum \begin{example} In place of: \begin{codeblock} return pair(5, 3.1415926); // explicit types \end{codeblock} a \Cpp program may contain: \begin{codeblock} return make_pair(5, 3.1415926); // types are deduced \end{codeblock} \end{example} \rSec2[pair.astuple]{Tuple-like access to pair} \indexlibrary{\idxcode{tuple_size}}% \begin{itemdecl} template struct tuple_size> : integral_constant { }; \end{itemdecl} \indexlibrary{\idxcode{tuple_element}}% \begin{itemdecl} tuple_element<0, pair>::type \end{itemdecl} \begin{itemdescr} \pnum\textit{Value:} The type \tcode{T1}. \end{itemdescr} \indexlibrary{\idxcode{tuple_element}}% \begin{itemdecl} tuple_element<1, pair>::type \end{itemdecl} \begin{itemdescr} \pnum\textit{Value:} The type T2. \end{itemdescr} \indexlibrarymember{get}{pair}% \begin{itemdecl} template constexpr tuple_element_t>& get(pair& p) noexcept; template constexpr const tuple_element_t>& get(const pair& p) noexcept; template constexpr tuple_element_t>&& get(pair&& p) noexcept; template constexpr const tuple_element_t>&& get(const pair&& p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns If \tcode{I == 0} returns a reference to \tcode{p.first}; if \tcode{I == 1} returns a reference to \tcode{p.second}; otherwise the program is ill-formed. \end{itemdescr} \indexlibrarymember{get}{pair}% \begin{itemdecl} template constexpr T1& get(pair& p) noexcept; template constexpr const T1& get(const pair& p) noexcept; template constexpr T1&& get(pair&& p) noexcept; template constexpr const T1&& get(const pair&& p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{T1} and \tcode{T2} are distinct types. Otherwise, the program is ill-formed. \pnum \returns A reference to \tcode{p.first}. \end{itemdescr} \indexlibrarymember{get}{pair}% \begin{itemdecl} template constexpr T2& get(pair& p) noexcept; template constexpr const T2& get(const pair& p) noexcept; template constexpr T2&& get(pair&& p) noexcept; template constexpr const T2&& get(const pair&& p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{T1} and \tcode{T2} are distinct types. Otherwise, the program is ill-formed. \pnum \returns A reference to \tcode{p.second}. \end{itemdescr} \rSec2[pair.piecewise]{Piecewise construction} \indexlibrary{\idxcode{piecewise_construct_t}}% \indexlibrary{\idxcode{piecewise_construct}}% \begin{itemdecl} struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; inline constexpr piecewise_construct_t piecewise_construct{}; \end{itemdecl} \pnum The \tcode{struct} \tcode{piecewise_construct_t} is an empty structure type used as a unique type to disambiguate constructor and function overloading. Specifically, \tcode{pair} has a constructor with \tcode{piecewise_construct_t} as the first argument, immediately followed by two \tcode{tuple}~(\ref{tuple}) arguments used for piecewise construction of the elements of the \tcode{pair} object. \rSec1[tuple]{Tuples} \rSec2[tuple.general]{In general} \pnum \indexlibrary{\idxcode{tuple}}% This subclause describes the tuple library that provides a tuple type as the class template \tcode{tuple} that can be instantiated with any number of arguments. Each template argument specifies the type of an element in the \tcode{tuple}. Consequently, tuples are heterogeneous, fixed-size collections of values. An instantiation of \tcode{tuple} with two arguments is similar to an instantiation of \tcode{pair} with the same two arguments. See~\ref{pairs}. \rSec2[tuple.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{tuple}}% \indexlibrary{\idxhdr{tuple}}% \begin{codeblock} namespace std { // \ref{tuple.tuple}, class template \tcode{tuple} template class tuple; // \ref{tuple.creation}, tuple creation functions inline constexpr @\unspec@ ignore; template constexpr tuple make_tuple(TTypes&&...); template constexpr tuple forward_as_tuple(TTypes&&...) noexcept; template constexpr tuple tie(TTypes&...) noexcept; template constexpr tuple tuple_cat(Tuples&&...); // \ref{tuple.apply}, calling a function with a tuple of arguments template constexpr decltype(auto) apply(F&& f, Tuple&& t); template constexpr T make_from_tuple(Tuple&& t); // \ref{tuple.helper}, tuple helper classes template class tuple_size; // not defined template class tuple_size; template class tuple_size; template class tuple_size; template class tuple_size>; template class tuple_element; // not defined template class tuple_element; template class tuple_element; template class tuple_element; template class tuple_element>; template using tuple_element_t = typename tuple_element::type; // \ref{tuple.elem}, element access template constexpr tuple_element_t>& get(tuple&) noexcept; template constexpr tuple_element_t>&& get(tuple&&) noexcept; template constexpr const tuple_element_t>& get(const tuple&) noexcept; template constexpr const tuple_element_t>&& get(const tuple&&) noexcept; template constexpr T& get(tuple& t) noexcept; template constexpr T&& get(tuple&& t) noexcept; template constexpr const T& get(const tuple& t) noexcept; template constexpr const T&& get(const tuple&& t) noexcept; // \ref{tuple.rel}, relational operators template constexpr bool operator==(const tuple&, const tuple&); template constexpr bool operator<(const tuple&, const tuple&); template constexpr bool operator!=(const tuple&, const tuple&); template constexpr bool operator>(const tuple&, const tuple&); template constexpr bool operator<=(const tuple&, const tuple&); template constexpr bool operator>=(const tuple&, const tuple&); // \ref{tuple.traits}, allocator-related traits template struct uses_allocator, Alloc>; // \ref{tuple.special}, specialized algorithms template void swap(tuple& x, tuple& y) noexcept(@\seebelow@); // \ref{tuple.helper}, tuple helper classes template inline constexpr size_t tuple_size_v = tuple_size::value; } \end{codeblock} \rSec2[tuple.tuple]{Class template \tcode{tuple}} \indexlibrary{\idxcode{tuple}}% \begin{codeblock} namespace std { template class tuple { public: // \ref{tuple.cnstr}, \tcode{tuple} construction @\EXPLICIT@ constexpr tuple(); @\EXPLICIT@ constexpr tuple(const Types&...); // only if \tcode{sizeof...(Types) >= 1} template @\EXPLICIT@ constexpr tuple(UTypes&&...); // only if \tcode{sizeof...(Types) >= 1} tuple(const tuple&) = default; tuple(tuple&&) = default; template @\EXPLICIT@ constexpr tuple(const tuple&); template @\EXPLICIT@ constexpr tuple(tuple&&); template @\EXPLICIT@ constexpr tuple(const pair&); // only if \tcode{sizeof...(Types) == 2} template @\EXPLICIT@ constexpr tuple(pair&&); // only if \tcode{sizeof...(Types) == 2} // allocator-extended constructors template tuple(allocator_arg_t, const Alloc& a); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const Types&...); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, UTypes&&...); template tuple(allocator_arg_t, const Alloc& a, const tuple&); template tuple(allocator_arg_t, const Alloc& a, tuple&&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const tuple&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, tuple&&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const pair&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, pair&&); // \ref{tuple.assign}, \tcode{tuple} assignment tuple& operator=(const tuple&); tuple& operator=(tuple&&) noexcept(@\seebelow@); template tuple& operator=(const tuple&); template tuple& operator=(tuple&&); template tuple& operator=(const pair&); // only if \tcode{sizeof...(Types) == 2} template tuple& operator=(pair&&); // only if \tcode{sizeof...(Types) == 2} // \ref{tuple.swap}, \tcode{tuple} swap void swap(tuple&) noexcept(@\seebelow@); }; template tuple(UTypes...) -> tuple; template tuple(pair) -> tuple; template tuple(allocator_arg_t, Alloc, UTypes...) -> tuple; template tuple(allocator_arg_t, Alloc, pair) -> tuple; template tuple(allocator_arg_t, Alloc, tuple) -> tuple; } \end{codeblock} \rSec3[tuple.cnstr]{Construction} \pnum For each \tcode{tuple} constructor, an exception is thrown only if the construction of one of the types in \tcode{Types} throws an exception. \pnum The defaulted move and copy constructor, respectively, of \tcode{tuple} shall be a constexpr function if and only if all required element-wise initializations for copy and move, respectively, would satisfy the requirements for a constexpr function. The defaulted move and copy constructor of \tcode{tuple<>} shall be constexpr functions. \pnum The destructor of tuple shall be a trivial destructor if \tcode{(is_trivially_destructible_v \&\& ...)} is \tcode{true}. \pnum In the constructor descriptions that follow, let $i$ be in the range \range{0}{sizeof...(Types)} in order, $\tcode{T}_i$ be the $i^\text{th}$ type in \tcode{Types}, and $\tcode{U}_i$ be the $i^\text{th}$ type in a template parameter pack named \tcode{UTypes}, where indexing is zero-based. \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} @\EXPLICIT@ constexpr tuple(); \end{itemdecl} \begin{itemdescr} \pnum \effects Value-initializes each element. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_default_construct\-ible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \begin{note} This behavior can be implemented by a constructor template with default template arguments. \end{note} The constructor is explicit if and only if $\tcode{T}_i$ is not implicitly default-constructible for at least one $i$. \begin{note} This behavior can be implemented with a trait that checks whether a \tcode{const $\tcode{T}_i$\&} can be initialized with \tcode{\{\}}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} @\EXPLICIT@ constexpr tuple(const Types&...); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes each element with the value of the corresponding parameter. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{sizeof...(Types) >= 1} and \tcode{is_copy_constructible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} for at least one $i$. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr tuple(UTypes&&... u); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the elements in the tuple with the corresponding value in \tcode{std::forward(u)}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{sizeof...(Types)} \tcode{==} \tcode{sizeof...(UTypes)} and \tcode{sizeof...(Types) >= 1} and \tcode{is_constructible_v<$\tcode{T}_i$, $\tcode{U}_i$\&\&>} is \tcode{true} for all $i$. The constructor is explicit if and only if \tcode{is_convertible_v<$\tcode{U}_i$\&\&, $\tcode{T}_i$>} is \tcode{false} for at least one $i$. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} tuple(const tuple& u) = default; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_copy_constructible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \pnum \effects Initializes each element of \tcode{*this} with the corresponding element of \tcode{u}. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} tuple(tuple&& u) = default; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_move_constructible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \pnum \effects For all $i$, initializes the $i^\text{th}$ element of \tcode{*this} with \tcode{std::forward<$\tcode{T}_i$>(get<$i$>(u))}. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr tuple(const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes each element of \tcode{*this} with the corresponding element of \tcode{u}. \pnum \remarks This constructor shall not participate in overload resolution unless \begin{itemize} \item \tcode{sizeof...(Types)} \tcode{==} \tcode{sizeof...(UTypes)} and \item \tcode{is_constructible_v<$\tcode{T}_i$, const $\tcode{U}_i$\&>} is \tcode{true} for all $i$, and \item \tcode{sizeof...(Types) != 1}, or (when \tcode{Types...} expands to \tcode{T} and \tcode{UTypes...} expands to \tcode{U})\linebreak \tcode{!is_convertible_v\&, T> \&\& !is_constructible_v\&>\linebreak{}\&\& !is_same_v} is \tcode{true}. \end{itemize} The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} for at least one $i$. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr tuple(tuple&& u); \end{itemdecl} \begin{itemdescr} \pnum \effects For all $i$, initializes the $i^\text{th}$ element of \tcode{*this} with \tcode{std::forward<$\tcode{U}_i$>(get<$i$>(u))}. \pnum \remarks This constructor shall not participate in overload resolution unless \begin{itemize} \item \tcode{sizeof...(Types)} \tcode{==} \tcode{sizeof...(UTypes)}, and \item \tcode{is_constructible_v<$\tcode{T}_i$, $\tcode{U}_i$\&\&>} is \tcode{true} for all $i$, and \item \tcode{sizeof...(Types) != 1}, or (when \tcode{Types...} expands to \tcode{T} and \tcode{UTypes...} expands to \tcode{U})\linebreak \tcode{!is_convertible_v, T> \&\& !is_constructible_v> \&\&\linebreak{}!is_same_v} is \tcode{true}. \end{itemize} The constructor is explicit if and only if \tcode{is_convertible_v<$\tcode{U}_i$\&\&, $\tcode{T}_i$>} is \tcode{false} for at least one $i$. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \indexlibrary{\idxcode{pair}}% \begin{itemdecl} template @\EXPLICIT@ constexpr tuple(const pair& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the first element with \tcode{u.first} and the second element with \tcode{u.second}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{sizeof...(Types) == 2}, \tcode{is_constructible_v<$\tcode{T}_0$, const U1\&>} is \tcode{true} and \tcode{is_constructible_v<$\tcode{T}_1$, const U2\&>} is \tcode{true}. \pnum The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convert\-ible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \indexlibrary{\idxcode{pair}}% \begin{itemdecl} template @\EXPLICIT@ constexpr tuple(pair&& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the first element with \tcode{std::forward(u.first)} and the second element with \tcode{std::forward(u.second)}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{sizeof...(Types) == 2}, \tcode{is_constructible_v<$\tcode{T}_0$, U1\&\&>} is \tcode{true} and \tcode{is_constructible_v<$\tcode{T}_1$, U2\&\&>} is \tcode{true}. \pnum The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false} or \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{tuple}!constructor}% \begin{itemdecl} template tuple(allocator_arg_t, const Alloc& a); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const Types&...); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, UTypes&&...); template tuple(allocator_arg_t, const Alloc& a, const tuple&); template tuple(allocator_arg_t, const Alloc& a, tuple&&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const tuple&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, tuple&&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, const pair&); template @\EXPLICIT@ tuple(allocator_arg_t, const Alloc& a, pair&&); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{Alloc} shall meet the requirements for an \tcode{Allocator}~(\ref{allocator.requirements}). \pnum \effects Equivalent to the preceding constructors except that each element is constructed with uses-allocator construction~(\ref{allocator.uses.construction}). \end{itemdescr} \rSec3[tuple.assign]{Assignment} \pnum For each \tcode{tuple} assignment operator, an exception is thrown only if the assignment of one of the types in \tcode{Types} throws an exception. In the function descriptions that follow, let $i$ be in the range \range{0}{sizeof...\brk{}(Types)} in order, $\tcode{T}_i$ be the $i^\text{th}$ type in \tcode{Types}, and $\tcode{U}_i$ be the $i^\text{th}$ type in a template parameter pack named \tcode{UTypes}, where indexing is zero-based. \indexlibrarymember{operator=}{tuple}% \begin{itemdecl} tuple& operator=(const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns each element of \tcode{u} to the corresponding element of \tcode{*this}. \pnum \remarks This operator shall be defined as deleted unless \tcode{is_copy_assignable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{tuple}% \begin{itemdecl} tuple& operator=(tuple&& u) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects For all $i$, assigns \tcode{std::forward<$\tcode{T}_i$>(get<$i$>(u))} to \tcode{get<$i$>(*this)}. \pnum \remarks This operator shall be defined as deleted unless \tcode{is_move_assignable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to the logical \textsc{and} of the following expressions: \begin{codeblock} is_nothrow_move_assignable_v<@$\mathtt{T}_i$@> \end{codeblock} where $\mathtt{T}_i$ is the $i^\text{th}$ type in \tcode{Types}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{tuple}% \begin{itemdecl} template tuple& operator=(const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns each element of \tcode{u} to the corresponding element of \tcode{*this}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{sizeof...(Types) == sizeof...(UTypes)} and \tcode{is_assignable_v<$\tcode{T}_i$\&, const $\tcode{U}_i$\&>} is \tcode{true} for all $i$. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{tuple}% \begin{itemdecl} template tuple& operator=(tuple&& u); \end{itemdecl} \begin{itemdescr} \pnum \effects For all $i$, assigns \tcode{std::forward<$\tcode{U}_i$>(get<$i$>(u))} to \tcode{get<$i$>(*this)}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{is_assignable_v<$\tcode{T}_i$\&, $\tcode{U}_i$\&\&> == true} for all $i$ and \tcode{sizeof...(Types) == sizeof...(UTypes)}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{tuple}% \indexlibrary{\idxcode{pair}}% \begin{itemdecl} template tuple& operator=(const pair& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns \tcode{u.first} to the first element of \tcode{*this} and \tcode{u.second} to the second element of \tcode{*this}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{sizeof...(Types) == 2} and \tcode{is_assignable_v<$\tcode{T}_0$\&, const U1\&>} is \tcode{true} for the first type $\tcode{T}_0$ in \tcode{Types} and \tcode{is_assignable_v<$\tcode{T}_1$\&, const U2\&>} is \tcode{true} for the second type $\tcode{T}_1$ in \tcode{Types}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{tuple}% \indexlibrary{\idxcode{pair}}% \begin{itemdecl} template tuple& operator=(pair&& u); \end{itemdecl} \begin{itemdescr} \pnum \effects Assigns \tcode{std::forward(u.first)} to the first element of \tcode{*this} and\\ \tcode{std::forward(u.second)} to the second element of \tcode{*this}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{sizeof...(Types) == 2} and \tcode{is_assignable_v<$\tcode{T}_0$\&, U1\&\&>} is \tcode{true} for the first type $\tcode{T}_0$ in \tcode{Types} and \tcode{is_assignable_v<$\tcode{T}_1$\&, U2\&\&>} is \tcode{true} for the second type $\tcode{T}_1$ in \tcode{Types}. \pnum \returns \tcode{*this}. \end{itemdescr} \rSec3[tuple.swap]{\tcode{swap}} \indexlibrarymember{swap}{tuple}% \begin{itemdecl} void swap(tuple& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \requires Each element in \tcode{*this} shall be swappable with~(\ref{swappable.requirements}) the corresponding element in \tcode{rhs}. \pnum \effects Calls \tcode{swap} for each element in \tcode{*this} and its corresponding element in \tcode{rhs}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to the logical \textsc{and} of the following expressions: \begin{codeblock} is_nothrow_swappable_v<@$\mathtt{T}_i$@> \end{codeblock} where $\mathtt{T}_i$ is the $i^\text{th}$ type in \tcode{Types}. \pnum \throws Nothing unless one of the element-wise \tcode{swap} calls throws an exception. \end{itemdescr} \rSec3[tuple.creation]{Tuple creation functions} \pnum In the function descriptions that follow, the members of a parameter pack \tcode{\placeholder{X}Types} are denoted by \tcode{\placeholder{X}}$_i$ for $i$ in \range{0}{sizeof...(\placeholder{X}Types)} in order, where indexing is zero-based. \indexlibrary{\idxcode{make_tuple}}% \indexlibrary{\idxcode{tuple}!\idxcode{make_tuple}}% \begin{itemdecl} template constexpr tuple make_tuple(TTypes&&... t); \end{itemdecl} \begin{itemdescr} \pnum The pack \tcode{VTypes} is defined as follows. Let \tcode{U}$_i$ be \tcode{decay_t} for each \tcode{T}$_i$ in \tcode{TTypes}. If \tcode{U}$_i$ is a specialization of \tcode{reference_wrapper}, then \tcode{V}$_i$ in \tcode{VTypes} is \tcode{U$_i$::type\&}, otherwise \tcode{V}$_i$ is \tcode{U}$_i$. \pnum \returns \tcode{tuple(std::forward(t)...)}. \pnum \begin{example} \begin{codeblock} int i; float j; make_tuple(1, ref(i), cref(j)) \end{codeblock} creates a tuple of type \tcode{tuple}. \end{example} \end{itemdescr} \indexlibrary{\idxcode{forward_as_tuple}}% \indexlibrary{\idxcode{tuple}!\idxcode{forward_as_tuple}}% \begin{itemdecl} template constexpr tuple forward_as_tuple(TTypes&&... t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a tuple of references to the arguments in \tcode{t} suitable for forwarding as arguments to a function. Because the result may contain references to temporary variables, a program shall ensure that the return value of this function does not outlive any of its arguments (e.g., the program should typically not store the result in a named variable). \pnum \returns \tcode{tuple(std::forward(t)...)}. \end{itemdescr} \indexlibrary{\idxcode{tie}}% \indexlibrary{\idxcode{ignore}}% \indexlibrary{\idxcode{tuple}!\idxcode{tie}}% \begin{itemdecl} template constexpr tuple tie(TTypes&... t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{tuple(t...)}. When an argument in \tcode{t} is \tcode{ignore}, assigning any value to the corresponding tuple element has no effect. \pnum \begin{example} \tcode{tie} functions allow one to create tuples that unpack tuples into variables. \tcode{ignore} can be used for elements that are not needed: \begin{codeblock} int i; std::string s; tie(i, ignore, s) = make_tuple(42, 3.14, "C++"); // \tcode{i == 42}, \tcode{s == "C++"} \end{codeblock} \end{example} \end{itemdescr} \indexlibrary{\idxcode{tuple_cat}} \begin{itemdecl} template constexpr tuple tuple_cat(Tuples&&... tpls); \end{itemdecl} \begin{itemdescr} \pnum In the following paragraphs, let $\tcode{T}_i$ be the $i^\text{th}$ type in \tcode{Tuples}, $\tcode{U}_i$ be \tcode{remove_reference_t}, and $\tcode{tp}_i$ be the $i^\text{th}$ parameter in the function parameter pack \tcode{tpls}, where all indexing is zero-based. \pnum \requires For all $i$, $\tcode{U}_i$ shall be the type $\cv_i$ \tcode{tuple<$\tcode{Args}_i$...>}, where $\cv_i$ is the (possibly empty) $i^\text{th}$ \grammarterm{cv-qualifier-seq} and $\tcode{Args}_i$ is the parameter pack representing the element types in $\tcode{U}_i$. Let $\tcode{A}_{ik}$ be the ${k}^\text{th}$ type in $\tcode{Args}_i$. For all $\tcode{A}_{ik}$ the following requirements shall be satisfied: \begin{itemize} \item If $\tcode{T}_i$ is deduced as an lvalue reference type, then \tcode{is_constructible_v<$\tcode{A}_{ik}$, $cv_i\;\tcode{A}_{ik}$\&> == true}, otherwise \item \tcode{is_constructible_v<$\tcode{A}_{ik}$, $cv_i\;\tcode{A}_{ik}$\&\&> == true}. \end{itemize} \pnum \remarks The types in \tcode{CTypes} shall be equal to the ordered sequence of the extended types \tcode{$\tcode{Args}_0$..., $\tcode{Args}_1$..., $\dotsc$, $\tcode{Args}_{n-1}$...}, where $n$ is equal to \tcode{sizeof...(Tuples)}. Let \tcode{$\tcode{e}_i$...} be the $i^\text{th}$ ordered sequence of tuple elements of the resulting \tcode{tuple} object corresponding to the type sequence $\tcode{Args}_i$. \pnum \returns A \tcode{tuple} object constructed by initializing the ${k_i}^\text{th}$ type element $\tcode{e}_{ik}$ in \tcode{$\tcode{e}_i$...} with \begin{codeblock} get<@$k_i$@>(std::forward<@$\tcode{T}_i$@>(@$\tcode{tp}_i$@)) \end{codeblock} for each valid $k_i$ and each group $\tcode{e}_i$ in order. \pnum \begin{note} An implementation may support additional types in the parameter pack \tcode{Tuples} that support the \tcode{tuple}-like protocol, such as \tcode{pair} and \tcode{array}. \end{note} \end{itemdescr} \rSec3[tuple.apply]{Calling a function with a \tcode{tuple} of arguments} \indexlibrary{\idxcode{apply}}% \begin{itemdecl} template constexpr decltype(auto) apply(F&& f, Tuple&& t); \end{itemdecl} \begin{itemdescr} \pnum \effects Given the exposition-only function: \begin{codeblock} template constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence) { // exposition only return @\placeholdernc{INVOKE}@(std::forward(f), std::get(std::forward(t))...); } \end{codeblock} Equivalent to: \begin{codeblock} return apply_impl(std::forward(f), std::forward(t), make_index_sequence>>{}); \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{make_from_tuple}}% \begin{itemdecl} template constexpr T make_from_tuple(Tuple&& t); \end{itemdecl} \begin{itemdescr} \pnum \effects Given the exposition-only function: \begin{codeblock} template constexpr T make_from_tuple_impl(Tuple&& t, index_sequence) { // exposition only return T(get(std::forward(t))...); } \end{codeblock} Equivalent to: \begin{codeblock} return make_from_tuple_impl(forward(t), make_index_sequence>>{}); \end{codeblock} \begin{note} The type of \tcode{T} must be supplied as an explicit template parameter, as it cannot be deduced from the argument list. \end{note} \end{itemdescr} \rSec3[tuple.helper]{Tuple helper classes} \indexlibrary{\idxcode{tuple_size}!in general}% \begin{itemdecl} template struct tuple_size; \end{itemdecl} \begin{itemdescr} \pnum \remarks All specializations of \tcode{tuple_size} shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}) with a base characteristic of \tcode{integral_constant} for some \tcode{N}. \end{itemdescr} \indexlibrary{\idxcode{tuple_size}}% \begin{itemdecl} template class tuple_size> : public integral_constant { }; \end{itemdecl} \indexlibrary{\idxcode{tuple_element}}% \begin{itemdecl} template class tuple_element> { public: using type = TI; }; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. The program is ill-formed if \tcode{I} is out of bounds. \pnum \ctype \tcode{TI} is the type of the \tcode{I}th element of \tcode{Types}, where indexing is zero-based. \end{itemdescr} \indexlibrary{\idxcode{tuple_size}}% \begin{itemdecl} template class tuple_size; template class tuple_size; template class tuple_size; \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{\placeholder{TS}} denote \tcode{tuple_size} of the \cv-unqualified type \tcode{T}. If the expression \tcode{\placeholder{TS}::value} is well-formed when treated as an unevaluated operand, then each of the three templates shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}) with a base characteristic of \begin{codeblock} integral_constant \end{codeblock} Otherwise, they shall have no member \tcode{value}. \pnum Access checking is performed as if in a context unrelated to \tcode{\placeholder{TS}} and \tcode{T}. Only the validity of the immediate context of the expression is considered. \begin{note} The compilation of the expression can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} \pnum In addition to being available via inclusion of the \tcode{} header, the three templates are available when either of the headers \tcode{} or \tcode{} are included. \end{itemdescr} \indexlibrary{\idxcode{tuple_element}}% \begin{itemdecl} template class tuple_element; template class tuple_element; template class tuple_element; \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{\placeholder{TE}} denote \tcode{tuple_element_t} of the \cv-unqualified type \tcode{T}. Then each of the three templates shall meet the \tcode{TransformationTrait} requirements~(\ref{meta.rqmts}) with a member typedef \tcode{type} that names the following type: \begin{itemize} \item for the first specialization, \tcode{add_const_t<\placeholder{TE}>}, \item for the second specialization, \tcode{add_volatile_t<\placeholder{TE}>}, and \item for the third specialization, \tcode{add_cv_t<\placeholder{TE}>}. \end{itemize} \pnum In addition to being available via inclusion of the \tcode{} header, the three templates are available when either of the headers \tcode{} or \tcode{} are included. \end{itemdescr} \rSec3[tuple.elem]{Element access} \indexlibrarymember{get}{tuple}% \begin{itemdecl} template constexpr tuple_element_t>& get(tuple& t) noexcept; template constexpr tuple_element_t>&& get(tuple&& t) noexcept; // Note A template constexpr const tuple_element_t>& get(const tuple& t) noexcept; // Note B template constexpr const tuple_element_t>&& get(const tuple&& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. The program is ill-formed if \tcode{I} is out of bounds. \pnum \returns A reference to the \tcode{I}th element of \tcode{t}, where indexing is zero-based. \pnum \begin{note}[Note A] If a \tcode{T} in \tcode{Types} is some reference type \tcode{X\&}, the return type is \tcode{X\&}, not \tcode{X\&\&}. However, if the element type is a non-reference type \tcode{T}, the return type is \tcode{T\&\&}. \end{note} \pnum \begin{note}[Note B] Constness is shallow. If a \tcode{T} in \tcode{Types} is some reference type \tcode{X\&}, the return type is \tcode{X\&}, not \tcode{const X\&}. However, if the element type is a non-reference type \tcode{T}, the return type is \tcode{const T\&}. This is consistent with how constness is defined to work for member variables of reference type. \end{note} \end{itemdescr} \indexlibrarymember{get}{tuple}% \begin{itemdecl} template constexpr T& get(tuple& t) noexcept; template constexpr T&& get(tuple&& t) noexcept; template constexpr const T& get(const tuple& t) noexcept; template constexpr const T&& get(const tuple&& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The type \tcode{T} occurs exactly once in \tcode{Types...}. Otherwise, the program is ill-formed. \pnum \returns A reference to the element of \tcode{t} corresponding to the type \tcode{T} in \tcode{Types...}. \pnum \begin{example} \begin{codeblock} const tuple t(1, 2, 3.4, 5.6); const int& i1 = get(t); // OK. Not ambiguous. \tcode{i1 == 1} const int& i2 = get(t); // OK. Not ambiguous. \tcode{i2 == 2} const double& d = get(t); // ERROR. ill-formed \end{codeblock} \end{example} \end{itemdescr} \pnum \begin{note} The reason \tcode{get} is a non-member function is that if this functionality had been provided as a member function, code where the type depended on a template parameter would have required using the \tcode{template} keyword. \end{note} \rSec3[tuple.rel]{Relational operators} \indexlibrarymember{operator==}{tuple}% \begin{itemdecl} template constexpr bool operator==(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum \requires For all \tcode{i}, where \tcode{0 <= i} and \tcode{i < sizeof...(TTypes)}, \tcode{get(t) == get(u)} is a valid expression returning a type that is convertible to \tcode{bool}. \tcode{sizeof...(TTypes)} \tcode{==} \tcode{sizeof...(UTypes)}. \pnum \returns \tcode{true} if \tcode{get(t) == get(u)} for all \tcode{i}, otherwise \tcode{false}. For any two zero-length tuples \tcode{e} and \tcode{f}, \tcode{e == f} returns \tcode{true}. \pnum \effects The elementary comparisons are performed in order from the zeroth index upwards. No comparisons or element accesses are performed after the first equality comparison that evaluates to \tcode{false}. \end{itemdescr} \indexlibrarymember{operator<}{tuple}% \begin{itemdecl} template constexpr bool operator<(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum \requires For all \tcode{i}, where \tcode{0 <= i} and \tcode{i < sizeof...(TTypes)}, both \tcode{get(t) < get(u)} and \tcode{get(u) < get(t)} are valid expressions returning types that are convertible to \tcode{bool}. \tcode{sizeof...(TTypes)} \tcode{==} \tcode{sizeof...(UTypes)}. \pnum\returns The result of a lexicographical comparison between \tcode{t} and \tcode{u}. The result is defined as: \tcode{(bool)(get<0>(t) < get<0>(u)) || (!(bool)(get<0>(u) < get<0>(t)) \&\& t$_{\mathrm{tail}}$ < u$_{\mathrm{tail}}$)}, where \tcode{r$_{\mathrm{tail}}$} for some tuple \tcode{r} is a tuple containing all but the first element of \tcode{r}. For any two zero-length tuples \tcode{e} and \tcode{f}, \tcode{e < f} returns \tcode{false}. \end{itemdescr} \indexlibrarymember{operator"!=}{tuple}% \begin{itemdecl} template constexpr bool operator!=(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!(t == u)}. \end{itemdescr} \indexlibrarymember{operator>}{tuple}% \begin{itemdecl} template constexpr bool operator>(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{u < t}. \end{itemdescr} \indexlibrarymember{operator<=}{tuple}% \begin{itemdecl} template constexpr bool operator<=(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!(u < t)}. \end{itemdescr} \indexlibrarymember{operator>=}{tuple}% \begin{itemdecl} template constexpr bool operator>=(const tuple& t, const tuple& u); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!(t < u)}. \end{itemdescr} \pnum \begin{note} The above definitions for comparison functions do not require \tcode{t$_{\mathrm{tail}}$} (or \tcode{u$_{\mathrm{tail}}$}) to be constructed. It may not even be possible, as \tcode{t} and \tcode{u} are not required to be copy constructible. Also, all comparison functions are short circuited; they do not perform element accesses beyond what is required to determine the result of the comparison. \end{note} \rSec3[tuple.traits]{Tuple traits} \indexlibrary{\idxcode{uses_allocator}}% \begin{itemdecl} template struct uses_allocator, Alloc> : true_type { }; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{Alloc} shall be an \tcode{Allocator}~(\ref{allocator.requirements}). \pnum \begin{note} Specialization of this trait informs other library components that \tcode{tuple} can be constructed with an allocator, even though it does not have a nested \tcode{allocator_type}. \end{note} \end{itemdescr} \rSec3[tuple.special]{Tuple specialized algorithms} \indexlibrary{\idxcode{swap}}% \begin{itemdecl} template void swap(tuple& x, tuple& y) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_swappable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$, where $0 \leq i < \tcode{sizeof...(Types)}$. The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} noexcept(x.swap(y)) \end{codeblock} \pnum \effects As if by \tcode{x.swap(y)}. \end{itemdescr} \rSec1[optional]{Optional objects} \rSec2[optional.general]{In general} \pnum This subclause describes class template \tcode{optional} that represents optional objects. An \defn{optional object} is an object that contains the storage for another object and manages the lifetime of this contained object, if any. The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed. The initialization state of the contained object is tracked by the optional object. \rSec2[optional.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{optional}}% \indexlibrary{\idxhdr{optional}}% \begin{codeblock} namespace std { // \ref{optional.optional}, class template \tcode{optional} template class optional; // \ref{optional.nullopt}, no-value state indicator struct nullopt_t{@\seebelow@}; inline constexpr nullopt_t nullopt(@\unspec@); // \ref{optional.bad.access}, class \tcode{bad_optional_access} class bad_optional_access; // \ref{optional.relops}, relational operators template constexpr bool operator==(const optional&, const optional&); template constexpr bool operator!=(const optional&, const optional&); template constexpr bool operator<(const optional&, const optional&); template constexpr bool operator>(const optional&, const optional&); template constexpr bool operator<=(const optional&, const optional&); template constexpr bool operator>=(const optional&, const optional&); // \ref{optional.nullops}, comparison with \tcode{nullopt} template constexpr bool operator==(const optional&, nullopt_t) noexcept; template constexpr bool operator==(nullopt_t, const optional&) noexcept; template constexpr bool operator!=(const optional&, nullopt_t) noexcept; template constexpr bool operator!=(nullopt_t, const optional&) noexcept; template constexpr bool operator<(const optional&, nullopt_t) noexcept; template constexpr bool operator<(nullopt_t, const optional&) noexcept; template constexpr bool operator<=(const optional&, nullopt_t) noexcept; template constexpr bool operator<=(nullopt_t, const optional&) noexcept; template constexpr bool operator>(const optional&, nullopt_t) noexcept; template constexpr bool operator>(nullopt_t, const optional&) noexcept; template constexpr bool operator>=(const optional&, nullopt_t) noexcept; template constexpr bool operator>=(nullopt_t, const optional&) noexcept; // \ref{optional.comp_with_t}, comparison with \tcode{T} template constexpr bool operator==(const optional&, const U&); template constexpr bool operator==(const U&, const optional&); template constexpr bool operator!=(const optional&, const U&); template constexpr bool operator!=(const U&, const optional&); template constexpr bool operator<(const optional&, const U&); template constexpr bool operator<(const U&, const optional&); template constexpr bool operator<=(const optional&, const U&); template constexpr bool operator<=(const U&, const optional&); template constexpr bool operator>(const optional&, const U&); template constexpr bool operator>(const U&, const optional&); template constexpr bool operator>=(const optional&, const U&); template constexpr bool operator>=(const U&, const optional&); // \ref{optional.specalg}, specialized algorithms template void swap(optional&, optional&) noexcept(@\seebelow@); template constexpr optional<@\seebelow@> make_optional(T&&); template constexpr optional make_optional(Args&&... args); template constexpr optional make_optional(initializer_list il, Args&&... args); // \ref{optional.hash}, hash support template struct hash; template struct hash>; } \end{codeblock} \pnum A program that necessitates the instantiation of template \tcode{optional} for a reference type, or for possibly cv-qualified types \tcode{in_place_t} or \tcode{nullopt_t} is ill-formed. \rSec2[optional.optional]{Class template \tcode{optional}} \indexlibrary{\idxcode{optional}}% \begin{codeblock} template class optional { public: using value_type = T; // \ref{optional.ctor}, constructors constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept; constexpr optional(const optional&); constexpr optional(optional&&) noexcept(@\seebelow@); template constexpr explicit optional(in_place_t, Args&&...); template constexpr explicit optional(in_place_t, initializer_list, Args&&...); template @\EXPLICIT@ constexpr optional(U&&); template @\EXPLICIT@ optional(const optional&); template @\EXPLICIT@ optional(optional&&); // \ref{optional.dtor}, destructor ~optional(); // \ref{optional.assign}, assignment optional& operator=(nullopt_t) noexcept; optional& operator=(const optional&); optional& operator=(optional&&) noexcept(@\seebelow@); template optional& operator=(U&&); template optional& operator=(const optional&); template optional& operator=(optional&&); template T& emplace(Args&&...); template T& emplace(initializer_list, Args&&...); // \ref{optional.swap}, swap void swap(optional&) noexcept(@\seebelow@); // \ref{optional.observe}, observers constexpr const T* operator->() const; constexpr T* operator->(); constexpr const T& operator*() const&; constexpr T& operator*() &; constexpr T&& operator*() &&; constexpr const T&& operator*() const&&; constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr const T& value() const&; constexpr T& value() &; constexpr T&& value() &&; constexpr const T&& value() const&&; template constexpr T value_or(U&&) const&; template constexpr T value_or(U&&) &&; // \ref{optional.mod}, modifiers void reset() noexcept; private: T *val; // \expos }; template optional(T) -> optional; \end{codeblock} \pnum Any instance of \tcode{optional} at any given time either contains a value or does not contain a value. When an instance of \tcode{optional} \defnx{contains a value}{contains a value!\idxcode{optional}}, it means that an object of type \tcode{T}, referred to as the optional object's \defnx{contained value}{contained value!\idxcode{optional}}, is allocated within the storage of the optional object. Implementations are not permitted to use additional storage, such as dynamic memory, to allocate its contained value. The contained value shall be allocated in a region of the \tcode{optional} storage suitably aligned for the type \tcode{T}. When an object of type \tcode{optional} is contextually converted to \tcode{bool}, the conversion returns \tcode{true} if the object contains a value; otherwise the conversion returns \tcode{false}. \pnum Member \tcode{val} is provided for exposition only. When an \tcode{optional} object contains a value, \tcode{val} points to the contained value. \pnum \tcode{T} shall be an object type and shall satisfy the requirements of \tcode{Destructible} (Table~\ref{tab:destructible}). \rSec3[optional.ctor]{Constructors} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \postconditions \tcode{*this} does not contain a value. \pnum \remarks No contained value is initialized. For every object type \tcode{T} these constructors shall be constexpr constructors (\ref{dcl.constexpr}). \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} constexpr optional(const optional& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{rhs} contains a value, initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{*rhs}. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks This constructor shall be defined as deleted unless \tcode{is_copy_constructible_v} is \tcode{true}. If \tcode{is_trivially_copy_constructible_v} is \tcode{true}, this constructor shall be a \tcode{constexpr} constructor. \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} constexpr optional(optional&& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{rhs} contains a value, initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{std::move(*rhs)}. \tcode{bool(rhs)} is unchanged. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to \tcode{is_nothrow_move_constructible_v}. This constructor shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true}. If \tcode{is_trivially_move_constructible_v} is \tcode{true}, this constructor shall be a \tcode{constexpr} constructor. \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} template constexpr explicit optional(in_place_t, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks If \tcode{T}'s constructor selected for the initialization is a constexpr constructor, this constructor shall be a constexpr constructor. This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} template constexpr explicit optional(in_place_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v\&, Args\&\&...>} is \tcode{true}. If \tcode{T}'s constructor selected for the initialization is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \pnum \begin{note} The following constructors are conditionally specified as explicit. This is typically implemented by declaring two such constructors, of which at most one participates in overload resolution. \end{note} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} template @\EXPLICIT@ constexpr optional(U&& v); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{std::forward(v)}. \pnum \postconditions \tcode{*this} contains a value. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks If \tcode{T}'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}, \tcode{is_same_v, in_place_t>} is \tcode{false}, and \tcode{is_same_v, decay_t>} is \tcode{false}. The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} template @\EXPLICIT@ optional(const optional& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{rhs} contains a value, initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{*rhs}. \pnum \postconditions \tcode{bool(rhs)} == \tcode{bool(*this)}. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks This constructor shall not participate in overload resolution unless \begin{itemize} \item \tcode{is_constructible_v} is \tcode{true}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, and \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}. \end{itemize} The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{optional}!constructor}% \begin{itemdecl} template @\EXPLICIT@ optional(optional&& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{rhs} contains a value, initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{std::move(*rhs)}. \tcode{bool(rhs)} is unchanged. \pnum \postconditions \tcode{bool(rhs)} == \tcode{bool(*this)}. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks This constructor shall not participate in overload resolution unless \begin{itemize} \item \tcode{is_constructible_v} is true, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, and \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}. \end{itemize} The constructor is explicit if and only if \tcode{is_convertible_v} is \tcode{false}. \end{itemdescr} \rSec3[optional.dtor]{Destructor} \indexlibrary{\idxcode{optional}!destructor}% \begin{itemdecl} ~optional(); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{is_trivially_destructible_v != true} and \tcode{*this} contains a value, calls \begin{codeblock} val->T::~T() \end{codeblock} \pnum \remarks If \tcode{is_trivially_destructible_v == true} then this destructor shall be a trivial destructor. \end{itemdescr} \rSec3[optional.assign]{Assignment} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} optional& operator=(nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{*this} contains a value, calls \tcode{val->T::\~T()} to destroy the contained value; otherwise no effect. \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{*this} does not contain a value. \end{itemdescr} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} optional& operator=(const optional& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects See Table~\ref{tab:optional.assign.copy}. \begin{lib2dtab2}{\tcode{optional::operator=(const optional\&)} effects}{tab:optional.assign.copy} {\tcode{*this} contains a value} {\tcode{*this} does not contain a value} \rowhdr{\tcode{rhs} contains a value} & assigns \tcode{*rhs} to the contained value & initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with \tcode{*rhs} \\ \rowsep \rowhdr{\tcode{rhs} does not contain a value} & destroys the contained value by calling \tcode{val->T::\~T()} & no effect \\ \end{lib2dtab2} \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \remarks If any exception is thrown, the result of the expression \tcode{bool(*this)} remains unchanged. If an exception is thrown during the call to \tcode{T}'s copy constructor, no effect. If an exception is thrown during the call to \tcode{T}'s copy assignment, the state of its contained value is as defined by the exception safety guarantee of \tcode{T}'s copy assignment. This operator shall be defined as deleted unless \tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_copy_assignable_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} optional& operator=(optional&& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects See Table~\ref{tab:optional.assign.move}. The result of the expression \tcode{bool(rhs)} remains unchanged. \begin{lib2dtab2}{\tcode{optional::operator=(optional\&\&)} effects}{tab:optional.assign.move} {\tcode{*this} contains a value} {\tcode{*this} does not contain a value} \rowhdr{\tcode{rhs} contains a value} & assigns \tcode{std::move(*rhs)} to the contained value & initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with \tcode{std::move(*rhs)} \\ \rowsep \rowhdr{\tcode{rhs} does not contain a value} & destroys the contained value by calling \tcode{val->T::\~T()} & no effect \\ \end{lib2dtab2} \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_move_assignable_v && is_nothrow_move_constructible_v \end{codeblock} \pnum If any exception is thrown, the result of the expression \tcode{bool(*this)} remains unchanged. If an exception is thrown during the call to \tcode{T}'s move constructor, the state of \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s move constructor. If an exception is thrown during the call to \tcode{T}'s move assignment, the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s move assignment. This operator shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true} and \tcode{is_move_assignable_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} template optional& operator=(U&& v); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{*this} contains a value, assigns \tcode{std::forward(v)} to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type \tcode{T} with \tcode{std::forward(v)}. \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{*this} contains a value. \pnum \remarks If any exception is thrown, the result of the expression \tcode{bool(*this)} remains unchanged. If an exception is thrown during the call to \tcode{T}'s constructor, the state of \tcode{v} is determined by the exception safety guarantee of \tcode{T}'s constructor. If an exception is thrown during the call to \tcode{T}'s assignment, the state of \tcode{*val} and \tcode{v} is determined by the exception safety guarantee of \tcode{T}'s assignment. This function shall not participate in overload resolution unless \tcode{is_same_v, decay_t>} is \tcode{false}, \tcode{conjunction_v, is_same>>} is \tcode{false}, \tcode{is_constructible_v} is \tcode{true}, and \tcode{is_assignable_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} template optional& operator=(const optional& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects See Table~\ref{tab:optional.assign.copy.templ}. \begin{lib2dtab2}{\tcode{optional::operator=(const optional\&)} effects}{tab:optional.assign.copy.templ} {\tcode{*this} contains a value} {\tcode{*this} does not contain a value} \rowhdr{\tcode{rhs} contains a value} & assigns \tcode{*rhs} to the contained value & initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with \tcode{*rhs} \\ \rowsep \rowhdr{\tcode{rhs} does not contain a value} & destroys the contained value by calling \tcode{val->T::\~T()} & no effect \\ \end{lib2dtab2} \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \remarks If any exception is thrown, the result of the expression \tcode{bool(*this)} remains unchanged. If an exception is thrown during the call to \tcode{T}'s constructor, the state of \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s constructor. If an exception is thrown during the call to \tcode{T}'s assignment, the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s assignment. This function shall not participate in overload resolution unless \begin{itemize} \item \tcode{is_constructible_v} is \tcode{true}, \item \tcode{is_assignable_v} is \tcode{true}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_assignable_v\&>} is \tcode{false}, \item \tcode{is_assignable_v\&\&>} is \tcode{false}, \item \tcode{is_assignable_v\&>} is \tcode{false}, and \item \tcode{is_assignable_v\&\&>} is \tcode{false}. \end{itemize} \end{itemdescr} \indexlibrarymember{operator=}{optional}% \begin{itemdecl} template optional& operator=(optional&& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects See Table~\ref{tab:optional.assign.move.templ}. The result of the expression \tcode{bool(rhs)} remains unchanged. \begin{lib2dtab2}{\tcode{optional::operator=(optional\&\&)} effects}{tab:optional.assign.move.templ} {\tcode{*this} contains a value} {\tcode{*this} does not contain a value} \rowhdr{\tcode{rhs} contains a value} & assigns \tcode{std::move(*rhs)} to the contained value & initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with \tcode{std::move(*rhs)} \\ \rowsep \rowhdr{\tcode{rhs} does not contain a value} & destroys the contained value by calling \tcode{val->T::\~T()} & no effect \\ \end{lib2dtab2} \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{bool(rhs) == bool(*this)}. \pnum \remarks If any exception is thrown, the result of the expression \tcode{bool(*this)} remains unchanged. If an exception is thrown during the call to \tcode{T}'s constructor, the state of \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s constructor. If an exception is thrown during the call to \tcode{T}'s assignment, the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s assignment. This function shall not participate in overload resolution unless \begin{itemize} \item \tcode{is_constructible_v} is \tcode{true}, \item \tcode{is_assignable_v} is \tcode{true}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_constructible_v\&>} is \tcode{false}, \item \tcode{is_constructible_v\&\&>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&, T>} is \tcode{false}, \item \tcode{is_convertible_v\&\&, T>} is \tcode{false}, \item \tcode{is_assignable_v\&>} is \tcode{false}, \item \tcode{is_assignable_v\&\&>} is \tcode{false}, \item \tcode{is_assignable_v\&>} is \tcode{false}, and \item \tcode{is_assignable_v\&\&>} is \tcode{false}. \end{itemize} \end{itemdescr} \indexlibrarymember{emplace}{optional}% \begin{itemdecl} template T& emplace(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_constructible_v} is \tcode{true}. \pnum \effects Calls \tcode{*this = nullopt}. Then initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks If an exception is thrown during the call to \tcode{T}'s constructor, \tcode{*this} does not contain a value, and the previous \tcode{*val} (if any) has been destroyed. \end{itemdescr} \indexlibrarymember{emplace}{optional}% \begin{itemdecl} template T& emplace(initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{*this = nullopt}. Then initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown by the selected constructor of \tcode{T}. \pnum \remarks If an exception is thrown during the call to \tcode{T}'s constructor, \tcode{*this} does not contain a value, and the previous \tcode{*val} (if any) has been destroyed. This function shall not participate in overload resolution unless \tcode{is_constructible_v\&, Args\&\&...>} is \tcode{true}. \end{itemdescr} \rSec3[optional.swap]{Swap} \indexlibrarymember{swap}{optional}% \begin{itemdecl} void swap(optional& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \requires Lvalues of type \tcode{T} shall be swappable and \tcode{is_move_constructible_v} is \tcode{true}. \pnum \effects See Table~\ref{tab:optional.swap}. \begin{lib2dtab2}{\tcode{optional::swap(optional\&)} effects}{tab:optional.swap} {\tcode{*this} contains a value} {\tcode{*this} does not contain a value} \rowhdr{\tcode{rhs} contains a value} & calls \tcode{swap(*(*this), *rhs)} & initializes the contained value of \tcode{*this} as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{std::move(*rhs)}, followed by \tcode{rhs.val->T::\~T()}; postcondition is that \tcode{*this} contains a value and \tcode{rhs} does not contain a value \\ \rowsep \rowhdr{\tcode{rhs} does not contain a value} & initializes the contained value of \tcode{rhs} as if direct-non-list-initializing an object of type \tcode{T} with the expression \tcode{std::move(*(*this))}, followed by \tcode{val->T::\~T()}; postcondition is that \tcode{*this} does not contain a value and \tcode{rhs} contains a value & no effect \\ \end{lib2dtab2} \pnum \throws Any exceptions thrown by the operations in the relevant part of Table~\ref{tab:optional.swap}. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_move_constructible_v && is_nothrow_swappable_v \end{codeblock} If any exception is thrown, the results of the expressions \tcode{bool(*this)} and \tcode{bool(rhs)} remain unchanged. If an exception is thrown during the call to function \tcode{swap}, the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{swap} for lvalues of \tcode{T}. If an exception is thrown during the call to \tcode{T}'s move constructor, the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s move constructor. \end{itemdescr} \rSec3[optional.observe]{Observers} \indexlibrarymember{operator->}{optional}% \begin{itemdecl} constexpr const T* operator->() const; constexpr T* operator->(); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{*this} contains a value. \pnum \returns \tcode{val}. \pnum \throws Nothing. \pnum \remarks These functions shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator*}{optional}% \begin{itemdecl} constexpr const T& operator*() const&; constexpr T& operator*() &; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{*this} contains a value. \pnum \returns \tcode{*val}. \pnum \throws Nothing. \pnum \remarks These functions shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator*}{optional}% \begin{itemdecl} constexpr T&& operator*() &&; constexpr const T&& operator*() const&&; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{*this} contains a value. \pnum \effects Equivalent to: \tcode{return std::move(*val);} \end{itemdescr} \indexlibrarymember{operator bool}{optional}% \begin{itemdecl} constexpr explicit operator bool() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if and only if \tcode{*this} contains a value. \pnum \remarks This function shall be a constexpr function. \end{itemdescr} \indexlibrarymember{has_value}{optional}% \begin{itemdecl} constexpr bool has_value() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if and only if \tcode{*this} contains a value. \pnum \remarks This function shall be a constexpr function. \end{itemdescr} \indexlibrarymember{value}{optional}% \begin{itemdecl} constexpr const T& value() const&; constexpr T& value() &; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return bool(*this) ? *val : throw bad_optional_access(); \end{codeblock} \end{itemdescr} \indexlibrarymember{value}{optional}% \begin{itemdecl} constexpr T&& value() &&; constexpr const T&& value() const&&; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return bool(*this) ? std::move(*val) : throw bad_optional_access(); \end{codeblock} \end{itemdescr} \indexlibrarymember{value_or}{optional}% \begin{itemdecl} template constexpr T value_or(U&& v) const&; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return bool(*this) ? **this : static_cast(std::forward(v)); \end{codeblock} \pnum \remarks If \tcode{is_copy_constructible_v \&\& is_convertible_v} is \tcode{false}, the program is ill-formed. \end{itemdescr} \indexlibrarymember{value_or}{optional}% \begin{itemdecl} template constexpr T value_or(U&& v) &&; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return bool(*this) ? std::move(**this) : static_cast(std::forward(v)); \end{codeblock} \pnum \remarks If \tcode{is_move_constructible_v \&\& is_convertible_v} is \tcode{false}, the program is ill-formed. \end{itemdescr} \rSec3[optional.mod]{Modifiers} \indexlibrarymember{reset}{optional}% \begin{itemdecl} void reset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{*this} contains a value, calls \tcode{val->T::\~T()} to destroy the contained value; otherwise no effect. \pnum \postconditions \tcode{*this} does not contain a value. \end{itemdescr} \rSec2[optional.nullopt]{No-value state indicator} \indexlibrary{\idxcode{nullopt_t}}% \indexlibrary{\idxcode{nullopt}}% \begin{itemdecl} struct nullopt_t{@\seebelow@}; inline constexpr nullopt_t nullopt(@\unspec@); \end{itemdecl} \pnum The struct \tcode{nullopt_t} is an empty structure type used as a unique type to indicate the state of not containing a value for \tcode{optional} objects. In particular, \tcode{optional} has a constructor with \tcode{nullopt_t} as a single argument; this indicates that an optional object not containing a value shall be constructed. \pnum Type \tcode{nullopt_t} shall not have a default constructor or an initializer-list constructor, and shall not be an aggregate. \rSec2[optional.bad.access]{Class \tcode{bad_optional_access}} \begin{codeblock} class bad_optional_access : public exception { public: bad_optional_access(); }; \end{codeblock} \pnum The class \tcode{bad_optional_access} defines the type of objects thrown as exceptions to report the situation where an attempt is made to access the value of an optional object that does not contain a value. \indexlibrary{\idxcode{bad_optional_access}!constructor}% \indexlibrarymember{what}{bad_optional_access}% \begin{itemdecl} bad_optional_access(); \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of class \tcode{bad_optional_access}. \pnum \postconditions \tcode{what()} returns an \impldef{return value of \tcode{bad_optional_access::what}} \ntbs. \end{itemdescr} \rSec2[optional.relops]{Relational operators} \indexlibrarymember{operator==}{optional}% \begin{itemdecl} template constexpr bool operator==(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x == *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \begin{note} \tcode{T} need not be \tcode{EqualityComparable}. \end{note} \pnum \returns If \tcode{bool(x) != bool(y)}, \tcode{false}; otherwise if \tcode{bool(x) == false}, \tcode{true}; otherwise \tcode{*x == *y}. \pnum \remarks Specializations of this function template for which \tcode{*x == *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator"!=}{optional}% \begin{itemdecl} template constexpr bool operator!=(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x != *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \returns If \tcode{bool(x) != bool(y)}, \tcode{true}; otherwise, if \tcode{bool(x) == false}, \tcode{false}; otherwise \tcode{*x != *y}. \pnum \remarks Specializations of this function template for which \tcode{*x != *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator<}{optional}% \begin{itemdecl} template constexpr bool operator<(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{*x < *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \returns If \tcode{!y}, \tcode{false}; otherwise, if \tcode{!x}, \tcode{true}; otherwise \tcode{*x < *y}. \pnum \remarks Specializations of this function template for which \tcode{*x < *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator>}{optional}% \begin{itemdecl} template constexpr bool operator>(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x > *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \returns If \tcode{!x}, \tcode{false}; otherwise, if \tcode{!y}, \tcode{true}; otherwise \tcode{*x > *y}. \pnum \remarks Specializations of this function template for which \tcode{*x > *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator<=}{optional}% \begin{itemdecl} template constexpr bool operator<=(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x <= *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \returns If \tcode{!x}, \tcode{true}; otherwise, if \tcode{!y}, \tcode{false}; otherwise \tcode{*x <= *y}. \pnum \remarks Specializations of this function template for which \tcode{*x <= *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \indexlibrarymember{operator>=}{optional}% \begin{itemdecl} template constexpr bool operator>=(const optional& x, const optional& y); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x >= *y} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \returns If \tcode{!y}, \tcode{true}; otherwise, if \tcode{!x}, \tcode{false}; otherwise \tcode{*x >= *y}. \pnum \remarks Specializations of this function template for which \tcode{*x >= *y} is a core constant expression shall be constexpr functions. \end{itemdescr} \rSec2[optional.nullops]{Comparison with \tcode{nullopt}} \indexlibrarymember{operator==}{optional}% \begin{itemdecl} template constexpr bool operator==(const optional& x, nullopt_t) noexcept; template constexpr bool operator==(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!x}. \end{itemdescr} \indexlibrarymember{operator"!=}{optional}% \begin{itemdecl} template constexpr bool operator!=(const optional& x, nullopt_t) noexcept; template constexpr bool operator!=(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bool(x)}. \end{itemdescr} \indexlibrarymember{operator<}{optional}% \begin{itemdecl} template constexpr bool operator<(const optional& x, nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{false}. \end{itemdescr} \indexlibrarymember{operator<}{optional}% \begin{itemdecl} template constexpr bool operator<(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bool(x)}. \end{itemdescr} \indexlibrarymember{operator<=}{optional}% \begin{itemdecl} template constexpr bool operator<=(const optional& x, nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!x}. \end{itemdescr} \indexlibrarymember{operator<=}{optional}% \begin{itemdecl} template constexpr bool operator<=(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true}. \end{itemdescr} \indexlibrarymember{operator>}{optional}% \begin{itemdecl} template constexpr bool operator>(const optional& x, nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bool(x)}. \end{itemdescr} \indexlibrarymember{operator>}{optional}% \begin{itemdecl} template constexpr bool operator>(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{false}. \end{itemdescr} \indexlibrarymember{operator>=}{optional}% \begin{itemdecl} template constexpr bool operator>=(const optional& x, nullopt_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true}. \end{itemdescr} \indexlibrarymember{operator>=}{optional}% \begin{itemdecl} template constexpr bool operator>=(nullopt_t, const optional& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!x}. \end{itemdescr} \rSec2[optional.comp_with_t]{Comparison with \tcode{T}} \indexlibrarymember{operator==}{optional}% \begin{itemdecl} template constexpr bool operator==(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x == v} shall be well-formed and its result shall be convertible to \tcode{bool}. \begin{note} \tcode{T} need not be \tcode{EqualityComparable}. \end{note} \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x == v :\ false;} \end{itemdescr} \indexlibrarymember{operator==}{optional}% \begin{itemdecl} template constexpr bool operator==(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v == *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v == *x :\ false;} \end{itemdescr} \indexlibrarymember{operator"!=}{optional}% \begin{itemdecl} template constexpr bool operator!=(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x != v} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x != v :\ true;} \end{itemdescr} \indexlibrarymember{operator"!=}{optional}% \begin{itemdecl} template constexpr bool operator!=(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v != *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v != *x :\ true;} \end{itemdescr} \indexlibrarymember{operator<}{optional}% \begin{itemdecl} template constexpr bool operator<(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x < v} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x < v :\ true;} \end{itemdescr} \indexlibrarymember{operator<}{optional}% \begin{itemdecl} template constexpr bool operator<(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v < *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v < *x :\ false;} \end{itemdescr} \indexlibrarymember{operator<=}{optional}% \begin{itemdecl} template constexpr bool operator<=(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x <= v} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x <= v :\ true;} \end{itemdescr} \indexlibrarymember{operator<=}{optional}% \begin{itemdecl} template constexpr bool operator<=(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v <= *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v <= *x :\ false;} \end{itemdescr} \indexlibrarymember{operator>}{optional}% \begin{itemdecl} template constexpr bool operator>(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x > v} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x > v :\ false;} \end{itemdescr} \indexlibrarymember{operator>}{optional}% \begin{itemdecl} template constexpr bool operator>(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v > *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v > *x :\ true;} \end{itemdescr} \indexlibrarymember{operator>=}{optional}% \begin{itemdecl} template constexpr bool operator>=(const optional& x, const U& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{*x >= v} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ *x >= v :\ false;} \end{itemdescr} \indexlibrarymember{operator>=}{optional}% \begin{itemdecl} template constexpr bool operator>=(const U& v, const optional& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{v >= *x} shall be well-formed and its result shall be convertible to \tcode{bool}. \pnum \effects Equivalent to: \tcode{return bool(x) ?\ v >= *x :\ true;} \end{itemdescr} \rSec2[optional.specalg]{Specialized algorithms} \indexlibrary{\idxcode{swap}!\idxcode{optional}}% \begin{itemdecl} template void swap(optional& x, optional& y) noexcept(noexcept(x.swap(y))); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{x.swap(y)}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true} and \tcode{is_swappable_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{make_optional}}% \begin{itemdecl} template constexpr optional> make_optional(T&& v); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{optional>(std::forward(v))}. \end{itemdescr} \indexlibrary{\idxcode{make_optional}}% \begin{itemdecl} template constexpr optional make_optional(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return optional(in_place, std::forward(args)...);} \end{itemdescr} \indexlibrary{\idxcode{make_optional}}% \begin{itemdecl} template constexpr optional make_optional(initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return optional(in_place, il, std::forward(args)...);} \end{itemdescr} \rSec2[optional.hash]{Hash support} \indexlibrary{\idxcode{hash}!\idxcode{optional}}% \begin{itemdecl} template struct hash>; \end{itemdecl} \begin{itemdescr} \pnum The specialization \tcode{hash>} is enabled~(\ref{unord.hash}) if and only if \tcode{hash>} is enabled. When enabled, for an object \tcode{o} of type \tcode{optional}, if \tcode{bool(o) == true}, then \tcode{hash>()(o)} shall evaluate to the same value as \tcode{hash>()(*o)}; otherwise it evaluates to an unspecified value. The member functions are not guaranteed to be \tcode{noexcept}. \end{itemdescr} \rSec1[variant]{Variants} \rSec2[variant.general]{In general} \pnum A variant object holds and manages the lifetime of a value. If the \tcode{variant} holds a value, that value's type has to be one of the template argument types given to variant. These template arguments are called alternatives. \rSec2[variant.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{variant}}% \indexlibrary{\idxhdr{variant}}% \begin{codeblock} namespace std { // \ref{variant.variant}, class template \tcode{variant} template class variant; // \ref{variant.helper}, variant helper classes template struct variant_size; // not defined template struct variant_size; template struct variant_size; template struct variant_size; template inline constexpr size_t variant_size_v = variant_size::value; template struct variant_size>; template struct variant_alternative; // not defined template struct variant_alternative; template struct variant_alternative; template struct variant_alternative; template using variant_alternative_t = typename variant_alternative::type; template struct variant_alternative>; inline constexpr size_t variant_npos = -1; // \ref{variant.get}, value access template constexpr bool holds_alternative(const variant&) noexcept; template constexpr variant_alternative_t>& get(variant&); template constexpr variant_alternative_t>&& get(variant&&); template constexpr const variant_alternative_t>& get(const variant&); template constexpr const variant_alternative_t>&& get(const variant&&); template constexpr T& get(variant&); template constexpr T&& get(variant&&); template constexpr const T& get(const variant&); template constexpr const T&& get(const variant&&); template constexpr add_pointer_t>> get_if(variant*) noexcept; template constexpr add_pointer_t>> get_if(const variant*) noexcept; template constexpr add_pointer_t get_if(variant*) noexcept; template constexpr add_pointer_t get_if(const variant*) noexcept; // \ref{variant.relops}, relational operators template constexpr bool operator==(const variant&, const variant&); template constexpr bool operator!=(const variant&, const variant&); template constexpr bool operator<(const variant&, const variant&); template constexpr bool operator>(const variant&, const variant&); template constexpr bool operator<=(const variant&, const variant&); template constexpr bool operator>=(const variant&, const variant&); // \ref{variant.visit}, visitation template constexpr @\seebelow@ visit(Visitor&&, Variants&&...); // \ref{variant.monostate}, class \tcode{monostate} struct monostate; // \ref{variant.monostate.relops}, \tcode{monostate} relational operators constexpr bool operator<(monostate, monostate) noexcept; constexpr bool operator>(monostate, monostate) noexcept; constexpr bool operator<=(monostate, monostate) noexcept; constexpr bool operator>=(monostate, monostate) noexcept; constexpr bool operator==(monostate, monostate) noexcept; constexpr bool operator!=(monostate, monostate) noexcept; // \ref{variant.specalg}, specialized algorithms template void swap(variant&, variant&) noexcept(@\seebelow@); // \ref{variant.bad.access}, class \tcode{bad_variant_access} class bad_variant_access; // \ref{variant.hash}, hash support template struct hash; template struct hash>; template <> struct hash; // \ref{variant.traits}, allocator-related traits template struct uses_allocator; template struct uses_allocator, Alloc>; } \end{codeblock} \indexlibrary{\idxcode{variant}}% \rSec2[variant.variant]{Class template \tcode{variant}} \begin{codeblock} namespace std { template class variant { public: // \ref{variant.ctor}, constructors constexpr variant() noexcept(@\seebelow@); variant(const variant&); variant(variant&&) noexcept(@\seebelow@); template constexpr variant(T&&) noexcept(@\seebelow@); template constexpr explicit variant(in_place_type_t, Args&&...); template constexpr explicit variant(in_place_type_t, initializer_list, Args&&...); template constexpr explicit variant(in_place_index_t, Args&&...); template constexpr explicit variant(in_place_index_t, initializer_list, Args&&...); // allocator-extended constructors template variant(allocator_arg_t, const Alloc&); template variant(allocator_arg_t, const Alloc&, const variant&); template variant(allocator_arg_t, const Alloc&, variant&&); template variant(allocator_arg_t, const Alloc&, T&&); template variant(allocator_arg_t, const Alloc&, in_place_type_t, Args&&...); template variant(allocator_arg_t, const Alloc&, in_place_type_t, initializer_list, Args&&...); template variant(allocator_arg_t, const Alloc&, in_place_index_t, Args&&...); template variant(allocator_arg_t, const Alloc&, in_place_index_t, initializer_list, Args&&...); // \ref{variant.dtor}, destructor ~variant(); // \ref{variant.assign}, assignment variant& operator=(const variant&); variant& operator=(variant&&) noexcept(@\seebelow@); template variant& operator=(T&&) noexcept(@\seebelow@); // \ref{variant.mod}, modifiers template T& emplace(Args&&...); template T& emplace(initializer_list, Args&&...); template variant_alternative_t>& emplace(Args&&...); template variant_alternative_t>& emplace(initializer_list, Args&&...); // \ref{variant.status}, value status constexpr bool valueless_by_exception() const noexcept; constexpr size_t index() const noexcept; // \ref{variant.swap}, swap void swap(variant&) noexcept(@\seebelow@); }; } \end{codeblock} \pnum Any instance of \tcode{variant} at any given time either holds a value of one of its alternative types, or it holds no value. When an instance of \tcode{variant} holds a value of alternative type \tcode{T}, it means that a value of type \tcode{T}, referred to as the \tcode{variant} object's \defnx{contained value}{contained value!\idxcode{variant}}, is allocated within the storage of the \tcode{variant} object. Implementations are not permitted to use additional storage, such as dynamic memory, to allocate the contained value. The contained value shall be allocated in a region of the \tcode{variant} storage suitably aligned for all types in \tcode{Types...}. It is \impldef{whether \tcode{variant} supports over-aligned types} whether over-aligned types are supported. \pnum All types in \tcode{Types...} shall be (possibly cv-qualified) object types that are not arrays. \pnum A program that instantiates the definition of \tcode{variant} with no template arguments is ill-formed. \rSec3[variant.ctor]{Constructors} \pnum In the descriptions that follow, let $i$ be in the range \range{0}{sizeof...(Types)}, and $\tcode{T}_i$ be the $i^\text{th}$ type in \tcode{Types...}. \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} constexpr variant() noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a \tcode{variant} holding a value-initialized value of type $\tcode{T}_0$. \pnum \postconditions \tcode{valueless_by_exception()} is \tcode{false} and \tcode{index()} is \tcode{0}. \pnum \throws Any exception thrown by the value-initialization of $\tcode{T}_0$. \pnum \remarks This function shall be \tcode{constexpr} if and only if the value-initialization of the alternative type $\tcode{T}_0$ would satisfy the requirements for a constexpr function. The expression inside \tcode{noexcept} is equivalent to \tcode{is_nothrow_default_constructible_v<$\tcode{T}_0$>}. This function shall not participate in overload resolution unless \tcode{is_default_constructible_v<$\tcode{T}_0$>} is \tcode{true}. \begin{note} See also class \tcode{monostate}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} variant(const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{w} holds a value, initializes the \tcode{variant} to hold the same alternative as \tcode{w} and direct-initializes the contained value with \tcode{get(w)}, where \tcode{j} is \tcode{w.index()}. Otherwise, initializes the \tcode{variant} to not hold a value. \pnum \throws Any exception thrown by direct-initializing any $\tcode{T}_i$ for all $i$. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_copy_constructible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} variant(variant&& w) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{w} holds a value, initializes the \tcode{variant} to hold the same alternative as \tcode{w} and direct-initializes the contained value with \tcode{get(std::move(w))}, where \tcode{j} is \tcode{w.index()}. Otherwise, initializes the \tcode{variant} to not hold a value. \pnum \throws Any exception thrown by move-constructing any $\tcode{T}_i$ for all $i$. \pnum \remarks The expression inside \tcode{noexcept} is equivalent to the logical AND of \tcode{is_nothrow_move_constructible_v<$\tcode{T}_i$>} for all $i$. This function shall not participate in overload resolution unless \tcode{is_move_constructible_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} template constexpr variant(T&& t) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum Let $\tcode{T}_j$ be a type that is determined as follows: build an imaginary function \tcode{\placeholdernc{FUN}($\tcode{T}_i$)} for each alternative type $\tcode{T}_i$. The overload \tcode{\placeholdernc{FUN}($\tcode{T}_j$)} selected by overload resolution for the expression \tcode{\placeholdernc{FUN}(std::forward(\brk{}t))} defines the alternative $\tcode{T}_j$ which is the type of the contained value after construction. \pnum \effects Initializes \tcode{*this} to hold the alternative type $\tcode{T}_j$ and direct-initializes the contained value as if direct-non-list-initializing it with \tcode{std::forward(t)}. \pnum \postconditions \tcode{holds_alternative<$\tcode{T}_j$>(*this)} is \tcode{true}. \pnum \throws Any exception thrown by the initialization of the selected alternative $\tcode{T}_j$. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_same_v, variant>} is \tcode{false}, unless \tcode{decay_t} is neither a specialization of \tcode{in_place_type_t} nor a specialization of \tcode{in_place_index_t}, unless \tcode{is_constructible_v<$\tcode{T}_j$, T>} is \tcode{true}, and unless the expression \tcode{\placeholdernc{FUN}(}\brk\tcode{std::forward(t))} (with \tcode{\placeholdernc{FUN}} being the above-mentioned set of imaginary functions) is well formed. \pnum \begin{note} \begin{codeblock} variant v("abc"); \end{codeblock} is ill-formed, as both alternative types have an equally viable constructor for the argument. \end{note} \pnum The expression inside \tcode{noexcept} is equivalent to \tcode{is_nothrow_constructible_v<$\tcode{T}_j$, T>}. If $\tcode{T}_j$'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} template constexpr explicit variant(in_place_type_t, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{holds_alternative(*this)} is \tcode{true}. \pnum \throws Any exception thrown by calling the selected constructor of \tcode{T}. \pnum \remarks This function shall not participate in overload resolution unless there is exactly one occurrence of \tcode{T} in \tcode{Types...} and \tcode{is_constructible_v} is \tcode{true}. If \tcode{T}'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} template constexpr explicit variant(in_place_type_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{T} with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{holds_alternative(*this)} is \tcode{true}. \pnum \throws Any exception thrown by calling the selected constructor of \tcode{T}. \pnum \remarks This function shall not participate in overload resolution unless there is exactly one occurrence of \tcode{T} in \tcode{Types...} and \tcode{is_constructible_v\&, Args...>} is \tcode{true}. If \tcode{T}'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} template constexpr explicit variant(in_place_index_t, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type $\tcode{T}_I$ with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{index()} is \tcode{I}. \pnum \throws Any exception thrown by calling the selected constructor of $\tcode{T}_I$. \pnum \remarks This function shall not participate in overload resolution unless \begin{itemize} \item \tcode{I} is less than \tcode{sizeof...(Types)} and \item \tcode{is_constructible_v<$\tcode{T}_I$, Args...>} is \tcode{true}. \end{itemize} If $\tcode{T}_I$'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} template constexpr explicit variant(in_place_index_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type $\tcode{T}_I$ with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{index()} is \tcode{I}. \pnum \remarks This function shall not participate in overload resolution unless \begin{itemize} \item \tcode{I} is less than \tcode{sizeof...(Types)} and \item \tcode{is_constructible_v<$\tcode{T}_I$, initializer_list\&, Args...>} is \tcode{true}. \end{itemize} If $\tcode{T}_I$'s selected constructor is a constexpr constructor, this constructor shall be a constexpr constructor. \end{itemdescr} \indexlibrary{\idxcode{variant}!constructor}% \begin{itemdecl} // allocator-extended constructors template variant(allocator_arg_t, const Alloc& a); template variant(allocator_arg_t, const Alloc& a, const variant& v); template variant(allocator_arg_t, const Alloc& a, variant&& v); template variant(allocator_arg_t, const Alloc& a, T&& t); template variant(allocator_arg_t, const Alloc& a, in_place_type_t, Args&&... args); template variant(allocator_arg_t, const Alloc& a, in_place_type_t, initializer_list il, Args&&... args); template variant(allocator_arg_t, const Alloc& a, in_place_index_t, Args&&... args); template variant(allocator_arg_t, const Alloc& a, in_place_index_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{Alloc} shall meet the requirements for an Allocator~(\ref{allocator.requirements}). \pnum \effects Equivalent to the preceding constructors except that the contained value is constructed with uses-allocator construction~(\ref{allocator.uses.construction}). \end{itemdescr} \rSec3[variant.dtor]{Destructor} \indexlibrary{\idxcode{variant}!destructor}% \begin{itemdecl} ~variant(); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{valueless_by_exception()} is \tcode{false}, destroys the currently contained value. \pnum \remarks If \tcode{is_trivially_destructible_v<$\tcode{T}_i$> == true} for all $\tcode{T}_i$ then this destructor shall be a trivial destructor. \end{itemdescr} \rSec3[variant.assign]{Assignment} \indexlibrarymember{operator=}{variant}% \begin{itemdecl} variant& operator=(const variant& rhs); \end{itemdecl} \begin{itemdescr} \pnum Let $j$ be \tcode{rhs.index()}. \pnum \effects \begin{itemize} \item If neither \tcode{*this} nor \tcode{rhs} holds a value, there is no effect. Otherwise, \item if \tcode{*this} holds a value but \tcode{rhs} does not, destroys the value contained in \tcode{*this} and sets \tcode{*this} to not hold a value. Otherwise, \item if \tcode{index() == $j$}, assigns the value contained in \tcode{rhs} to the value contained in \tcode{*this}. Otherwise, \item if either \tcode{is_nothrow_copy_constructible_v<$\tcode{T}_j$>} or \tcode{!is_nothrow_move_constructible_v<$\tcode{T}_j$>} is \tcode{true}, equivalent to \tcode{emplace<$j$>(get<$j$>(rhs))}. Otherwise, \item equivalent to \tcode{operator=(variant(rhs))}. \end{itemize} \pnum \returns \tcode{*this}. \pnum \postconditions \tcode{index() == rhs.index()}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_copy_constructible_v<$\tcode{T}_i$> \&\&} \tcode{is_copy_assignable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. \end{itemdescr} \indexlibrarymember{operator=}{variant}% \begin{itemdecl} variant& operator=(variant&& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum Let $j$ be \tcode{rhs.index()}. \pnum \effects \begin{itemize} \item If neither \tcode{*this} nor \tcode{rhs} holds a value, there is no effect. Otherwise, \item if \tcode{*this} holds a value but \tcode{rhs} does not, destroys the value contained in \tcode{*this} and sets \tcode{*this} to not hold a value. Otherwise, \item if \tcode{index() == $j$}, assigns \tcode{get<$j$>(std::move(rhs))} to the value contained in \tcode{*this}. Otherwise, \item equivalent to \tcode{emplace<$j$>(get<$j$>(std::move(rhs)))}. \end{itemize} \pnum \returns \tcode{*this}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_move_constructible_v<$\tcode{T}_i$> \&\& is_move_assignable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. The expression inside \tcode{noexcept} is equivalent to: \tcode{is_nothrow_move_constructible_v<$\tcode{T}_i$> \&\& is_nothrow_move_assignable_v<$\tcode{T}_i$>} for all $i$. \begin{itemize} \item If an exception is thrown during the call to $\tcode{T}_j$'s move construction (with $j$ being \tcode{rhs.index())}, the \tcode{variant} will hold no value. \item If an exception is thrown during the call to $\tcode{T}_j$'s move assignment, the state of the contained value is as defined by the exception safety guarantee of $\tcode{T}_j$'s move assignment; \tcode{index()} will be $j$. \end{itemize} \end{itemdescr} \indexlibrarymember{operator=}{variant}% \begin{itemdecl} template variant& operator=(T&& t) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum Let $\tcode{T}_j$ be a type that is determined as follows: build an imaginary function \tcode{\placeholdernc{FUN}($\tcode{T}_i$)} for each alternative type $\tcode{T}_i$. The overload \tcode{\placeholdernc{FUN}($\tcode{T}_j$)} selected by overload resolution for the expression \tcode{\placeholdernc{FUN}(std::forward(\brk{}t))} defines the alternative $\tcode{T}_j$ which is the type of the contained value after assignment. \pnum \effects \begin{itemize} \item If \tcode{*this} holds a $\tcode{T}_j$, assigns \tcode{std::forward(t)} to the value contained in \tcode{*this}. Otherwise, \item if \tcode{is_nothrow_constructible_v<$\tcode{T}_j$, T> ||} \tcode{!is_nothrow_move_constructible_v<$\tcode{T}_j$>} is \tcode{true}, equivalent to \tcode{emplace<$j$>(std::forward(t))}. Otherwise, \item equivalent to \tcode{operator=(variant(std::forward(t)))}. \end{itemize} \pnum \postconditions \tcode{holds_alternative<$\tcode{T}_j$>(*this)} is \tcode{true}, with $\tcode{T}_j$ selected by the imaginary function overload resolution described above. \pnum \returns \tcode{*this}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_same_v, variant>} is \tcode{false}, unless \tcode{is_assignable_v<$\tcode{T}_j$\&, T> \&\& is_constructible_v<$\tcode{T}_j$, T>} is \tcode{true}, and unless the expression \tcode{\placeholdernc{FUN}(std::forward(t))} (with \tcode{\placeholdernc{FUN}} being the above-mentioned set of imaginary functions) is well formed. \pnum \begin{note} \begin{codeblock} variant v; v = "abc"; \end{codeblock} is ill-formed, as both alternative types have an equally viable constructor for the argument. \end{note} \pnum The expression inside \tcode{noexcept} is equivalent to: \begin{codeblock} is_nothrow_assignable_v && is_nothrow_constructible_v \end{codeblock} \begin{itemize} \item If an exception is thrown during the assignment of \tcode{std::forward(t)} to the value contained in \tcode{*this}, the state of the contained value and \tcode{t} are as defined by the exception safety guarantee of the assignment expression; \tcode{valueless_by_exception()} will be \tcode{false}. \item If an exception is thrown during the initialization of the contained value, the \tcode{variant} object might not hold a value. \end{itemize} \end{itemdescr} \rSec3[variant.mod]{Modifiers} \indexlibrarymember{emplace}{variant}% \begin{itemdecl} template T& emplace(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let $I$ be the zero-based index of \tcode{T} in \tcode{Types...}. \pnum \effects Equivalent to: \tcode{return emplace<$I$>(std::forward(args)...);} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}, and \tcode{T} occurs exactly once in \tcode{Types...}. \end{itemdescr} \indexlibrarymember{emplace}{variant}% \begin{itemdecl} template T& emplace(initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let $I$ be the zero-based index of \tcode{T} in \tcode{Types...}. \pnum \effects Equivalent to: \tcode{return emplace<$I$>(il, std::forward(args)...);} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_constructible_v\&, Args...>} is \tcode{true}, and \tcode{T} occurs exactly once in \tcode{Types...}. \end{itemdescr} \indexlibrarymember{emplace}{variant}% \begin{itemdecl} template variant_alternative_t>& emplace(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. \pnum \effects Destroys the currently contained value if \tcode{valueless_by_exception()} is \tcode{false}. Then initializes the contained value as if direct-non-list-initializing a value of type $\tcode{T}_I$ with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{index()} is \tcode{I}. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown during the initialization of the contained value. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_constructible_v<$\tcode{T}_I$, Args...>} is \tcode{true}. If an exception is thrown during the initialization of the contained value, the \tcode{variant} might not hold a value. \end{itemdescr} \indexlibrarymember{emplace}{variant}% \begin{itemdecl} template variant_alternative_t>& emplace(initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. \pnum \effects Destroys the currently contained value if \tcode{valueless_by_exception()} is \tcode{false}. Then initializes the contained value as if direct-non-list-initializing a value of type $\tcode{T}_I$ with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{index()} is \tcode{I}. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown during the initialization of the contained value. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_constructible_v<$\tcode{T}_I$, initializer_list\&, Args...>} is \tcode{true}. If an exception is thrown during the initialization of the contained value, the \tcode{variant} might not hold a value. \end{itemdescr} \rSec3[variant.status]{Value status} \indexlibrarymember{valueless_by_exception}{variant}% \begin{itemdecl} constexpr bool valueless_by_exception() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Returns \tcode{false} if and only if the \tcode{variant} holds a value. \pnum \begin{note} A \tcode{variant} might not hold a value if an exception is thrown during a type-changing assignment or emplacement. The latter means that even a \tcode{variant} can become \tcode{valueless_by_exception()}, for instance by \begin{codeblock} struct S { operator int() { throw 42; }}; variant v{12.f}; v.emplace<1>(S()); \end{codeblock} \end{note} \end{itemdescr} \indexlibrarymember{index}{variant}% \begin{itemdecl} constexpr size_t index() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{valueless_by_exception()} is \tcode{true}, returns \tcode{variant_npos}. Otherwise, returns the zero-based index of the alternative of the contained value. \end{itemdescr} \rSec3[variant.swap]{Swap} \indexlibrarymember{swap}{variant}% \begin{itemdecl} void swap(variant& rhs) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \requires Lvalues of type $\tcode{T}_i$ shall be swappable~(\ref{swappable.requirements}) and \tcode{is_move_constructible_v<$\tcode{T}_i$>} shall be \tcode{true} for all $i$. \pnum \effects \begin{itemize} \item if \tcode{valueless_by_exception() \&\& rhs.valueless_by_exception()} no effect. Otherwise, \item if \tcode{index() == rhs.index()}, calls \tcode{swap(get<$i$>(*this), get<$i$>(rhs))} where $i$ is \tcode{index()}. Otherwise, \item exchanges values of \tcode{rhs} and \tcode{*this}. \end{itemize} \pnum \throws If \tcode{index() == rhs.index()}, any exception thrown by \tcode{swap(get<$i$>(*this), get<$i$>(rhs))} with $i$ being \tcode{index()}. Otherwise, any exception thrown by the move constructor of $\tcode{T}_i$ or $\tcode{T}_j$ with $i$ being \tcode{index()} and $j$ being \tcode{rhs.index()}. \pnum \remarks If an exception is thrown during the call to function \tcode{swap(get<$i$>(*this), get<$i$>(rhs))}, the states of the contained values of \tcode{*this} and of \tcode{rhs} are determined by the exception safety guarantee of \tcode{swap} for lvalues of $\tcode{T}_i$ with $i$ being \tcode{index()}. If an exception is thrown during the exchange of the values of \tcode{*this} and \tcode{rhs}, the states of the values of \tcode{*this} and of \tcode{rhs} are determined by the exception safety guarantee of \tcode{variant}'s move constructor. The expression inside \tcode{noexcept} is equivalent to the logical AND of \tcode{is_nothrow_move_constructible_v<$\tcode{T}_i$> \&\& is_nothrow_swappable_v<$\tcode{T}_i$>} for all $i$. \end{itemdescr} \rSec2[variant.helper]{\tcode{variant} helper classes} \indexlibrary{\idxcode{variant_size}}% \begin{itemdecl} template struct variant_size; \end{itemdecl} \begin{itemdescr} \pnum \remarks All specializations of \tcode{variant_size} shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}) with a base characteristic of \tcode{integral_constant} for some \tcode{N}. \end{itemdescr} \indexlibrary{\idxcode{variant_size}}% \begin{itemdecl} template class variant_size; template class variant_size; template class variant_size; \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VS} denote \tcode{variant_size} of the cv-unqualified type \tcode{T}. Then each of the three templates shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}) with a base characteristic of \tcode{integral_constant}. \end{itemdescr} \indexlibrary{\idxcode{variant_size}}% \begin{itemdecl} template struct variant_size> : integral_constant { }; \end{itemdecl} % No itemdescr needed for variant_size> \indexlibrary{\idxcode{variant_alternative}}% \begin{itemdecl} template class variant_alternative; template class variant_alternative; template class variant_alternative; \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VA} denote \tcode{variant_alternative} of the cv-unqualified type \tcode{T}. Then each of the three templates shall meet the \tcode{TransformationTrait} requirements~(\ref{meta.rqmts}) with a member typedef \tcode{type} that names the following type: \begin{itemize} \item for the first specialization, \tcode{add_const_t}, \item for the second specialization, \tcode{add_volatile_t}, and \item for the third specialization, \tcode{add_cv_t}. \end{itemize} \end{itemdescr} \indexlibrary{\idxcode{variant_alternative}}% \begin{itemdecl} variant_alternative>::type \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. \pnum \textit{Value:} The type $\tcode{T}_I$. \end{itemdescr} \rSec2[variant.get]{Value access} \indexlibrary{\idxcode{holds_alternative}} \indexlibrary{\idxcode{variant}!\idxcode{holds_alternative}} \begin{itemdecl} template constexpr bool holds_alternative(const variant& v) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The type \tcode{T} occurs exactly once in \tcode{Types...}. Otherwise, the program is ill-formed. \pnum \returns \tcode{true} if \tcode{index()} is equal to the zero-based index of \tcode{T} in \tcode{Types...}. \end{itemdescr} \indexlibrarymember{get}{variant}% \begin{itemdecl} template constexpr variant_alternative_t>& get(variant& v); template constexpr variant_alternative_t>&& get(variant&& v); template constexpr const variant_alternative_t>& get(const variant& v); template constexpr const variant_alternative_t>&& get(const variant&& v); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. Otherwise the program is ill-formed. \pnum \effects If \tcode{v.index()} is \tcode{I}, returns a reference to the object stored in the \tcode{variant}. Otherwise, throws an exception of type \tcode{bad_variant_access}. \end{itemdescr} \indexlibrarymember{get}{variant}% \begin{itemdecl} template constexpr T& get(variant& v); template constexpr T&& get(variant&& v); template constexpr const T& get(const variant& v); template constexpr const T&& get(const variant&& v); \end{itemdecl} \begin{itemdescr} \pnum \requires The type \tcode{T} occurs exactly once in \tcode{Types...}. Otherwise, the program is ill-formed. \pnum \effects If \tcode{v} holds a value of type \tcode{T}, returns a reference to that value. Otherwise, throws an exception of type \tcode{bad_variant_access}. \end{itemdescr} \indexlibrary{\idxcode{get_if}}% \indexlibrary{\idxcode{variant}!\idxcode{get_if}}% \begin{itemdecl} template constexpr add_pointer_t>> get_if(variant* v) noexcept; template constexpr add_pointer_t>> get_if(const variant* v) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{I < sizeof...(Types)}. Otherwise the program is ill-formed. \pnum \returns A pointer to the value stored in the \tcode{variant}, if \tcode{v != nullptr} and \tcode{v->index() == I}. Otherwise, returns \tcode{nullptr}. \end{itemdescr} \indexlibrary{\idxcode{get_if}}% \indexlibrary{\idxcode{variant}!\idxcode{get_if}}% \begin{itemdecl} template constexpr add_pointer_t get_if(variant* v) noexcept; template constexpr add_pointer_t get_if(const variant* v) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The type \tcode{T} occurs exactly once in \tcode{Types...}. Otherwise, the program is ill-formed. \pnum \effects Equivalent to: \tcode{return get_if<$i$>(v);} with $i$ being the zero-based index of \tcode{T} in \tcode{Types...}. \end{itemdescr} \rSec2[variant.relops]{Relational operators} \indexlibrarymember{operator==}{variant}% \begin{itemdecl} template constexpr bool operator==(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) == get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{v.index() != w.index()}, \tcode{false}; otherwise if \tcode{v.valueless_by_exception()}, \tcode{true}; otherwise \tcode{get<$i$>(v) == get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \indexlibrarymember{operator"!=}{variant}% \begin{itemdecl} template constexpr bool operator!=(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) != get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{v.index() != w.index()}, \tcode{true}; otherwise if \tcode{v.valueless_by_exception()}, \tcode{false}; otherwise \tcode{get<$i$>(v) != get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \indexlibrarymember{operator<}{variant}% \begin{itemdecl} template constexpr bool operator<(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) < get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{w.valueless_by_exception()}, \tcode{false}; otherwise if \tcode{v.valueless_by_exception()}, \tcode{true}; otherwise, if \tcode{v.index() < w.index()}, \tcode{true}; otherwise if \tcode{v.index() > w.index()}, \tcode{false}; otherwise \tcode{get<$i$>(v) < get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \indexlibrarymember{operator>}{variant}% \begin{itemdecl} template constexpr bool operator>(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) > get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{v.valueless_by_exception()}, \tcode{false}; otherwise if \tcode{w.valueless_by_exception()}, \tcode{true}; otherwise, if \tcode{v.index() > w.index()}, \tcode{true}; otherwise if \tcode{v.index() < w.index()}, \tcode{false}; otherwise \tcode{get<$i$>(v) > get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \indexlibrarymember{operator<=}{variant}% \begin{itemdecl} template constexpr bool operator<=(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) <= get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{v.valueless_by_exception()}, \tcode{true}; otherwise if \tcode{w.valueless_by_exception()}, \tcode{false}; otherwise, if \tcode{v.index() < w.index()}, \tcode{true}; otherwise if \tcode{v.index() > w.index()}, \tcode{false}; otherwise \tcode{get<$i$>(v) <= get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \indexlibrarymember{operator>=}{variant}% \begin{itemdecl} template constexpr bool operator>=(const variant& v, const variant& w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get<$i$>(v) >= get<$i$>(w)} is a valid expression returning a type that is convertible to \tcode{bool}, for all $i$. \pnum \returns If \tcode{w.valueless_by_exception()}, \tcode{true}; otherwise if \tcode{v.valueless_by_exception()}, \tcode{false}; otherwise, if \tcode{v.index() > w.index()}, \tcode{true}; otherwise if \tcode{v.index() < w.index()}, \tcode{false}; otherwise \tcode{get<$i$>(v) >= get<$i$>(w)} with $i$ being \tcode{v.index()}. \end{itemdescr} \rSec2[variant.visit]{Visitation} \indexlibrary{\idxcode{visit}}% \indexlibrary{\idxcode{variant}!\idxcode{visit}}% \begin{itemdecl} template constexpr @\seebelow@ visit(Visitor&& vis, Variants&&... vars); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression in the \effects element shall be a valid expression of the same type and value category, for all combinations of alternative types of all variants. Otherwise, the program is ill-formed. \pnum \effects Let \tcode{is...} be \tcode{vars.index()...}. Returns \tcode{\placeholdernc{INVOKE}(forward(vis), get(}\brk{} \tcode{forward(vars))...);}. \pnum \remarks The return type is the common type of all possible \tcode{\placeholder{INVOKE}} expressions of the \effects element. \pnum \throws \tcode{bad_variant_access} if any \tcode{variant} in \tcode{vars} is \tcode{valueless_by_exception()}. \pnum \complexity For \tcode{sizeof...(Variants) <= 1}, the invocation of the callable object is implemented in constant time, i.e. it does not depend on \tcode{sizeof...(Types).} For \tcode{sizeof...(Variants) > 1}, the invocation of the callable object has no complexity requirements. \end{itemdescr} \indexlibrary{\idxcode{monostate}}% \rSec2[variant.monostate]{Class \tcode{monostate}} \begin{itemdecl} struct monostate{}; \end{itemdecl} \begin{itemdescr} \pnum The class \tcode{monostate} can serve as a first alternative type for a \tcode{variant} to make the \tcode{variant} type default constructible. \end{itemdescr} \rSec2[variant.monostate.relops]{\tcode{monostate} relational operators} \indexlibrary{\idxcode{operator<}!\idxcode{monostate}}% \indexlibrary{\idxcode{operator>}!\idxcode{monostate}}% \indexlibrary{\idxcode{operator<=}!\idxcode{monostate}}% \indexlibrary{\idxcode{operator>=}!\idxcode{monostate}}% \indexlibrary{\idxcode{operator==}!\idxcode{monostate}}% \indexlibrary{\idxcode{operator"!=}!\idxcode{monostate}}% \begin{itemdecl} constexpr bool operator<(monostate, monostate) noexcept { return false; } constexpr bool operator>(monostate, monostate) noexcept { return false; } constexpr bool operator<=(monostate, monostate) noexcept { return true; } constexpr bool operator>=(monostate, monostate) noexcept { return true; } constexpr bool operator==(monostate, monostate) noexcept { return true; } constexpr bool operator!=(monostate, monostate) noexcept { return false; } \end{itemdecl} \begin{itemdescr} \pnum \begin{note} \tcode{monostate} objects have only a single state; they thus always compare equal.\end{note} \end{itemdescr} \rSec2[variant.specalg]{Specialized algorithms} \indexlibrary{\idxcode{swap}!\idxcode{variant}}% \begin{itemdecl} template void swap(variant& v, variant& w) noexcept(@\seebelow@); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{v.swap(w)}. \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_move_constructible_v<$\tcode{T}_i$> \&\& is_swappable_v<$\tcode{T}_i$>} is \tcode{true} for all $i$. The expression inside \tcode{noexcept} is equivalent to \tcode{noexcept(v.swap(w))}. \end{itemdescr} \indexlibrary{\idxcode{bad_variant_access}}% \rSec2[variant.bad.access]{Class \tcode{bad_variant_access}} \begin{codeblock} class bad_variant_access : public exception { public: bad_variant_access() noexcept; const char* what() const noexcept override; }; \end{codeblock} \pnum Objects of type \tcode{bad_variant_access} are thrown to report invalid accesses to the value of a \tcode{variant} object. \indexlibrary{\idxcode{bad_variant_access}!constructor}% \begin{itemdecl} bad_variant_access() noexcept; \end{itemdecl} \begin{itemdescr} \pnum Constructs a \tcode{bad_variant_access} object. \end{itemdescr} \indexlibrarymember{what}{bad_variant_access}% \begin{itemdecl} const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} \pnum \returns An \impldef{return value of \tcode{bad_variant_access::what}} \ntbs. \end{itemdescr} \rSec2[variant.hash]{Hash support} \indexlibrary{\idxcode{hash}!\idxcode{variant}}% \begin{itemdecl} template struct hash>; \end{itemdecl} \begin{itemdescr} \pnum The specialization \tcode{hash>} is enabled~(\ref{unord.hash}) if and only if every specialization in \tcode{hash>...} is enabled. The member functions are not guaranteed to be \tcode{noexcept}. \end{itemdescr} \indexlibrary{\idxcode{hash}!\idxcode{monostate}}% \begin{itemdecl} template <> struct hash; \end{itemdecl} \begin{itemdescr} \pnum The specialization is enabled~(\ref{unord.hash}). \end{itemdescr} \rSec2[variant.traits]{Allocator-related traits} \indexlibrary{\idxcode{uses_allocator}!\idxcode{variant}}% \begin{itemdecl} template struct uses_allocator, Alloc> : true_type { }; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{Alloc} shall be an Allocator~(\ref{allocator.requirements}). \pnum \begin{note} Specialization of this trait informs other library components that variant can be constructed with an allocator, even though it does not have a nested \tcode{allocator_type}. \end{note} \end{itemdescr} \rSec1[any]{Storage for any type} \pnum This section describes components that \Cpp programs may use to perform operations on objects of a discriminated type. \pnum \begin{note} The discriminated type may contain values of different types but does not attempt conversion between them, i.e. \tcode{5} is held strictly as an \tcode{int} and is not implicitly convertible either to \tcode{"5"} or to \tcode{5.0}. This indifference to interpretation but awareness of type effectively allows safe, generic containers of single values, with no scope for surprises from ambiguous conversions. \end{note} \rSec2[any.synop]{Header \tcode{} synopsis} \indextext{\idxhdr{any}}% \indexlibrary{\idxhdr{any}}% \begin{codeblock} namespace std { // \ref{any.bad_any_cast}, class \tcode{bad_any_cast} class bad_any_cast; // \ref{any.class}, class \tcode{any} class any; // \ref{any.nonmembers}, non-member functions void swap(any& x, any& y) noexcept; template any make_any(Args&& ...args); template any make_any(initializer_list il, Args&& ...args); template T any_cast(const any& operand); template T any_cast(any& operand); template T any_cast(any&& operand); template const T* any_cast(const any* operand) noexcept; template T* any_cast(any* operand) noexcept; } \end{codeblock} \rSec2[any.bad_any_cast]{Class \tcode{bad_any_cast}} \indexlibrary{\idxcode{bad_any_cast}}% \begin{codeblock} class bad_any_cast : public bad_cast { public: const char* what() const noexcept override; }; \end{codeblock} \pnum Objects of type \tcode{bad_any_cast} are thrown by a failed \tcode{any_cast}~(\ref{any.nonmembers}). \indexlibrarymember{what}{bad_any_cast}% \begin{itemdecl} const char* what() const noexcept override; \end{itemdecl} \begin{itemdescr} \pnum \returns An \impldef{return value of \tcode{bad_any_cast::what}} \ntbs. \pnum \remarks The message may be a null-terminated multibyte string~(\ref{multibyte.strings}), suitable for conversion and display as a wstring~(\ref{string.classes}, \ref{locale.codecvt}). \end{itemdescr} \rSec2[any.class]{Class \tcode{any}} \begin{codeblock} class any { public: // \ref{any.cons}, construction and destruction constexpr any() noexcept; any(const any& other); any(any&& other) noexcept; template any(T&& value); template explicit any(in_place_type_t, Args&&...); template explicit any(in_place_type_t, initializer_list, Args&&...); ~any(); // \ref{any.assign}, assignments any& operator=(const any& rhs); any& operator=(any&& rhs) noexcept; template any& operator=(T&& rhs); // \ref{any.modifiers}, modifiers template decay_t& emplace(Args&& ...); template decay_t& emplace(initializer_list, Args&&...); void reset() noexcept; void swap(any& rhs) noexcept; // \ref{any.observers}, observers bool has_value() const noexcept; const type_info& type() const noexcept; }; \end{codeblock} \pnum An object of class \tcode{any} stores an instance of any type that satisfies the constructor requirements or it has no value, and this is referred to as the \defn{state} of the class \tcode{any} object. The stored instance is called the \defnx{contained value}{contained value!\idxcode{any}}, Two states are equivalent if either they both have no value, or both have a value and the contained values are equivalent. \pnum The non-member \tcode{any_cast} functions provide type-safe access to the contained value. \pnum Implementations should avoid the use of dynamically allocated memory for a small contained value. \begin{example} where the object constructed is holding only an \tcode{int}. \end{example} Such small-object optimization shall only be applied to types \tcode{T} for which \tcode{is_nothrow_move_constructible_v} is \tcode{true}. \rSec3[any.cons]{Construction and destruction} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} constexpr any() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \postconditions \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} any(const any& other); \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{other.has_value()} is \tcode{false}, constructs an object that has no value. Otherwise, equivalent to \tcode{any(in_place, any_cast(other))} where \tcode{T} is the type of the contained value. \pnum \throws Any exceptions arising from calling the selected constructor for the contained value. \end{itemdescr} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} any(any&& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{other.has_value()} is \tcode{false}, constructs an object that has no value. Otherwise, constructs an object of type \tcode{any} that contains either the contained value of \tcode{other}, or contains an object of the same type constructed from the contained value of \tcode{other} considering that contained value as an rvalue. \pnum \postconditions \tcode{other} is left in a valid but otherwise unspecified state. \end{itemdescr} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} template any(T&& value); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Constructs an object of type \tcode{any} that contains an object of type \tcode{VT} direct-initialized with \tcode{std::forward(value)}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{VT} is not the same type as \tcode{any}, \tcode{VT} is not a specialization of \tcode{in_place_type_t}, and \tcode{is_copy_constructible_v} is \tcode{true}. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \end{itemdescr} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} template explicit any(in_place_type_t, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{VT} with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value of type \tcode{VT}. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{any}!constructor}% \begin{itemdecl} template explicit any(in_place_type_t, initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Initializes the contained value as if direct-non-list-initializing an object of type \tcode{VT} with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v\&, Args...>} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{any}!destructor} \begin{itemdecl} ~any(); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{reset()}. \end{itemdescr} \rSec3[any.assign]{Assignment} \indexlibrarymember{operator=}{any}% \begin{itemdecl} any& operator=(const any& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{any(rhs).swap(*this)}. No effects if an exception is thrown. \pnum \returns \tcode{*this}. \pnum \throws Any exceptions arising from the copy constructor for the contained value. \end{itemdescr} \indexlibrarymember{operator=}{any}% \begin{itemdecl} any& operator=(any&& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{any(std::move(rhs)).swap(*this)}. \pnum \returns \tcode{*this}. \pnum \postconditions The state of \tcode{*this} is equivalent to the original state of \tcode{rhs} and \tcode{rhs} is left in a valid but otherwise unspecified state. \end{itemdescr} \indexlibrarymember{operator=}{any}% \begin{itemdecl} template any& operator=(T&& rhs); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Constructs an object \tcode{tmp} of type \tcode{any} that contains an object of type \tcode{VT} direct-initialized with \tcode{std::forward(rhs)}, and \tcode{tmp.swap(*this)}. No effects if an exception is thrown. \pnum \returns \tcode{*this}. \pnum \remarks This operator shall not participate in overload resolution unless \tcode{VT} is not the same type as \tcode{any} and \tcode{is_copy_constructible_v} is \tcode{true}. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \end{itemdescr} \rSec3[any.modifiers]{Modifiers} \indexlibrarymember{emplace}{any}% \begin{itemdecl} template decay_t& emplace(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Calls \tcode{reset()}. Then initializes the contained value as if direct-non-list-initializing an object of type \tcode{VT} with the arguments \tcode{std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \pnum \remarks If an exception is thrown during the call to \tcode{VT}'s constructor, \tcode{*this} does not contain a value, and any previously contained value has been destroyed. This function shall not participate in overload resolution unless \tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \indexlibrarymember{emplace}{any}% \begin{itemdecl} template decay_t& emplace(initializer_list il, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{VT} be \tcode{decay_t}. \pnum \requires \tcode{VT} shall satisfy the \tcode{CopyConstructible} requirements. \pnum \effects Calls \tcode{reset()}. Then initializes the contained value as if direct-non-list-initializing an object of type \tcode{VT} with the arguments \tcode{il, std::forward(args)...}. \pnum \postconditions \tcode{*this} contains a value. \pnum \returns A reference to the new contained value. \pnum \throws Any exception thrown by the selected constructor of \tcode{VT}. \pnum \remarks If an exception is thrown during the call to \tcode{VT}'s constructor, \tcode{*this} does not contain a value, and any previously contained value has been destroyed. The function shall not participate in overload resolution unless \tcode{is_copy_constructible_v} is \tcode{true} and \tcode{is_constructible_v\&, Args...>} is \tcode{true}. \end{itemdescr} \indexlibrarymember{reset}{any}% \begin{itemdecl} void reset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{has_value()} is \tcode{true}, destroys the contained value. \pnum \postconditions \tcode{has_value()} is \tcode{false}. \end{itemdescr} \indexlibrarymember{swap}{any}% \begin{itemdecl} void swap(any& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Exchanges the states of \tcode{*this} and \tcode{rhs}. \end{itemdescr} \rSec3[any.observers]{Observers} \indexlibrarymember{has_value}{any}% \begin{itemdecl} bool has_value() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if \tcode{*this} contains an object, otherwise \tcode{false}. \end{itemdescr} \indexlibrarymember{type}{any}% \begin{itemdecl} const type_info& type() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{typeid(T)} if \tcode{*this} has a contained value of type \tcode{T}, otherwise \tcode{typeid(void)}. \pnum \begin{note} Useful for querying against types known either at compile time or only at runtime. \end{note} \end{itemdescr} \rSec2[any.nonmembers]{Non-member functions} \indexlibrary{\idxcode{swap}!\idxcode{any}}% \begin{itemdecl} void swap(any& x, any& y) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{x.swap(y)}. \end{itemdescr} \indexlibrary{\idxcode{make_any}}% \begin{itemdecl} template any make_any(Args&& ...args); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return any(in_place_type, std::forward(args)...);} \end{itemdescr} \indexlibrary{\idxcode{make_any}}% \begin{itemdecl} template any make_any(initializer_list il, Args&& ...args); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return any(in_place_type, il, std::forward(args)...);} \end{itemdescr} \indexlibrary{\idxcode{any_cast}}% \begin{itemdecl} template T any_cast(const any& operand); template T any_cast(any& operand); template T any_cast(any&& operand); \end{itemdecl} \begin{itemdescr} \pnum Let \tcode{U} be the type \tcode{remove_cv_t>}. \pnum \requires For the first overload, \tcode{is_constructible_v} is \tcode{true}. For the second overload, \tcode{is_constructible_v} is \tcode{true}. For the third overload, \tcode{is_constructible_v} is \tcode{true}. Otherwise the program is ill-formed. \pnum \returns For the first and second overload, \tcode{static_cast(*any_cast(\&operand))}. For the third overload, \tcode{static_cast(std::move(*any_cast(\&operand)))}. \pnum \throws \tcode{bad_any_cast} if \tcode{operand.type() != typeid(remove_reference_t)}. \pnum \begin{example} \begin{codeblock} any x(5); // \tcode{x} holds \tcode{int} assert(any_cast(x) == 5); // cast to value any_cast(x) = 10; // cast to reference assert(any_cast(x) == 10); x = "Meow"; // \tcode{x} holds \tcode{const char*} assert(strcmp(any_cast(x), "Meow") == 0); any_cast(x) = "Harry"; assert(strcmp(any_cast(x), "Harry") == 0); x = string("Meow"); // \tcode{x} holds \tcode{string} string s, s2("Jane"); s = move(any_cast(x)); // move from \tcode{any} assert(s == "Meow"); any_cast(x) = move(s2); // move to \tcode{any} assert(any_cast(x) == "Jane"); string cat("Meow"); const any y(cat); // \tcode{const y} holds \tcode{string} assert(any_cast(y) == cat); any_cast(y); // error; cannot // \tcode{any_cast} away const \end{codeblock} \end{example} \end{itemdescr} \indexlibrary{\idxcode{any_cast}}% \begin{itemdecl} template const T* any_cast(const any* operand) noexcept; template T* any_cast(any* operand) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns If \tcode{operand != nullptr \&\& operand->type() == typeid(T)}, a pointer to the object contained by \tcode{operand}; otherwise, \tcode{nullptr}. \pnum \begin{example} \begin{codeblock} bool is_string(const any& operand) { return any_cast(&operand) != nullptr; } \end{codeblock} \end{example} \end{itemdescr} \rSec1[bitset]{Bitsets} \indexlibrary{\idxcode{bitset}}% \rSec2[bitset.syn]{Header \tcode{} synopsis}% \indextext{\idxhdr{bitset}}% \indexlibrary{\idxhdr{bitset}}% \begin{codeblock} #include #include // for \tcode{istream} (\ref{istream.syn}), \tcode{ostream} (\ref{ostream.syn}), see \ref{iosfwd.syn} namespace std { template class bitset; // \ref{bitset.operators}, bitset operators template bitset operator&(const bitset&, const bitset&) noexcept; template bitset operator|(const bitset&, const bitset&) noexcept; template bitset operator^(const bitset&, const bitset&) noexcept; template basic_istream& operator>>(basic_istream& is, bitset& x); template basic_ostream& operator<<(basic_ostream& os, const bitset& x); } \end{codeblock} \pnum The header \tcode{} defines a class template and several related functions for representing and manipulating fixed-size sequences of bits. \rSec2[template.bitset]{Class template \tcode{bitset}}% \indexlibrary{\idxcode{bitset}}% \begin{codeblock} namespace std { template class bitset { public: // bit reference: class reference { friend class bitset; reference() noexcept; public: ~reference() noexcept; reference& operator=(bool x) noexcept; // for \tcode{b[i] = x;} reference& operator=(const reference&) noexcept; // for \tcode{b[i] = b[j];} bool operator~() const noexcept; // flips the bit operator bool() const noexcept; // for \tcode{x = b[i];} reference& flip() noexcept; // for \tcode{b[i].flip();} }; // \ref{bitset.cons}, constructors constexpr bitset() noexcept; constexpr bitset(unsigned long long val) noexcept; template explicit bitset( const basic_string& str, typename basic_string::size_type pos = 0, typename basic_string::size_type n = basic_string::npos, charT zero = charT('0'), charT one = charT('1')); template explicit bitset( const charT* str, typename basic_string::size_type n = basic_string::npos, charT zero = charT('0'), charT one = charT('1')); // \ref{bitset.members}, bitset operations bitset& operator&=(const bitset& rhs) noexcept; bitset& operator|=(const bitset& rhs) noexcept; bitset& operator^=(const bitset& rhs) noexcept; bitset& operator<<=(size_t pos) noexcept; bitset& operator>>=(size_t pos) noexcept; bitset& set() noexcept; bitset& set(size_t pos, bool val = true); bitset& reset() noexcept; bitset& reset(size_t pos); bitset operator~() const noexcept; bitset& flip() noexcept; bitset& flip(size_t pos); // element access: constexpr bool operator[](size_t pos) const; // for \tcode{b[i];} reference operator[](size_t pos); // for \tcode{b[i];} unsigned long to_ulong() const; unsigned long long to_ullong() const; template , class Allocator = allocator> basic_string to_string(charT zero = charT('0'), charT one = charT('1')) const; size_t count() const noexcept; constexpr size_t size() const noexcept; bool operator==(const bitset& rhs) const noexcept; bool operator!=(const bitset& rhs) const noexcept; bool test(size_t pos) const; bool all() const noexcept; bool any() const noexcept; bool none() const noexcept; bitset operator<<(size_t pos) const noexcept; bitset operator>>(size_t pos) const noexcept; }; // \ref{bitset.hash}, hash support template struct hash; template struct hash>; } \end{codeblock} \pnum The class template \tcode{bitset}% describes an object that can store a sequence consisting of a fixed number of bits, \tcode{N}. \pnum Each bit represents either the value zero (reset) or one (set). To \term{toggle} a bit is to change the value zero to one, or the value one to zero. Each bit has a non-negative position \tcode{pos}. When converting between an object of class \tcode{bitset} and a value of some integral type, bit position \tcode{pos} corresponds to the \term{bit value} \tcode{1 << pos}. The integral value corresponding to two or more bits is the sum of their bit values. \pnum The functions described in this subclause can report three kinds of errors, each associated with a distinct exception: \begin{itemize} \item an \term{invalid-argument} error is associated with exceptions of type \tcode{invalid_argument}~(\ref{invalid.argument}); \indexlibrary{\idxcode{invalid_argument}}% \item an \term{out-of-range} error is associated with exceptions of type \tcode{out_of_range}~(\ref{out.of.range}); \indexlibrary{\idxcode{out_of_range}}% \item an \term{overflow} error is associated with exceptions of type \tcode{overflow_error}~(\ref{overflow.error}). \indexlibrary{\idxcode{overflow_error}}% \end{itemize} \rSec3[bitset.cons]{\tcode{bitset} constructors} \indexlibrary{\idxcode{bitset}!constructor}% \begin{itemdecl} constexpr bitset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of class \tcode{bitset}, initializing all bits to zero. \end{itemdescr} \indexlibrary{\idxcode{bitset}!constructor}% \begin{itemdecl} constexpr bitset(unsigned long long val) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of class \tcode{bitset}, initializing the first \tcode{M} bit positions to the corresponding bit values in \tcode{val}. \tcode{M} is the smaller of \tcode{N} and the number of bits in the value representation~(\ref{basic.types}) of \tcode{unsigned long long}. If \tcode{M < N}, the remaining bit positions are initialized to zero. \end{itemdescr} \indexlibrary{\idxcode{bitset}!constructor}% \begin{itemdecl} template explicit bitset( const basic_string& str, typename basic_string::size_type pos = 0, typename basic_string::size_type n = basic_string::npos, charT zero = charT('0'), charT one = charT('1')); \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{out_of_range} if \tcode{pos > str.size()} or \tcode{invalid_argument} if an invalid character is found (see below).% \indexlibrary{\idxcode{out_of_range}} \pnum \effects Determines the effective length \tcode{rlen} of the initializing string as the smaller of \tcode{n} and \tcode{str.size() - pos}. The function then throws% \indexlibrary{\idxcode{invalid_argument}} \tcode{invalid_argument} if any of the \tcode{rlen} characters in \tcode{str} beginning at position \tcode{pos} is other than \tcode{zero} or \tcode{one}. The function uses \tcode{traits::eq()} to compare the character values. Otherwise, the function constructs an object of class \tcode{bitset}, initializing the first \tcode{M} bit positions to values determined from the corresponding characters in the string \tcode{str}. \tcode{M} is the smaller of \tcode{N} and \tcode{rlen}. \pnum An element of the constructed object has value zero if the corresponding character in \tcode{str}, beginning at position \tcode{pos}, is \tcode{zero}. Otherwise, the element has the value one. Character position \tcode{pos + M - 1} corresponds to bit position zero. Subsequent decreasing character positions correspond to increasing bit positions. \pnum If \tcode{M < N}, remaining bit positions are initialized to zero. \end{itemdescr} \indexlibrary{\idxcode{bitset}!constructor}% \begin{itemdecl} template explicit bitset( const charT* str, typename basic_string::size_type n = basic_string::npos, charT zero = charT('0'), charT one = charT('1')); \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of class \tcode{bitset} as if by: \begin{codeblock} bitset(n == basic_string::npos ? basic_string(str) : basic_string(str, n), 0, n, zero, one) \end{codeblock} \end{itemdescr} \rSec3[bitset.members]{\tcode{bitset} members} \indexlibrarymember{operator\&=}{bitset}% \begin{itemdecl} bitset& operator&=(const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Clears each bit in \tcode{*this} for which the corresponding bit in \tcode{rhs} is clear, and leaves all other bits unchanged. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator"|=}{bitset}% \begin{itemdecl} bitset& operator|=(const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Sets each bit in \tcode{*this} for which the corresponding bit in \tcode{rhs} is set, and leaves all other bits unchanged. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator\caret=}{bitset}% \begin{itemdecl} bitset& operator^=(const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Toggles each bit in \tcode{*this} for which the corresponding bit in \tcode{rhs} is set, and leaves all other bits unchanged. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator<<=}{bitset}% \begin{itemdecl} bitset& operator<<=(size_t pos) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Replaces each bit at position \tcode{I} in \tcode{*this} with a value determined as follows: \begin{itemize} \item If \tcode{I < pos}, the new value is zero; \item If \tcode{I >= pos}, the new value is the previous value of the bit at position \tcode{I - pos}. \end{itemize} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator>>=}{bitset}% \begin{itemdecl} bitset& operator>>=(size_t pos) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Replaces each bit at position \tcode{I} in \tcode{*this} with a value determined as follows: \begin{itemize} \item If \tcode{pos >= N - I}, the new value is zero; \item If \tcode{pos < N - I}, the new value is the previous value of the bit at position \tcode{I + pos}. \end{itemize} \pnum \returns \tcode{*this}. \end{itemdescr} % Do not use \indexlibrarymember. \indexlibrary{\idxcode{set} (member)!\idxcode{bitset}}% \indexlibrary{\idxcode{bitset}!\idxcode{set}}% \begin{itemdecl} bitset& set() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Sets all bits in \tcode{*this}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrary{\idxcode{set} (member)!\idxcode{bitset}}% \indexlibrary{\idxcode{bitset}!\idxcode{set}}% \begin{itemdecl} bitset& set(size_t pos, bool val = true); \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{out_of_range} if \tcode{pos} does not correspond to a valid bit position.% \indexlibrary{\idxcode{out_of_range}} \pnum \effects Stores a new value in the bit at position \tcode{pos} in \tcode{*this}. If \tcode{val} is \tcode{true}, the stored value is one, otherwise it is zero. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{reset}{bitset}% \begin{itemdecl} bitset& reset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Resets all bits in \tcode{*this}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{reset}{bitset}% \begin{itemdecl} bitset& reset(size_t pos); \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{out_of_range} if \tcode{pos} does not correspond to a valid bit position. \indexlibrary{\idxcode{out_of_range}}% \pnum \effects Resets the bit at position \tcode{pos} in \tcode{*this}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator\~{}}{bitset}% \begin{itemdecl} bitset operator~() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object \tcode{x} of class \tcode{bitset} and initializes it with \tcode{*this}. \pnum \returns \tcode{x.flip()}. \end{itemdescr} \indexlibrarymember{flip}{bitset}% \begin{itemdecl} bitset& flip() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Toggles all bits in \tcode{*this}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{flip}{bitset}% \begin{itemdecl} bitset& flip(size_t pos); \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{out_of_range} if \tcode{pos} does not correspond to a valid bit position.% \indexlibrary{\idxcode{out_of_range}} \pnum \effects Toggles the bit at position \tcode{pos} in \tcode{*this}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{to_ulong}{bitset}% \begin{itemdecl} unsigned long to_ulong() const; \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{overflow_error}% \indexlibrary{\idxcode{overflow_error}} if the integral value \tcode{x} corresponding to the bits in \tcode{*this} cannot be represented as type \tcode{unsigned long}. \pnum \returns \tcode{x}. \end{itemdescr} \indexlibrarymember{to_ullong}{bitset}% \begin{itemdecl} unsigned long long to_ullong() const; \end{itemdecl} \begin{itemdescr} \pnum \indexlibrary{\idxcode{overflow_error}}% \throws \tcode{overflow_error} if the integral value \tcode{x} corresponding to the bits in \tcode{*this} cannot be represented as type \tcode{unsigned long long}. \pnum \returns \tcode{x}. \end{itemdescr} \indexlibrarymember{to_string}{bitset}% \begin{itemdecl} template , class Allocator = allocator> basic_string to_string(charT zero = charT('0'), charT one = charT('1')) const; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a string object of the appropriate type and initializes it to a string of length \tcode{N} characters. Each character is determined by the value of its corresponding bit position in \tcode{*this}. Character position \tcode{N - 1} corresponds to bit position zero. Subsequent decreasing character positions correspond to increasing bit positions. Bit value zero becomes the character \tcode{zero}, bit value one becomes the character \tcode{one}. \pnum \returns The created object. \end{itemdescr} \indexlibrarymember{count}{bitset}% \begin{itemdecl} size_t count() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A count of the number of bits set in \tcode{*this}. \end{itemdescr} \indexlibrarymember{size}{bitset}% \begin{itemdecl} constexpr size_t size() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{N}. \end{itemdescr} \indexlibrarymember{operator==}{bitset}% \begin{itemdecl} bool operator==(const bitset& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if the value of each bit in \tcode{*this} equals the value of the corresponding bit in \tcode{rhs}. \end{itemdescr} \indexlibrarymember{operator"!=}{bitset}% \begin{itemdecl} bool operator!=(const bitset& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if \tcode{!(*this == rhs)}. \end{itemdescr} \indexlibrarymember{test}{bitset}% \begin{itemdecl} bool test(size_t pos) const; \end{itemdecl} \begin{itemdescr} \pnum \throws \tcode{out_of_range} if \tcode{pos} does not correspond to a valid bit position.% \indexlibrary{\idxcode{out_of_range}} \pnum \returns \tcode{true} if the bit at position \tcode{pos} in \tcode{*this} has the value one. \end{itemdescr} \indexlibrarymember{all}{bitset}% \begin{itemdecl} bool all() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{count() == size()}. \end{itemdescr} \indexlibrary{\idxcode{any} (member)!\idxcode{bitset}}% \indexlibrary{\idxcode{bitset}!\idxcode{any}}% \begin{itemdecl} bool any() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{count() != 0}. \end{itemdescr} \indexlibrarymember{none}{bitset}% \begin{itemdecl} bool none() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{count() == 0}. \end{itemdescr} \indexlibrarymember{operator<<}{bitset}% \begin{itemdecl} bitset operator<<(size_t pos) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bitset(*this) <<= pos}. \end{itemdescr} \indexlibrarymember{operator>>}{bitset}% \begin{itemdecl} bitset operator>>(size_t pos) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bitset(*this) >>= pos}. \end{itemdescr} \indexlibrarymember{operator[]}{bitset}% \begin{itemdecl} constexpr bool operator[](size_t pos) const; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{pos} shall be valid. \pnum \returns \tcode{true} if the bit at position \tcode{pos} in \tcode{*this} has the value one, otherwise \tcode{false}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{operator[]}{bitset}% \begin{itemdecl} bitset::reference operator[](size_t pos); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{pos} shall be valid. \pnum \returns An object of type \tcode{bitset::reference} such that \tcode{(*this)[pos] == this->test(pos)}, and such that \tcode{(*this)[pos] = val} is equivalent to \tcode{this->set(pos, val)}. \pnum \throws Nothing. \pnum \remarks For the purpose of determining the presence of a data race~(\ref{intro.multithread}), any access or update through the resulting reference potentially accesses or modifies, respectively, the entire underlying bitset. \end{itemdescr} \rSec2[bitset.hash]{\tcode{bitset} hash support} \indexlibrary{\idxcode{hash_code}}% \begin{itemdecl} template struct hash>; \end{itemdecl} \begin{itemdescr} \pnum The specialization is enabled~(\ref{unord.hash}). \end{itemdescr} \rSec2[bitset.operators]{\tcode{bitset} operators} \indexlibrarymember{operator\&}{bitset}% \begin{itemdecl} bitset operator&(const bitset& lhs, const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bitset(lhs) \&= rhs}. \end{itemdescr} \indexlibrarymember{operator"|}{bitset}% \begin{itemdecl} bitset operator|(const bitset& lhs, const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bitset(lhs) |= rhs}. \end{itemdescr} \indexlibrarymember{operator\caret}{bitset}% \begin{itemdecl} bitset operator^(const bitset& lhs, const bitset& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{bitset(lhs) \caret= rhs}. \end{itemdescr} \indexlibrarymember{operator>>}{bitset}% \begin{itemdecl} template basic_istream& operator>>(basic_istream& is, bitset& x); \end{itemdecl} \begin{itemdescr} \pnum A formatted input function~(\ref{istream.formatted}). \pnum \effects Extracts up to \tcode{N} characters from \tcode{is}. Stores these characters in a temporary object \tcode{str} of type \tcode{basic_string}, then evaluates the expression \tcode{x = bitset(str)}. Characters are extracted and stored until any of the following occurs: \begin{itemize} \item \tcode{N} characters have been extracted and stored; \item end-of-file occurs on the input sequence;% \indextext{end-of-file} \item the next input character is neither \tcode{is.widen('0')} nor \tcode{is.widen('1')} (in which case the input character is not extracted). \end{itemize} \pnum If no characters are stored in \tcode{str}, calls \tcode{is.setstate(ios_base::failbit)} (which may throw \tcode{ios_base::failure}~(\ref{iostate.flags})). \pnum \returns \tcode{is}. \end{itemdescr} \indexlibrarymember{operator<<}{bitset}% \begin{itemdecl} template basic_ostream& operator<<(basic_ostream& os, const bitset& x); \end{itemdecl} \begin{itemdescr} \pnum \returns \begin{codeblock} os << x.template to_string>( use_facet>(os.getloc()).widen('0'), use_facet>(os.getloc()).widen('1')) \end{codeblock} (see~\ref{ostream.formatted}). \end{itemdescr} \rSec1[memory]{Memory} \rSec2[memory.general]{In general} \pnum This subclause describes the contents of the header \tcode{}~(\ref{memory.syn}) and some of the contents of the header \tcode{}~(\ref{cstdlib.syn}). \rSec2[memory.syn]{Header \tcode{} synopsis} \pnum The header \tcode{} defines several types and function templates that describe properties of pointers and pointer-like types, manage memory for containers and other template types, destroy objects, and construct multiple objects in uninitialized memory buffers~(\ref{pointer.traits}--\ref{specialized.algorithms}). The header also defines the templates \tcode{unique_ptr}, \tcode{shared_ptr}, \tcode{weak_ptr}, and various function templates that operate on objects of these types~(\ref{smartptr}). \indextext{\idxhdr{memory}}% \indexlibrary{\idxhdr{memory}}% \begin{codeblock} namespace std { // \ref{pointer.traits}, pointer traits template struct pointer_traits; template struct pointer_traits; // \ref{util.dynamic.safety}, pointer safety enum class pointer_safety { relaxed, preferred, strict }; void declare_reachable(void* p); template T* undeclare_reachable(T* p); void declare_no_pointers(char* p, size_t n); void undeclare_no_pointers(char* p, size_t n); pointer_safety get_pointer_safety() noexcept; // \ref{ptr.align}, pointer alignment function void* align(size_t alignment, size_t size, void*& ptr, size_t& space); // \ref{allocator.tag}, allocator argument tag struct allocator_arg_t { explicit allocator_arg_t() = default; }; inline constexpr allocator_arg_t allocator_arg{}; // \ref{allocator.uses}, \tcode{uses_allocator} template struct uses_allocator; // \ref{allocator.traits}, allocator traits template struct allocator_traits; // \ref{default.allocator}, the default allocator template class allocator; template bool operator==(const allocator&, const allocator&) noexcept; template bool operator!=(const allocator&, const allocator&) noexcept; // \ref{specialized.algorithms}, specialized algorithms template constexpr T* addressof(T& r) noexcept; template const T* addressof(const T&&) = delete; template void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); template void uninitialized_default_construct(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, ForwardIterator last); template ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); template ForwardIterator uninitialized_default_construct_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, Size n); template void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); template void uninitialized_value_construct(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, ForwardIterator last); template ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); template ForwardIterator uninitialized_value_construct_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, Size n); template ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); template ForwardIterator uninitialized_copy(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} InputIterator first, InputIterator last, ForwardIterator result); template ForwardIterator uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); template ForwardIterator uninitialized_copy_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} InputIterator first, Size n, ForwardIterator result); template ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); template ForwardIterator uninitialized_move(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} InputIterator first, InputIterator last, ForwardIterator result); template pair uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); template pair uninitialized_move_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} InputIterator first, Size n, ForwardIterator result); template void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); template void uninitialized_fill(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, ForwardIterator last, const T& x); template ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x); template ForwardIterator uninitialized_fill_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, Size n, const T& x); template void destroy_at(T* location); template void destroy(ForwardIterator first, ForwardIterator last); template void destroy(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, ForwardIterator last); template ForwardIterator destroy_n(ForwardIterator first, Size n); template ForwardIterator destroy_n(ExecutionPolicy&& exec, // see \ref{algorithms.parallel.overloads} ForwardIterator first, Size n); // \ref{unique.ptr}, class template \tcode{unique_ptr} template struct default_delete; template struct default_delete; template > class unique_ptr; template class unique_ptr; template unique_ptr make_unique(Args&&... args); template unique_ptr make_unique(size_t n); template @\unspec@ make_unique(Args&&...) = delete; template void swap(unique_ptr& x, unique_ptr& y) noexcept; template bool operator==(const unique_ptr& x, const unique_ptr& y); template bool operator!=(const unique_ptr& x, const unique_ptr& y); template bool operator<(const unique_ptr& x, const unique_ptr& y); template bool operator<=(const unique_ptr& x, const unique_ptr& y); template bool operator>(const unique_ptr& x, const unique_ptr& y); template bool operator>=(const unique_ptr& x, const unique_ptr& y); template bool operator==(const unique_ptr& x, nullptr_t) noexcept; template bool operator==(nullptr_t, const unique_ptr& y) noexcept; template bool operator!=(const unique_ptr& x, nullptr_t) noexcept; template bool operator!=(nullptr_t, const unique_ptr& y) noexcept; template bool operator<(const unique_ptr& x, nullptr_t); template bool operator<(nullptr_t, const unique_ptr& y); template bool operator<=(const unique_ptr& x, nullptr_t); template bool operator<=(nullptr_t, const unique_ptr& y); template bool operator>(const unique_ptr& x, nullptr_t); template bool operator>(nullptr_t, const unique_ptr& y); template bool operator>=(const unique_ptr& x, nullptr_t); template bool operator>=(nullptr_t, const unique_ptr& y); // \ref{util.smartptr.weak.bad}, class \tcode{bad_weak_ptr} class bad_weak_ptr; // \ref{util.smartptr.shared}, class template \tcode{shared_ptr} template class shared_ptr; // \ref{util.smartptr.shared.create}, \tcode{shared_ptr} creation template shared_ptr make_shared(Args&&... args); template shared_ptr allocate_shared(const A& a, Args&&... args); // \ref{util.smartptr.shared.cmp}, \tcode{shared_ptr} comparisons template bool operator==(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator!=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator<(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator>(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator<=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator>=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator==(const shared_ptr& x, nullptr_t) noexcept; template bool operator==(nullptr_t, const shared_ptr& y) noexcept; template bool operator!=(const shared_ptr& x, nullptr_t) noexcept; template bool operator!=(nullptr_t, const shared_ptr& y) noexcept; template bool operator<(const shared_ptr& x, nullptr_t) noexcept; template bool operator<(nullptr_t, const shared_ptr& y) noexcept; template bool operator<=(const shared_ptr& x, nullptr_t) noexcept; template bool operator<=(nullptr_t, const shared_ptr& y) noexcept; template bool operator>(const shared_ptr& x, nullptr_t) noexcept; template bool operator>(nullptr_t, const shared_ptr& y) noexcept; template bool operator>=(const shared_ptr& x, nullptr_t) noexcept; template bool operator>=(nullptr_t, const shared_ptr& y) noexcept; // \ref{util.smartptr.shared.spec}, \tcode{shared_ptr} specialized algorithms template void swap(shared_ptr& a, shared_ptr& b) noexcept; // \ref{util.smartptr.shared.cast}, \tcode{shared_ptr} casts template shared_ptr static_pointer_cast(const shared_ptr& r) noexcept; template shared_ptr dynamic_pointer_cast(const shared_ptr& r) noexcept; template shared_ptr const_pointer_cast(const shared_ptr& r) noexcept; // \ref{util.smartptr.getdeleter}, \tcode{shared_ptr} \tcode{get_deleter} template D* get_deleter(const shared_ptr& p) noexcept; // \ref{util.smartptr.shared.io}, \tcode{shared_ptr} I/O template basic_ostream& operator<< (basic_ostream& os, const shared_ptr& p); // \ref{util.smartptr.weak}, class template \tcode{weak_ptr} template class weak_ptr; // \ref{util.smartptr.weak.spec}, \tcode{weak_ptr} specialized algorithms template void swap(weak_ptr& a, weak_ptr& b) noexcept; // \ref{util.smartptr.ownerless}, class template \tcode{owner_less} template struct owner_less; // \ref{util.smartptr.enab}, class template \tcode{enable_shared_from_this} template class enable_shared_from_this; // \ref{util.smartptr.shared.atomic}, \tcode{shared_ptr} atomic access template bool atomic_is_lock_free(const shared_ptr* p); template shared_ptr atomic_load(const shared_ptr* p); template shared_ptr atomic_load_explicit(const shared_ptr* p, memory_order mo); template void atomic_store(shared_ptr* p, shared_ptr r); template void atomic_store_explicit(shared_ptr* p, shared_ptr r, memory_order mo); template shared_ptr atomic_exchange(shared_ptr* p, shared_ptr r); template shared_ptr atomic_exchange_explicit(shared_ptr* p, shared_ptr r, memory_order mo); template bool atomic_compare_exchange_weak( shared_ptr* p, shared_ptr* v, shared_ptr w); template bool atomic_compare_exchange_strong( shared_ptr* p, shared_ptr* v, shared_ptr w); template bool atomic_compare_exchange_weak_explicit( shared_ptr* p, shared_ptr* v, shared_ptr w, memory_order success, memory_order failure); template bool atomic_compare_exchange_strong_explicit( shared_ptr* p, shared_ptr* v, shared_ptr w, memory_order success, memory_order failure); // \ref{util.smartptr.hash}, hash support template struct hash; template struct hash>; template struct hash>; // \ref{allocator.uses.trait}, \tcode{uses_allocator} template inline constexpr bool uses_allocator_v = uses_allocator::value; } \end{codeblock} \rSec2[pointer.traits]{Pointer traits} \pnum The class template \tcode{pointer_traits} supplies a uniform interface to certain attributes of pointer-like types. \indexlibrary{\idxcode{pointer_traits}}% \begin{codeblock} namespace std { template struct pointer_traits { using pointer = Ptr; using element_type = @\seebelow@; using difference_type = @\seebelow@; template using rebind = @\seebelow@; static pointer pointer_to(@\seebelow@ r); }; template struct pointer_traits { using pointer = T*; using element_type = T; using difference_type = ptrdiff_t; template using rebind = U*; static pointer pointer_to(@\seebelow@ r) noexcept; }; } \end{codeblock} \rSec3[pointer.traits.types]{Pointer traits member types} \indexlibrarymember{element_type}{pointer_traits}% \begin{itemdecl} using element_type = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Ptr::element_type} if the \grammarterm{qualified-id} \tcode{Ptr::element_type} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{T} if \tcode{Ptr} is a class template instantiation of the form \tcode{SomePointer}, where \tcode{Args} is zero or more type arguments; otherwise, the specialization is ill-formed. \end{itemdescr} \indexlibrarymember{difference_type}{pointer_traits}% \begin{itemdecl} using difference_type = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Ptr::difference_type} if the \grammarterm{qualified-id} \tcode{Ptr::difference_type} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{ptrdiff_t}. \end{itemdescr} \indexlibrarymember{rebind}{pointer_traits}% \begin{itemdecl} template using rebind = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \templalias \tcode{Ptr::rebind} if the \grammarterm{qualified-id} \tcode{Ptr::rebind} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{SomePointer} if \tcode{Ptr} is a class template instantiation of the form \tcode{SomePointer}, where \tcode{Args} is zero or more type arguments; otherwise, the instantiation of \tcode{rebind} is ill-formed. \end{itemdescr} \rSec3[pointer.traits.functions]{Pointer traits member functions} \indexlibrarymember{pointer_to}{pointer_traits}% \begin{itemdecl} static pointer pointer_traits::pointer_to(@\seebelow@ r); static pointer pointer_traits::pointer_to(@\seebelow@ r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \remarks If \tcode{element_type} is \cv{}~\tcode{void}, the type of \tcode{r} is unspecified; otherwise, it is \tcode{element_type\&}. \pnum \returns The first member function returns a pointer to \tcode{r} obtained by calling \tcode{Ptr::pointer_to(r)} through which indirection is valid; an instantiation of this function is ill-formed if \tcode{Ptr} does not have a matching \tcode{pointer_to} static member function. The second member function returns \tcode{addressof(r)}. \end{itemdescr} \rSec2[util.dynamic.safety]{Pointer safety} \pnum A complete object is \techterm{declared reachable} while the number of calls to \tcode{declare_reachable} with an argument referencing the object exceeds the number of calls to \tcode{undeclare_reachable} with an argument referencing the object. \indexlibrary{\idxcode{declare_reachable}}% \begin{itemdecl} void declare_reachable(void* p); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall be a safely-derived pointer~(\ref{basic.stc.dynamic.safety}) or a null pointer value. \pnum \effects If \tcode{p} is not null, the complete object referenced by \tcode{p} is subsequently declared reachable~(\ref{basic.stc.dynamic.safety}). \pnum \throws May throw \tcode{bad_alloc} if the system cannot allocate additional memory that may be required to track objects declared reachable. \end{itemdescr} \indexlibrary{\idxcode{undeclare_reachable}}% \begin{itemdecl} template T* undeclare_reachable(T* p); \end{itemdecl} \begin{itemdescr} \pnum \requires If \tcode{p} is not null, the complete object referenced by \tcode{p} shall have been previously declared reachable, and shall be live~(\ref{basic.life}) from the time of the call until the last \tcode{undeclare_reachable(p)} call on the object. \pnum \returns A safely derived copy of \tcode{p} which shall compare equal to \tcode{p}. \pnum \throws Nothing. \pnum \begin{note} It is expected that calls to \tcode{declare_reachable(p)} will consume a small amount of memory in addition to that occupied by the referenced object until the matching call to \tcode{undeclare_reachable(p)} is encountered. Long running programs should arrange that calls are matched. \end{note} \end{itemdescr} \indexlibrary{\idxcode{declare_no_pointers}}% \begin{itemdecl} void declare_no_pointers(char* p, size_t n); \end{itemdecl} \begin{itemdescr} \pnum \requires No bytes in the specified range are currently registered with \tcode{declare_no_pointers()}. If the specified range is in an allocated object, then it must be entirely within a single allocated object. The object must be live until the corresponding \tcode{undeclare_no_pointers()} call. \begin{note} In a garbage-collecting implementation, the fact that a region in an object is registered with \tcode{declare_no_pointers()} should not prevent the object from being collected. \end{note} \pnum \effects The \tcode{n} bytes starting at \tcode{p} no longer contain traceable pointer locations, independent of their type. Hence indirection through a pointer located there is undefined if the object it points to was created by global \tcode{operator new} and not previously declared reachable. \begin{note} This may be used to inform a garbage collector or leak detector that this region of memory need not be traced. \end{note} \pnum \throws Nothing. \pnum \begin{note} Under some conditions implementations may need to allocate memory. However, the request can be ignored if memory allocation fails. \end{note} \end{itemdescr} \indexlibrary{\idxcode{undeclare_no_pointers}}% \begin{itemdecl} void undeclare_no_pointers(char* p, size_t n); \end{itemdecl} \begin{itemdescr} \pnum \requires The same range must previously have been passed to \tcode{declare_no_pointers()}. \pnum \effects Unregisters a range registered with \tcode{declare_no_pointers()} for destruction. It must be called before the lifetime of the object ends. \pnum \throws Nothing. \end{itemdescr} \indexlibrary{\idxcode{get_pointer_safety}}% \begin{itemdecl} pointer_safety get_pointer_safety() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{pointer_safety::strict} if the implementation has strict pointer safety~(\ref{basic.stc.dynamic.safety}). It is \impldef{whether \tcode{get_pointer_safety} returns \tcode{pointer_safety::relaxed} or \tcode{pointer_safety::\brk{}preferred} if the implementation has relaxed pointer safety} whether \tcode{get_pointer_safety} returns \tcode{pointer_safety::relaxed} or \tcode{point\-er_safety::preferred} if the implementation has relaxed pointer safety.\footnote{\tcode{pointer_safety::preferred} might be returned to indicate that a leak detector is running so that the program can avoid spurious leak reports.} \end{itemdescr} \rSec2[ptr.align]{Align} \indexlibrary{\idxcode{align}}% \begin{itemdecl} void* align(size_t alignment, size_t size, void*& ptr, size_t& space); \end{itemdecl} \begin{itemdescr} \pnum \effects If it is possible to fit \tcode{size} bytes of storage aligned by \tcode{alignment} into the buffer pointed to by \tcode{ptr} with length \tcode{space}, the function updates \tcode{ptr} to represent the first possible address of such storage and decreases \tcode{space} by the number of bytes used for alignment. Otherwise, the function does nothing. \pnum \requires \begin{itemize} \item \tcode{alignment} shall be a power of two \item \tcode{ptr} shall represent the address of contiguous storage of at least \tcode{space} bytes \end{itemize} \pnum \returns A null pointer if the requested aligned buffer would not fit into the available space, otherwise the adjusted value of \tcode{ptr}. \pnum \begin{note} The function updates its \tcode{ptr} and \tcode{space} arguments so that it can be called repeatedly with possibly different \tcode{alignment} and \tcode{size} arguments for the same buffer. \end{note} \end{itemdescr} \rSec2[allocator.tag]{Allocator argument tag} \indexlibrary{\idxcode{allocator_arg_t}}% \indexlibrary{\idxcode{allocator_arg}}% \begin{itemdecl} namespace std { struct allocator_arg_t { explicit allocator_arg_t() = default; }; inline constexpr allocator_arg_t allocator_arg{}; } \end{itemdecl} \pnum The \tcode{allocator_arg_t} struct is an empty structure type used as a unique type to disambiguate constructor and function overloading. Specifically, several types (see \tcode{tuple}~\ref{tuple}) have constructors with \tcode{allocator_arg_t} as the first argument, immediately followed by an argument of a type that satisfies the \tcode{Allocator} requirements~(\ref{allocator.requirements}). \rSec2[allocator.uses]{\tcode{uses_allocator}} \rSec3[allocator.uses.trait]{\tcode{uses_allocator} trait} \indexlibrary{\idxcode{uses_allocator}}% \begin{itemdecl} template struct uses_allocator; \end{itemdecl} \begin{itemdescr} \pnum \remarks Automatically detects whether \tcode{T} has a nested \tcode{allocator_type} that is convertible from \tcode{Alloc}. Meets the \tcode{BinaryTypeTrait} requirements~(\ref{meta.rqmts}). The implementation shall provide a definition that is derived from \tcode{true_type} if the \grammarterm{qualified-id} \tcode{T::allocator_type} is valid and denotes a type~(\ref{temp.deduct}) and \tcode{is_convertible_v != false}, otherwise it shall be derived from \tcode{false_type}. A program may specialize this template to derive from \tcode{true_type} for a user-defined type \tcode{T} that does not have a nested \tcode{allocator_type} but nonetheless can be constructed with an allocator where either: \begin{itemize} \item the first argument of a constructor has type \tcode{allocator_arg_t} and the second argument has type \tcode{Alloc} or \item the last argument of a constructor has type \tcode{Alloc}. \end{itemize} \end{itemdescr} \rSec3[allocator.uses.construction]{Uses-allocator construction} \pnum \defnx{Uses-allocator construction}{uses-allocator construction} with allocator \tcode{Alloc} refers to the construction of an object \tcode{obj} of type \tcode{T}, using constructor arguments \tcode{v1, v2, ..., vN} of types \tcode{V1, V2, ..., VN}, respectively, and an allocator \tcode{alloc} of type \tcode{Alloc}, according to the following rules: \begin{itemize} \item if \tcode{uses_allocator_v} is \tcode{false} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{obj} is initialized as \tcode{obj(v1, v2, ..., vN)}; \item otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{obj} is initialized as \tcode{obj(allocator_arg, alloc, v1, v2, ..., vN)}; \item otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{obj} is initialized as \tcode{obj(v1, v2, ..., vN, alloc)}; \item otherwise, the request for uses-allocator construction is ill-formed. \begin{note} An error will result if \tcode{uses_allocator_v} is \tcode{true} but the specific constructor does not take an allocator. This definition prevents a silent failure to pass the allocator to an element. \end{note} \end{itemize} \rSec2[allocator.traits]{Allocator traits} \pnum The class template \tcode{allocator_traits} supplies a uniform interface to all allocator types. An allocator cannot be a non-class type, however, even if \tcode{allocator_traits} supplies the entire required interface. \begin{note} Thus, it is always possible to create a derived class from an allocator. \end{note} \indexlibrary{\idxcode{allocator_traits}}% \begin{codeblock} namespace std { template struct allocator_traits { using allocator_type = Alloc; using value_type = typename Alloc::value_type; using pointer = @\seebelow@; using const_pointer = @\seebelow@; using void_pointer = @\seebelow@; using const_void_pointer = @\seebelow@; using difference_type = @\seebelow@; using size_type = @\seebelow@; using propagate_on_container_copy_assignment = @\seebelow@; using propagate_on_container_move_assignment = @\seebelow@; using propagate_on_container_swap = @\seebelow@; using is_always_equal = @\seebelow@; template using rebind_alloc = @\seebelow@; template using rebind_traits = allocator_traits>; static pointer allocate(Alloc& a, size_type n); static pointer allocate(Alloc& a, size_type n, const_void_pointer hint); static void deallocate(Alloc& a, pointer p, size_type n); template static void construct(Alloc& a, T* p, Args&&... args); template static void destroy(Alloc& a, T* p); static size_type max_size(const Alloc& a) noexcept; static Alloc select_on_container_copy_construction(const Alloc& rhs); }; } \end{codeblock} \rSec3[allocator.traits.types]{Allocator traits member types} \indexlibrarymember{pointer}{allocator_traits}% \begin{itemdecl} using pointer = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::pointer} if the \grammarterm{qualified-id} \tcode{Alloc::pointer} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{value_type*}. \end{itemdescr} \indexlibrarymember{const_pointer}{allocator_traits}% \begin{itemdecl} using const_pointer = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::const_pointer} if the \grammarterm{qualified-id} \tcode{Alloc::const_pointer} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{pointer_traits::rebind<\brk{}const value_type>}. \end{itemdescr} \indexlibrarymember{void_pointer}{allocator_traits}% \begin{itemdecl} using void_pointer = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::void_pointer} if the \grammarterm{qualified-id} \tcode{Alloc::void_pointer} is valid and denotes a type (\ref{temp.deduct}); otherwise, \tcode{pointer_traits::rebind<\brk{}void>}. \end{itemdescr} \indexlibrarymember{const_void_pointer}{allocator_traits}% \begin{itemdecl} using const_void_pointer = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::const_void_pointer} if the \grammarterm{qualified-id} \tcode{Alloc::const_void_pointer} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{pointer_traits::\brk{}rebind}. \end{itemdescr} \indexlibrarymember{difference_type}{allocator_traits}% \begin{itemdecl} using difference_type = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::difference_type} if the \grammarterm{qualified-id} \tcode{Alloc::difference_type} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{pointer_traits::dif\-ference_type}. \end{itemdescr} \indexlibrarymember{size_type}{allocator_traits}% \begin{itemdecl} using size_type = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::size_type} if the \grammarterm{qualified-id} \tcode{Alloc::size_type} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{make_unsigned_t}. \end{itemdescr} \indexlibrarymember{propagate_on_container_copy_assignment}{allocator_traits}% \begin{itemdecl} using propagate_on_container_copy_assignment = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::propagate_on_container_copy_assignment} if the \grammarterm{qualified-id} \tcode{Alloc::propagate_on_container_copy_assignment} is valid and denotes a type~(\ref{temp.deduct}); otherwise \tcode{false_type}. \end{itemdescr} \indexlibrarymember{propagate_on_container_move_assignment}{allocator_traits}% \begin{itemdecl} using propagate_on_container_move_assignment = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::propagate_on_container_move_assignment} if the \grammarterm{qualified-id} \tcode{Alloc::propagate_on_container_move_assignment} is valid and denotes a type~(\ref{temp.deduct}); otherwise \tcode{false_type}. \end{itemdescr} \indexlibrarymember{propagate_on_container_swap}{allocator_traits}% \begin{itemdecl} using propagate_on_container_swap = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::propagate_on_container_swap} if the \grammarterm{qualified-id} \tcode{Alloc::propagate_on_container_swap} is valid and denotes a type~(\ref{temp.deduct}); otherwise \tcode{false_type}. \end{itemdescr} \indexlibrarymember{is_always_equal}{allocator_traits}% \begin{itemdecl} using is_always_equal = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{Alloc::is_always_equal} if the \grammarterm{qualified-id} \tcode{Alloc::is_always_equal} is valid and denotes a type~(\ref{temp.deduct}); otherwise \tcode{is_empty::type}. \end{itemdescr} \indexlibrarymember{rebind_alloc}{allocator_traits}% \begin{itemdecl} template using rebind_alloc = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \templalias \tcode{Alloc::rebind::other} if the \grammarterm{qualified-id} \tcode{Alloc::rebind::other} is valid and denotes a type~(\ref{temp.deduct}); otherwise, \tcode{Alloc} if \tcode{Alloc} is a class template instantiation of the form \tcode{Alloc}, where \tcode{Args} is zero or more type arguments; otherwise, the instantiation of \tcode{rebind_alloc} is ill-formed. \end{itemdescr} \rSec3[allocator.traits.members]{Allocator traits static member functions} \indexlibrarymember{allocate}{allocator_traits}% \begin{itemdecl} static pointer allocate(Alloc& a, size_type n); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{a.allocate(n)}. \end{itemdescr} \indexlibrarymember{allocate}{allocator_traits}% \begin{itemdecl} static pointer allocate(Alloc& a, size_type n, const_void_pointer hint); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{a.allocate(n, hint)} if that expression is well-formed; otherwise, \tcode{a.allocate(n)}. \end{itemdescr} \indexlibrarymember{deallocate}{allocator_traits}% \begin{itemdecl} static void deallocate(Alloc& a, pointer p, size_type n); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{a.deallocate(p, n)}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{construct}{allocator_traits}% \begin{itemdecl} template static void construct(Alloc& a, T* p, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{a.construct(p, std::forward(args)...)} if that call is well-formed; otherwise, invokes \tcode{::new (static_cast(p)) T(std::forward(args)...)}. \end{itemdescr} \indexlibrarymember{destroy}{allocator_traits}% \begin{itemdecl} template static void destroy(Alloc& a, T* p); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{a.destroy(p)} if that call is well-formed; otherwise, invokes \tcode{p->\~{}T()}. \end{itemdescr} \indexlibrarymember{max_size}{allocator_traits}% \begin{itemdecl} static size_type max_size(const Alloc& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{a.max_size()} if that expression is well-formed; otherwise, \tcode{numeric_limits::\brk{}max()/sizeof(value_type)}. \end{itemdescr} \indexlibrarymember{select_on_container_copy_construction}{allocator_traits}% \begin{itemdecl} static Alloc select_on_container_copy_construction(const Alloc& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rhs.select_on_container_copy_construction()} if that expression is well-formed; otherwise, \tcode{rhs}. \end{itemdescr} \rSec2[default.allocator]{The default allocator} \pnum All specializations of the default allocator satisfy the allocator completeness requirements~(\ref{allocator.requirements.completeness}). \indexlibrary{\idxcode{allocator}}% \begin{codeblock} namespace std { template class allocator { public: using value_type = T; using propagate_on_container_move_assignment = true_type; using is_always_equal = true_type; allocator() noexcept; allocator(const allocator&) noexcept; template allocator(const allocator&) noexcept; ~allocator(); T* allocate(size_t n); void deallocate(T* p, size_t n); }; } \end{codeblock} \rSec3[allocator.members]{\tcode{allocator} members} \pnum Except for the destructor, member functions of the default allocator shall not introduce data races~(\ref{intro.multithread}) as a result of concurrent calls to those member functions from different threads. Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before the next allocation (if any) in this order. \indexlibrarymember{allocate}{allocator}% \begin{itemdecl} T* allocate(size_t n); \end{itemdecl} \begin{itemdescr} \pnum \returns A pointer to the initial element of an array of storage of size \tcode{n} \tcode{* sizeof(T)}, aligned appropriately for objects of type \tcode{T}. \pnum \remarks the storage is obtained by calling \tcode{::operator new}~(\ref{new.delete}), but it is unspecified when or how often this function is called. \pnum \throws \tcode{bad_alloc} if the storage cannot be obtained. \end{itemdescr} \indexlibrarymember{deallocate}{allocator}% \begin{itemdecl} void deallocate(T* p, size_t n); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall be a pointer value obtained from \tcode{allocate()}. \tcode{n} shall equal the value passed as the first argument to the invocation of allocate which returned \tcode{p}. \pnum \effects Deallocates the storage referenced by \tcode{p} . \pnum \remarks Uses \tcode{::operator delete}~(\ref{new.delete}), but it is unspecified when this function is called. \end{itemdescr} \rSec3[allocator.globals]{\tcode{allocator} globals} \indexlibrarymember{operator==}{allocator}% \begin{itemdecl} template bool operator==(const allocator&, const allocator&) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true}. \end{itemdescr} \indexlibrarymember{operator"!=}{allocator}% \begin{itemdecl} template bool operator!=(const allocator&, const allocator&) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{false}. \end{itemdescr} \rSec2[specialized.algorithms]{Specialized algorithms} \pnum Throughout this subclause, the names of template parameters are used to express type requirements. \begin{itemize} \item If an algorithm's template parameter is named \tcode{InputIterator}, the template argument shall satisfy the requirements of an input iterator~(\ref{input.iterators}). \item If an algorithm's template parameter is named \tcode{ForwardIterator}, the template argument shall satisfy the requirements of a forward iterator~(\ref{forward.iterators}), and is required to have the property that no exceptions are thrown from increment, assignment, comparison, or indirection through valid iterators. \end{itemize} Unless otherwise specified, if an exception is thrown in the following algorithms there are no effects. \rSec3[specialized.addressof]{\tcode{addressof}} \indexlibrary{\idxcode{addressof}}% \begin{itemdecl} template constexpr T* addressof(T& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The actual address of the object or function referenced by \tcode{r}, even in the presence of an overloaded \tcode{operator\&}. \pnum \remarks An expression \tcode{addressof(E)} is a constant subexpression~(\ref{defns.const.subexpr}) if \tcode{E} is an lvalue constant subexpression. \end{itemdescr} \rSec3[uninitialized.construct.default]{\tcode{uninitialized_default_construct}} \indexlibrary{\idxcode{uninitialized_default_construct}}% \begin{itemdecl} template void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; first != last; ++first) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type; \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{uninitialized_default_construct_n}}% \begin{itemdecl} template ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; n>0; (void)++first, --n) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type; return first; \end{codeblock} \end{itemdescr} \rSec3[uninitialized.construct.value]{\tcode{uninitialized_value_construct}} \indexlibrary{\idxcode{uninitialized_value_construct}}% \begin{itemdecl} template void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; first != last; ++first) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type(); \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{uninitialized_value_construct_n}}% \begin{itemdecl} template ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; n>0; (void)++first, --n) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type(); return first; \end{codeblock} \end{itemdescr} \rSec3[uninitialized.copy]{\tcode{uninitialized_copy}} \indexlibrary{\idxcode{uninitialized_copy}}% \begin{itemdecl} template ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \begin{codeblock} for (; first != last; ++result, (void) ++first) ::new (static_cast(addressof(*result))) typename iterator_traits::value_type(*first); \end{codeblock} \pnum \returns \tcode{result}. \end{itemdescr} \indexlibrary{\idxcode{uninitialized_copy_n}}% \begin{itemdecl} template ForwardIterator uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \begin{codeblock} for ( ; n > 0; ++result, (void) ++first, --n) { ::new (static_cast(addressof(*result))) typename iterator_traits::value_type(*first); } \end{codeblock} \pnum \returns \tcode{result}. \end{itemdescr} \rSec3[uninitialized.move]{\tcode{uninitialized_move}} \indexlibrary{\idxcode{uninitialized_move}}% \begin{itemdecl} template ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; first != last; (void)++result, ++first) ::new (static_cast(addressof(*result))) typename iterator_traits::value_type(std::move(*first)); return result; \end{codeblock} \pnum \remarks If an exception is thrown, some objects in the range \range{first}{last} are left in a valid but unspecified state. \end{itemdescr} \indexlibrary{\idxcode{uninitialized_move_n}}% \begin{itemdecl} template pair uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; n > 0; ++result, (void) ++first, --n) ::new (static_cast(addressof(*result))) typename iterator_traits::value_type(std::move(*first)); return {first,result}; \end{codeblock} \pnum \remarks If an exception is thrown, some objects in the range \range{first}{std::next(first,n)} are left in a valid but unspecified state. \end{itemdescr} \rSec3[uninitialized.fill]{\tcode{uninitialized_fill}} \indexlibrary{\idxcode{uninitialized_fill}}% \begin{itemdecl} template void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \begin{codeblock} for (; first != last; ++first) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type(x); \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{uninitialized_fill_n}}% \begin{itemdecl} template ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \begin{codeblock} for (; n--; ++first) ::new (static_cast(addressof(*first))) typename iterator_traits::value_type(x); return first; \end{codeblock} \end{itemdescr} \rSec3[specialized.destroy]{\tcode{destroy}} \indexlibrary{\idxcode{destroy_at}}% \begin{itemdecl} template void destroy_at(T* location); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} location->~T(); \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{destroy}}% \begin{itemdecl} template void destroy(ForwardIterator first, ForwardIterator last); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; first!=last; ++first) destroy_at(addressof(*first)); \end{codeblock} \end{itemdescr} \indexlibrary{\idxcode{destroy_n}}% \begin{itemdecl} template ForwardIterator destroy_n(ForwardIterator first, Size n); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} for (; n > 0; (void)++first, --n) destroy_at(addressof(*first)); return first; \end{codeblock} \end{itemdescr} \rSec2[c.malloc]{C library memory allocation} \pnum \indextext{\idxhdr{cstdlib}}% \begin{note} The header \tcode{}~(\ref{cstdlib.syn}) declares the functions described in this subclause. \end{note} \indexlibrary{\idxcode{aligned_alloc}}% \indexlibrary{\idxcode{calloc}}% \indexlibrary{\idxcode{malloc}}% \indexlibrary{\idxcode{realloc}}% \begin{itemdecl} void* aligned_alloc(size_t alignment, size_t size); void* calloc(size_t nmemb, size_t size); void* malloc(size_t size); void* realloc(void* ptr, size_t size); \end{itemdecl} \begin{itemdescr} \pnum \effects These functions have the semantics specified in the C standard library. \pnum \remarks These functions do not attempt to allocate storage by calling \tcode{::operator new()}~(\ref{support.dynamic}). \indexlibrary{\idxcode{new}!\idxcode{operator}}% \pnum Storage allocated directly with these functions is implicitly declared reachable (see~\ref{basic.stc.dynamic.safety}) on allocation, ceases to be declared reachable on deallocation, and need not cease to be declared reachable as the result of an \tcode{undeclare_reachable()} call. \begin{note} This allows existing C libraries to remain unaffected by restrictions on pointers that are not safely derived, at the expense of providing far fewer garbage collection and leak detection options for \tcode{malloc()}-allocated objects. It also allows \tcode{malloc()} to be implemented with a separate allocation arena, bypassing the normal \tcode{declare_reachable()} implementation. The above functions should never intentionally be used as a replacement for \tcode{declare_reachable()}, and newly written code is strongly encouraged to treat memory allocated with these functions as though it were allocated with \tcode{operator new}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{free}}% \begin{itemdecl} void free(void* ptr); \end{itemdecl} \begin{itemdescr} \pnum \effects This function has the semantics specified in the C standard library. \pnum \remarks This function does not attempt to deallocate storage by calling \tcode{::operator delete()}\indexlibrary{\idxcode{delete}!\idxcode{operator}}. \end{itemdescr} \xref ISO C 7.22.3 \rSec1[smartptr]{Smart pointers} \rSec2[unique.ptr]{Class template \tcode{unique_ptr}} \pnum A \defn{unique pointer} is an object that owns another object and manages that other object through a pointer. More precisely, a unique pointer is an object \textit{u} that stores a pointer to a second object \textit{p} and will dispose of \textit{p} when \textit{u} is itself destroyed (e.g., when leaving block scope~(\ref{stmt.dcl})). In this context, \textit{u} is said to \defn{own} \tcode{p}. \pnum The mechanism by which \textit{u} disposes of \textit{p} is known as \textit{p}'s associated \defn{deleter}, a function object whose correct invocation results in \textit{p}'s appropriate disposition (typically its deletion). \pnum Let the notation \textit{u.p} denote the pointer stored by \textit{u}, and let \textit{u.d} denote the associated deleter. Upon request, \textit{u} can \defn{reset} (replace) \textit{u.p} and \textit{u.d} with another pointer and deleter, but must properly dispose of its owned object via the associated deleter before such replacement is considered completed. \pnum Additionally, \textit{u} can, upon request, \defn{transfer ownership} to another unique pointer \textit{u2}. Upon completion of such a transfer, the following postconditions hold: \begin{itemize} \item \textit{u2.p} is equal to the pre-transfer \textit{u.p}, \item \textit{u.p} is equal to \tcode{nullptr}, and \item if the pre-transfer \textit{u.d} maintained state, such state has been transferred to \textit{u2.d}. \end{itemize} As in the case of a reset, \textit{u2} must properly dispose of its pre-transfer owned object via the pre-transfer associated deleter before the ownership transfer is considered complete. \begin{note} A deleter's state need never be copied, only moved or swapped as ownership is transferred. \end{note} \pnum Each object of a type \tcode{U} instantiated from the \tcode{unique_ptr} template specified in this subclause has the strict ownership semantics, specified above, of a unique pointer. In partial satisfaction of these semantics, each such \tcode{U} is \tcode{MoveConstructible} and \tcode{MoveAssignable}, but is not \tcode{CopyConstructible} nor \tcode{CopyAssignable}. The template parameter \tcode{T} of \tcode{unique_ptr} may be an incomplete type. \pnum \begin{note} The uses of \tcode{unique_ptr} include providing exception safety for dynamically allocated memory, passing ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from a function. \end{note} \begin{codeblock} namespace std { template struct default_delete; template struct default_delete; template> class unique_ptr; template class unique_ptr; template unique_ptr make_unique(Args&&... args); template unique_ptr make_unique(size_t n); template @\unspec@ make_unique(Args&&...) = delete; template void swap(unique_ptr& x, unique_ptr& y) noexcept; template bool operator==(const unique_ptr& x, const unique_ptr& y); template bool operator!=(const unique_ptr& x, const unique_ptr& y); template bool operator<(const unique_ptr& x, const unique_ptr& y); template bool operator<=(const unique_ptr& x, const unique_ptr& y); template bool operator>(const unique_ptr& x, const unique_ptr& y); template bool operator>=(const unique_ptr& x, const unique_ptr& y); template bool operator==(const unique_ptr& x, nullptr_t) noexcept; template bool operator==(nullptr_t, const unique_ptr& y) noexcept; template bool operator!=(const unique_ptr& x, nullptr_t) noexcept; template bool operator!=(nullptr_t, const unique_ptr& y) noexcept; template bool operator<(const unique_ptr& x, nullptr_t); template bool operator<(nullptr_t, const unique_ptr& y); template bool operator<=(const unique_ptr& x, nullptr_t); template bool operator<=(nullptr_t, const unique_ptr& y); template bool operator>(const unique_ptr& x, nullptr_t); template bool operator>(nullptr_t, const unique_ptr& y); template bool operator>=(const unique_ptr& x, nullptr_t); template bool operator>=(nullptr_t, const unique_ptr& y); } \end{codeblock} \rSec3[unique.ptr.dltr]{Default deleters} \rSec4[unique.ptr.dltr.general]{In general} \pnum The class template \tcode{default_delete} serves as the default deleter (destruction policy) for the class template \tcode{unique_ptr}. \pnum The template parameter \tcode{T} of \tcode{default_delete} may be an incomplete type. \rSec4[unique.ptr.dltr.dflt]{\tcode{default_delete}} \begin{codeblock} namespace std { template struct default_delete { constexpr default_delete() noexcept = default; template default_delete(const default_delete&) noexcept; void operator()(T*) const; }; } \end{codeblock} \indexlibrary{\idxcode{default_delete}!constructor}% \begin{itemdecl} template default_delete(const default_delete& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a \tcode{default_delete} object from another \tcode{default_delete} object. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{U*} is implicitly convertible to \tcode{T*}. \end{itemdescr} \indexlibrarymember{operator()}{default_delete}% \begin{itemdecl} void operator()(T* ptr) const; \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{delete} on \tcode{ptr}. \pnum \remarks If \tcode{T} is an incomplete type, the program is ill-formed. \end{itemdescr} \rSec4[unique.ptr.dltr.dflt1]{\tcode{default_delete}} \begin{codeblock} namespace std { template struct default_delete { constexpr default_delete() noexcept = default; template default_delete(const default_delete&) noexcept; template void operator()(U* ptr) const; }; } \end{codeblock} \indexlibrary{\idxcode{default_delete}!constructor} \begin{itemdecl} template default_delete(const default_delete& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects constructs a \tcode{default_delete} object from another \tcode{default_delete} object. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{U(*)[]} is convertible to \tcode{T(*)[]}. \end{itemdescr} \indexlibrarymember{operator()}{default_delete}% \begin{itemdecl} template void operator()(U* ptr) const; \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{delete[]} on \tcode{ptr}. \pnum \remarks If \tcode{U} is an incomplete type, the program is ill-formed. This function shall not participate in overload resolution unless \tcode{U(*)[]} is convertible to \tcode{T(*)[]}. \end{itemdescr} \rSec3[unique.ptr.single]{\tcode{unique_ptr} for single objects} \indexlibrary{\idxcode{unique_ptr}}% \begin{codeblock} namespace std { template > class unique_ptr { public: using pointer = @\seebelow@; using element_type = T; using deleter_type = D; // \ref{unique.ptr.single.ctor}, constructors constexpr unique_ptr() noexcept; explicit unique_ptr(pointer p) noexcept; unique_ptr(pointer p, @\seebelow@ d1) noexcept; unique_ptr(pointer p, @\seebelow@ d2) noexcept; unique_ptr(unique_ptr&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; template unique_ptr(unique_ptr&& u) noexcept; // \ref{unique.ptr.single.dtor}, destructor ~unique_ptr(); // \ref{unique.ptr.single.asgn}, assignment unique_ptr& operator=(unique_ptr&& u) noexcept; template unique_ptr& operator=(unique_ptr&& u) noexcept; unique_ptr& operator=(nullptr_t) noexcept; // \ref{unique.ptr.single.observers}, observers add_lvalue_reference_t operator*() const; pointer operator->() const noexcept; pointer get() const noexcept; deleter_type& get_deleter() noexcept; const deleter_type& get_deleter() const noexcept; explicit operator bool() const noexcept; // \ref{unique.ptr.single.modifiers}, modifiers pointer release() noexcept; void reset(pointer p = pointer()) noexcept; void swap(unique_ptr& u) noexcept; // disable copy from lvalue unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; }; } \end{codeblock} \pnum The default type for the template parameter \tcode{D} is \tcode{default_delete}. A client-supplied template argument \tcode{D} shall be a function object type~(\ref{function.objects}), lvalue reference to function, or lvalue reference to function object type for which, given a value \tcode{d} of type \tcode{D} and a value \tcode{ptr} of type \tcode{unique_ptr::pointer}, the expression \tcode{d(ptr)} is valid and has the effect of disposing of the pointer as appropriate for that deleter. \pnum If the deleter's type \tcode{D} is not a reference type, \tcode{D} shall satisfy the requirements of \tcode{Destructible} (Table~\ref{tab:destructible}). \pnum If the \grammarterm{qualified-id} \tcode{remove_reference_t::pointer} is valid and denotes a type~(\ref{temp.deduct}), then \tcode{unique_ptr::pointer} shall be a synonym for \tcode{remove_reference_t::pointer}. Otherwise \tcode{unique_ptr::pointer} shall be a synonym for \tcode{element_type*}. The type \tcode{unique_ptr::pointer} shall satisfy the requirements of \tcode{NullablePointer} (\ref{nullablepointer.requirements}). \pnum \begin{example} Given an allocator type \tcode{X}~(\ref{allocator.requirements}) and letting \tcode{A} be a synonym for \tcode{allocator_traits}, the types \tcode{A::pointer}, \tcode{A::const_pointer}, \tcode{A::void_pointer}, and \tcode{A::const_void_pointer} may be used as \tcode{unique_ptr::pointer}. \end{example} \rSec4[unique.ptr.single.ctor]{\tcode{unique_ptr} constructors} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} constexpr unique_ptr() noexcept; constexpr unique_ptr(nullptr_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{D} shall satisfy the requirements of \tcode{DefaultConstructible} (Table~\ref{tab:defaultconstructible}), and that construction shall not throw an exception. \pnum \effects Constructs a \tcode{unique_ptr} object that owns nothing, value-initializing the stored pointer and the stored deleter. \pnum \postconditions \tcode{get() == nullptr}. \tcode{get_deleter()} returns a reference to the stored deleter. \pnum \remarks If \tcode{is_pointer_v} is \tcode{true} or \tcode{is_default_constructible_v} is \tcode{false}, this constructor shall not participate in overload resolution. \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} explicit unique_ptr(pointer p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{D} shall satisfy the requirements of \tcode{DefaultConstructible} (Table~\ref{tab:defaultconstructible}), and that construction shall not throw an exception. \pnum \effects Constructs a \tcode{unique_ptr} which owns \tcode{p}, initializing the stored pointer with \tcode{p} and value-initializing the stored deleter. \pnum \postconditions \tcode{get() == p}. \tcode{get_deleter()} returns a reference to the stored deleter. \pnum \remarks If \tcode{is_pointer_v} is \tcode{true} or \tcode{is_default_constructible_v} is \tcode{false}, this constructor shall not participate in overload resolution. If class template argument deduction~(\ref{over.match.class.deduct}) would select the function template corresponding to this constructor, then the program is ill-formed. \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} unique_ptr(pointer p, @\seebelow@ d1) noexcept; unique_ptr(pointer p, @\seebelow@ d2) noexcept; \end{itemdecl} \begin{itemdescr} \pnum The signature of these constructors depends upon whether \tcode{D} is a reference type. If \tcode{D} is a non-reference type \tcode{A}, then the signatures are: \begin{codeblock} unique_ptr(pointer p, const A& d) noexcept; unique_ptr(pointer p, A&& d) noexcept; \end{codeblock} \pnum If \tcode{D} is an lvalue reference type \tcode{A\&}, then the signatures are: \begin{codeblock} unique_ptr(pointer p, A& d) noexcept; unique_ptr(pointer p, A&& d) = delete; \end{codeblock} \pnum If \tcode{D} is an lvalue reference type \tcode{const A\&}, then the signatures are: \begin{codeblock} unique_ptr(pointer p, const A& d) noexcept; unique_ptr(pointer p, const A&& d) = delete; \end{codeblock} \pnum \effects Constructs a \tcode{unique_ptr} object which owns \tcode{p}, initializing the stored pointer with \tcode{p} and initializing the deleter from \tcode{std::forward(d)}. \pnum \remarks These constructors shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}. \pnum \postconditions \tcode{get() == p}. \tcode{get_deleter()} returns a reference to the stored deleter. If \tcode{D} is a reference type then \tcode{get_deleter()} returns a reference to the lvalue \tcode{d}. \pnum \remarks If class template argument deduction~(\ref{over.match.class.deduct}) would select a function template corresponding to either of these constructors, then the program is ill-formed. \pnum \begin{example} \begin{codeblock} D d; unique_ptr p1(new int, D()); // \tcode{D} must be \tcode{MoveConstructible} unique_ptr p2(new int, d); // \tcode{D} must be \tcode{CopyConstructible} unique_ptr p3(new int, d); // \tcode{p3} holds a reference to \tcode{d} unique_ptr p4(new int, D()); // error: rvalue deleter object combined // with reference deleter type \end{codeblock} \end{example} \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} unique_ptr(unique_ptr&& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires If \tcode{D} is not a reference type, \tcode{D} shall satisfy the requirements of \tcode{MoveConstructible} (Table~\ref{tab:moveconstructible}). Construction of the deleter from an rvalue of type \tcode{D} shall not throw an exception. \pnum \effects Constructs a \tcode{unique_ptr} by transferring ownership from \tcode{u} to \tcode{*this}. If \tcode{D} is a reference type, this deleter is copy constructed from \tcode{u}'s deleter; otherwise, this deleter is move constructed from \tcode{u}'s deleter. \begin{note} The deleter constructor can be implemented with \tcode{std::forward}. \end{note} \pnum \postconditions \tcode{get()} yields the value \tcode{u.get()} yielded before the construction. \tcode{get_deleter()} returns a reference to the stored deleter that was constructed from \tcode{u.get_deleter()}. If \tcode{D} is a reference type then \tcode{get_deleter()} and \tcode{u.get_deleter()} both reference the same lvalue deleter. \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} template unique_ptr(unique_ptr&& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires If \tcode{E} is not a reference type, construction of the deleter from an rvalue of type \tcode{E} shall be well formed and shall not throw an exception. Otherwise, \tcode{E} is a reference type and construction of the deleter from an lvalue of type \tcode{E} shall be well formed and shall not throw an exception. \pnum \remarks This constructor shall not participate in overload resolution unless: \begin{itemize} \item \tcode{unique_ptr::pointer} is implicitly convertible to \tcode{pointer}, \item \tcode{U} is not an array type, and \item either \tcode{D} is a reference type and \tcode{E} is the same type as \tcode{D}, or \tcode{D} is not a reference type and \tcode{E} is implicitly convertible to \tcode{D}. \end{itemize} \pnum \effects Constructs a \tcode{unique_ptr} by transferring ownership from \tcode{u} to \tcode{*this}. If \tcode{E} is a reference type, this deleter is copy constructed from \tcode{u}'s deleter; otherwise, this deleter is move constructed from \tcode{u}'s deleter. \begin{note} The deleter constructor can be implemented with \tcode{std::forward}. \end{note} \pnum \postconditions \tcode{get()} yields the value \tcode{u.get()} yielded before the construction. \tcode{get_deleter()} returns a reference to the stored deleter that was constructed from \tcode{u.get_deleter()}. \end{itemdescr} \rSec4[unique.ptr.single.dtor]{\tcode{unique_ptr} destructor} \indexlibrary{\idxcode{unique_ptr}!destructor}% \begin{itemdecl} ~unique_ptr(); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{get_deleter()(get())} shall be well formed, shall have well-defined behavior, and shall not throw exceptions. \begin{note} The use of \tcode{default_delete} requires \tcode{T} to be a complete type. \end{note} \pnum \effects If \tcode{get() == nullptr} there are no effects. Otherwise \tcode{get_deleter()(get())}. \end{itemdescr} \rSec4[unique.ptr.single.asgn]{\tcode{unique_ptr} assignment} \indexlibrarymember{operator=}{unique_ptr}% \begin{itemdecl} unique_ptr& operator=(unique_ptr&& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires If \tcode{D} is not a reference type, \tcode{D} shall satisfy the requirements of \tcode{MoveAssignable} (Table~\ref{tab:moveassignable}) and assignment of the deleter from an rvalue of type \tcode{D} shall not throw an exception. Otherwise, \tcode{D} is a reference type; \tcode{remove_reference_t} shall satisfy the \tcode{CopyAssignable} requirements and assignment of the deleter from an lvalue of type \tcode{D} shall not throw an exception. \pnum \effects Transfers ownership from \tcode{u} to \tcode{*this} as if by calling \tcode{reset(u.release())} followed by \tcode{get_deleter() = std::forward(u.get_deleter())}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{unique_ptr}% \begin{itemdecl} template unique_ptr& operator=(unique_ptr&& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires If \tcode{E} is not a reference type, assignment of the deleter from an rvalue of type \tcode{E} shall be well-formed and shall not throw an exception. Otherwise, \tcode{E} is a reference type and assignment of the deleter from an lvalue of type \tcode{E} shall be well-formed and shall not throw an exception. \pnum \remarks This operator shall not participate in overload resolution unless: \begin{itemize} \item \tcode{unique_ptr::pointer} is implicitly convertible to \tcode{pointer}, and \item \tcode{U} is not an array type, and \item \tcode{is_assignable_v} is \tcode{true}. \end{itemize} \pnum \effects Transfers ownership from \tcode{u} to \tcode{*this} as if by calling \tcode{reset(u.release())} followed by \tcode{get_deleter() = std::forward(u.get_deleter())}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{unique_ptr}% \begin{itemdecl} unique_ptr& operator=(nullptr_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{reset()}. \pnum \postconditions \tcode{get() == nullptr}. \pnum \returns \tcode{*this}. \end{itemdescr} \rSec4[unique.ptr.single.observers]{\tcode{unique_ptr} observers} \indexlibrarymember{operator*}{unique_ptr}% \begin{itemdecl} add_lvalue_reference_t operator*() const; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get() != nullptr}. \pnum \returns \tcode{*get()}. \end{itemdescr} \indexlibrarymember{operator->}{unique_ptr}% \begin{itemdecl} pointer operator->() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get() != nullptr}. \pnum \returns \tcode{get()}. \pnum \begin{note} The use of this function typically requires that \tcode{T} be a complete type. \end{note} \end{itemdescr} \indexlibrarymember{get}{unique_ptr}% \begin{itemdecl} pointer get() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The stored pointer. \end{itemdescr} \indexlibrarymember{get_deleter}{unique_ptr}% \begin{itemdecl} deleter_type& get_deleter() noexcept; const deleter_type& get_deleter() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A reference to the stored deleter. \end{itemdescr} \indexlibrarymember{operator bool}{unique_ptr}% \begin{itemdecl} explicit operator bool() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{get() != nullptr}. \end{itemdescr} \rSec4[unique.ptr.single.modifiers]{\tcode{unique_ptr} modifiers} \indexlibrarymember{release}{unique_ptr}% \begin{itemdecl} pointer release() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \postconditions \tcode{get() == nullptr}. \pnum \returns The value \tcode{get()} had at the start of the call to \tcode{release}. \end{itemdescr} \indexlibrarymember{reset}{unique_ptr}% \begin{itemdecl} void reset(pointer p = pointer()) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{get_deleter()(get())} shall be well formed, shall have well-defined behavior, and shall not throw exceptions. \pnum \effects Assigns \tcode{p} to the stored pointer, and then if and only if the old value of the stored pointer, \tcode{old_p}, was not equal to \tcode{nullptr}, calls \tcode{get_deleter()(old_p)}. \begin{note} The order of these operations is significant because the call to \tcode{get_deleter()} may destroy \tcode{*this}. \end{note} \pnum \postconditions \tcode{get() == p}. \begin{note} The postcondition does not hold if the call to \tcode{get_deleter()} destroys \tcode{*this} since \tcode{this->get()} is no longer a valid expression. \end{note} \end{itemdescr} \indexlibrarymember{swap}{unique_ptr}% \begin{itemdecl} void swap(unique_ptr& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{get_deleter()} shall be swappable~(\ref{swappable.requirements}) and shall not throw an exception under \tcode{swap}. \pnum \effects Invokes \tcode{swap} on the stored pointers and on the stored deleters of \tcode{*this} and \tcode{u}. \end{itemdescr} \rSec3[unique.ptr.runtime]{\tcode{unique_ptr} for array objects with a runtime length} \indexlibrary{\idxcode{unique_ptr}}% \begin{codeblock} namespace std { template class unique_ptr { public: using pointer = @\seebelow@; using element_type = T; using deleter_type = D; // \ref{unique.ptr.runtime.ctor}, constructors constexpr unique_ptr() noexcept; template explicit unique_ptr(U p) noexcept; template unique_ptr(U p, @\seebelow@ d) noexcept; template unique_ptr(U p, @\seebelow@ d) noexcept; unique_ptr(unique_ptr&& u) noexcept; template unique_ptr(unique_ptr&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; // destructor ~unique_ptr(); // assignment unique_ptr& operator=(unique_ptr&& u) noexcept; template unique_ptr& operator=(unique_ptr&& u) noexcept; unique_ptr& operator=(nullptr_t) noexcept; // \ref{unique.ptr.runtime.observers}, observers T& operator[](size_t i) const; pointer get() const noexcept; deleter_type& get_deleter() noexcept; const deleter_type& get_deleter() const noexcept; explicit operator bool() const noexcept; // \ref{unique.ptr.runtime.modifiers}, modifiers pointer release() noexcept; template void reset(U p) noexcept; void reset(nullptr_t = nullptr) noexcept; void swap(unique_ptr& u) noexcept; // disable copy from lvalue unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; }; } \end{codeblock} \pnum A specialization for array types is provided with a slightly altered interface. \begin{itemize} \item Conversions between different types of \tcode{unique_ptr} that would be disallowed for the corresponding pointer-to-array types, and conversions to or from the non-array forms of \tcode{unique_ptr}, produce an ill-formed program. \item Pointers to types derived from \tcode{T} are rejected by the constructors, and by \tcode{reset}. \item The observers \tcode{operator*} and \tcode{operator->} are not provided. \item The indexing observer \tcode{operator[]} is provided. \item The default deleter will call \tcode{delete[]}. \end{itemize} \pnum Descriptions are provided below only for members that differ from the primary template. \pnum The template argument \tcode{T} shall be a complete type. \rSec4[unique.ptr.runtime.ctor]{\tcode{unique_ptr} constructors} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} template explicit unique_ptr(U p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum This constructor behaves the same as the constructor in the primary template that takes a single parameter of type \tcode{pointer} except that it additionally shall not participate in overload resolution unless \begin{itemize} \item \tcode{U} is the same type as \tcode{pointer}, or \item \tcode{pointer} is the same type as \tcode{element_type*}, \tcode{U} is a pointer type \tcode{V*}, and \tcode{V(*)[]} is convertible to \tcode{element_type(*)[]}. \end{itemize} \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} template unique_ptr(U p, @\seebelow@ d) noexcept; template unique_ptr(U p, @\seebelow@ d) noexcept; \end{itemdecl} \begin{itemdescr} \pnum These constructors behave the same as the constructors in the primary template that take a parameter of type \tcode{pointer} and a second parameter except that they shall not participate in overload resolution unless either \begin{itemize} \item \tcode{U} is the same type as \tcode{pointer}, \item \tcode{U} is \tcode{nullptr_t}, or \item \tcode{pointer} is the same type as \tcode{element_type*}, \tcode{U} is a pointer type \tcode{V*}, and \tcode{V(*)[]} is convertible to \tcode{element_type(*)[]}. \end{itemize} \end{itemdescr} \indexlibrary{\idxcode{unique_ptr}!constructor}% \begin{itemdecl} template unique_ptr(unique_ptr&& u) noexcept; \end{itemdecl} \begin{itemdescr} \pnum This constructor behaves the same as in the primary template, except that it shall not participate in overload resolution unless all of the following conditions hold, where \tcode{UP} is \tcode{unique_ptr}: \begin{itemize} \item \tcode{U} is an array type, and \item \tcode{pointer} is the same type as \tcode{element_type*}, and \item \tcode{UP::pointer} is the same type as \tcode{UP::element_type*}, and \item \tcode{UP::element_type(*)[]} is convertible to \tcode{element_type(*)[]}, and \item either \tcode{D} is a reference type and \tcode{E} is the same type as \tcode{D}, or \tcode{D} is not a reference type and \tcode{E} is implicitly convertible to \tcode{D}. \end{itemize} \begin{note} This replaces the overload-resolution specification of the primary template \end{note} \end{itemdescr} \rSec4[unique.ptr.runtime.asgn]{\tcode{unique_ptr} assignment} \indexlibrarymember{operator=}{unique_ptr}% \begin{itemdecl} template unique_ptr& operator=(unique_ptr&& u)noexcept; \end{itemdecl} \begin{itemdescr} \pnum This operator behaves the same as in the primary template, except that it shall not participate in overload resolution unless all of the following conditions hold, where \tcode{UP} is \tcode{unique_ptr}: \begin{itemize} \item \tcode{U} is an array type, and \item \tcode{pointer} is the same type as \tcode{element_type*}, and \item \tcode{UP::pointer} is the same type as \tcode{UP::element_type*}, and \item \tcode{UP::element_type(*)[]} is convertible to \tcode{element_type(*)[]}, and \item \tcode{is_assignable_v} is \tcode{true}. \end{itemize} \begin{note} This replaces the overload-resolution specification of the primary template \end{note} \end{itemdescr} \rSec4[unique.ptr.runtime.observers]{\tcode{unique_ptr} observers} \indexlibrarymember{operator[]}{unique_ptr}% \begin{itemdecl} T& operator[](size_t i) const; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{i <} the number of elements in the array to which the stored pointer points. \pnum \returns \tcode{get()[i]}. \end{itemdescr} \rSec4[unique.ptr.runtime.modifiers]{\tcode{unique_ptr} modifiers} \indexlibrarymember{reset}{unique_ptr}% \begin{itemdecl} void reset(nullptr_t p = nullptr) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{reset(pointer())}. \end{itemdescr} \indexlibrarymember{reset}{unique_ptr}% \begin{itemdecl} template void reset(U p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum This function behaves the same as the \tcode{reset} member of the primary template, except that it shall not participate in overload resolution unless either \begin{itemize} \item \tcode{U} is the same type as \tcode{pointer}, or \item \tcode{pointer} is the same type as \tcode{element_type*}, \tcode{U} is a pointer type \tcode{V*}, and \tcode{V(*)[]} is convertible to \tcode{element_type(*)[]}. \end{itemize} \end{itemdescr} \rSec3[unique.ptr.create]{\tcode{unique_ptr} creation} \indexlibrary{\idxcode{make_unique}}% \begin{itemdecl} template unique_ptr make_unique(Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{T} is not an array. \pnum \returns \tcode{unique_ptr(new T(std::forward(args)...))}. \end{itemdescr} \indexlibrary{\idxcode{make_unique}}% \begin{itemdecl} template unique_ptr make_unique(size_t n); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{T} is an array of unknown bound. \pnum \returns \tcode{unique_ptr(new remove_extent_t[n]())}. \end{itemdescr} \indexlibrary{\idxcode{make_unique}}% \begin{itemdecl} template @\unspec@ make_unique(Args&&...) = delete; \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{T} is an array of known bound. \end{itemdescr} \rSec3[unique.ptr.special]{\tcode{unique_ptr} specialized algorithms} \indexlibrary{\idxcode{swap(unique_ptr\&, unique_ptr\&)}}% \begin{itemdecl} template void swap(unique_ptr& x, unique_ptr& y) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{is_swappable_v} is \tcode{true}. \pnum \effects Calls \tcode{x.swap(y)}. \end{itemdescr} \indexlibrarymember{operator==}{unique_ptr}% \begin{itemdecl} template bool operator==(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{x.get() == y.get()}. \end{itemdescr} \indexlibrarymember{operator"!=}{unique_ptr}% \begin{itemdecl} template bool operator!=(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{x.get() != y.get()}. \end{itemdescr} \indexlibrarymember{operator<}{unique_ptr}% \begin{itemdecl} template bool operator<(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \requires Let \tcode{\placeholder{CT}} denote \begin{codeblock} common_type_t::pointer, typename unique_ptr::pointer> \end{codeblock} Then the specialization \tcode{less<\placeholder{CT}>} shall be a function object type~(\ref{function.objects}) that induces a strict weak ordering~(\ref{alg.sorting}) on the pointer values. \pnum \returns \tcode{less<\placeholder{CT}>()(x.get(), y.get())}. \pnum \remarks If \tcode{unique_ptr::pointer} is not implicitly convertible to \tcode{\placeholder{CT}} or \tcode{unique_ptr::pointer} is not implicitly convertible to \tcode{\placeholder{CT}}, the program is ill-formed. \end{itemdescr} \indexlibrarymember{operator<=}{unique_ptr}% \begin{itemdecl} template bool operator<=(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(y < x)}. \end{itemdescr} \indexlibrarymember{operator>}{unique_ptr}% \begin{itemdecl} template bool operator>(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{y < x}. \end{itemdescr} \indexlibrarymember{operator>=}{unique_ptr}% \begin{itemdecl} template bool operator>=(const unique_ptr& x, const unique_ptr& y); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(x < y)}. \end{itemdescr} \indexlibrarymember{operator==}{unique_ptr}% \begin{itemdecl} template bool operator==(const unique_ptr& x, nullptr_t) noexcept; template bool operator==(nullptr_t, const unique_ptr& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!x}. \end{itemdescr} \indexlibrarymember{operator"!=}{unique_ptr}% \begin{itemdecl} template bool operator!=(const unique_ptr& x, nullptr_t) noexcept; template bool operator!=(nullptr_t, const unique_ptr& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{(bool)x}. \end{itemdescr} \indexlibrarymember{operator<}{unique_ptr}% \begin{itemdecl} template bool operator<(const unique_ptr& x, nullptr_t); template bool operator<(nullptr_t, const unique_ptr& x); \end{itemdecl} \begin{itemdescr} \pnum \requires The specialization \tcode{less::pointer>} shall be a function object type~(\ref{function.objects}) that induces a strict weak ordering~(\ref{alg.sorting}) on the pointer values. \pnum \returns The first function template returns \tcode{less::pointer>()(x.get(),\\nullptr)}. The second function template returns \tcode{less::pointer>()(nullptr, x.get())}. \end{itemdescr} \indexlibrarymember{operator>}{unique_ptr}% \begin{itemdecl} template bool operator>(const unique_ptr& x, nullptr_t); template bool operator>(nullptr_t, const unique_ptr& x); \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{nullptr < x}. The second function template returns \tcode{x < nullptr}. \end{itemdescr} \indexlibrarymember{operator<=}{unique_ptr}% \begin{itemdecl} template bool operator<=(const unique_ptr& x, nullptr_t); template bool operator<=(nullptr_t, const unique_ptr& x); \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{!(nullptr < x)}. The second function template returns \tcode{!(x < nullptr)}. \end{itemdescr} \indexlibrarymember{operator>=}{unique_ptr}% \begin{itemdecl} template bool operator>=(const unique_ptr& x, nullptr_t); template bool operator>=(nullptr_t, const unique_ptr& x); \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{!(x < nullptr)}. The second function template returns \tcode{!(nullptr < x)}. \end{itemdescr} \indextext{smart pointers|(}% \rSec2[util.smartptr]{Shared-ownership pointers} \rSec3[util.smartptr.weak.bad]{Class \tcode{bad_weak_ptr}} \indexlibrary{\idxcode{bad_weak_ptr}}% \begin{codeblock} namespace std { class bad_weak_ptr : public exception { public: bad_weak_ptr() noexcept; }; } \end{codeblock} \pnum An exception of type \tcode{bad_weak_ptr} is thrown by the \tcode{shared_ptr} constructor taking a \tcode{weak_ptr}. \indexlibrary{\idxcode{bad_weak_ptr}!constructor}% \indexlibrarymember{what}{bad_weak_ptr}% \begin{itemdecl} bad_weak_ptr() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\postconditions \tcode{what()} returns an \impldef{return value of \tcode{bad_weak_ptr::what}} \ntbs. \end{itemdescr} \rSec3[util.smartptr.shared]{Class template \tcode{shared_ptr}} \pnum \indexlibrary{\idxcode{shared_ptr}}% The \tcode{shared_ptr} class template stores a pointer, usually obtained via \tcode{new}. \tcode{shared_ptr} implements semantics of shared ownership; the last remaining owner of the pointer is responsible for destroying the object, or otherwise releasing the resources associated with the stored pointer. A \tcode{shared_ptr} is said to be empty if it does not own a pointer. \begin{codeblock} namespace std { template class shared_ptr { public: using element_type = remove_extent_t; using weak_type = weak_ptr; // \ref{util.smartptr.shared.const}, constructors constexpr shared_ptr() noexcept; template explicit shared_ptr(Y* p); template shared_ptr(Y* p, D d); template shared_ptr(Y* p, D d, A a); template shared_ptr(nullptr_t p, D d); template shared_ptr(nullptr_t p, D d, A a); template shared_ptr(const shared_ptr& r, element_type* p) noexcept; shared_ptr(const shared_ptr& r) noexcept; template shared_ptr(const shared_ptr& r) noexcept; shared_ptr(shared_ptr&& r) noexcept; template shared_ptr(shared_ptr&& r) noexcept; template explicit shared_ptr(const weak_ptr& r); template shared_ptr(unique_ptr&& r); constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } // \ref{util.smartptr.shared.dest}, destructor ~shared_ptr(); // \ref{util.smartptr.shared.assign}, assignment shared_ptr& operator=(const shared_ptr& r) noexcept; template shared_ptr& operator=(const shared_ptr& r) noexcept; shared_ptr& operator=(shared_ptr&& r) noexcept; template shared_ptr& operator=(shared_ptr&& r) noexcept; template shared_ptr& operator=(unique_ptr&& r); // \ref{util.smartptr.shared.mod}, modifiers void swap(shared_ptr& r) noexcept; void reset() noexcept; template void reset(Y* p); template void reset(Y* p, D d); template void reset(Y* p, D d, A a); // \ref{util.smartptr.shared.obs}, observers element_type* get() const noexcept; T& operator*() const noexcept; T* operator->() const noexcept; element_type& operator[](ptrdiff_t i) const; long use_count() const noexcept; explicit operator bool() const noexcept; template bool owner_before(const shared_ptr& b) const noexcept; template bool owner_before(const weak_ptr& b) const noexcept; }; template shared_ptr(weak_ptr) -> shared_ptr; template shared_ptr(unique_ptr) -> shared_ptr; // \ref{util.smartptr.shared.create}, \tcode{shared_ptr} creation template shared_ptr make_shared(Args&&... args); template shared_ptr allocate_shared(const A& a, Args&&... args); // \ref{util.smartptr.shared.cmp}, \tcode{shared_ptr} comparisons template bool operator==(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator!=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator<(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator>(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator<=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator>=(const shared_ptr& a, const shared_ptr& b) noexcept; template bool operator==(const shared_ptr& a, nullptr_t) noexcept; template bool operator==(nullptr_t, const shared_ptr& b) noexcept; template bool operator!=(const shared_ptr& a, nullptr_t) noexcept; template bool operator!=(nullptr_t, const shared_ptr& b) noexcept; template bool operator<(const shared_ptr& a, nullptr_t) noexcept; template bool operator<(nullptr_t, const shared_ptr& b) noexcept; template bool operator<=(const shared_ptr& a, nullptr_t) noexcept; template bool operator<=(nullptr_t, const shared_ptr& b) noexcept; template bool operator>(const shared_ptr& a, nullptr_t) noexcept; template bool operator>(nullptr_t, const shared_ptr& b) noexcept; template bool operator>=(const shared_ptr& a, nullptr_t) noexcept; template bool operator>=(nullptr_t, const shared_ptr& b) noexcept; // \ref{util.smartptr.shared.spec}, \tcode{shared_ptr} specialized algorithms template void swap(shared_ptr& a, shared_ptr& b) noexcept; // \ref{util.smartptr.shared.cast}, \tcode{shared_ptr} casts template shared_ptr static_pointer_cast(const shared_ptr& r) noexcept; template shared_ptr dynamic_pointer_cast(const shared_ptr& r) noexcept; template shared_ptr const_pointer_cast(const shared_ptr& r) noexcept; template shared_ptr reinterpret_pointer_cast(const shared_ptr& r) noexcept; // \ref{util.smartptr.getdeleter}, \tcode{shared_ptr} \tcode{get_deleter} template D* get_deleter(const shared_ptr& p) noexcept; // \ref{util.smartptr.shared.io}, \tcode{shared_ptr} I/O template basic_ostream& operator<< (basic_ostream& os, const shared_ptr& p); } \end{codeblock} \pnum Specializations of \tcode{shared_ptr} shall be \tcode{CopyConstructible}, \tcode{CopyAssignable}, and \tcode{LessThanComparable}, allowing their use in standard containers. Specializations of \tcode{shared_ptr} shall be contextually convertible to \tcode{bool}, allowing their use in boolean expressions and declarations in conditions. The template parameter \tcode{T} of \tcode{shared_ptr} may be an incomplete type. \pnum \begin{example} \begin{codeblock} if (shared_ptr px = dynamic_pointer_cast(py)) { // do something with \tcode{px} } \end{codeblock} \end{example} \pnum For purposes of determining the presence of a data race, member functions shall access and modify only the \tcode{shared_ptr} and \tcode{weak_ptr} objects themselves and not objects they refer to. Changes in \tcode{use_count()} do not reflect modifications that can introduce data races. \pnum For the purposes of subclause \ref{util.smartptr}, a pointer type \tcode{Y*} is said to be \defnx{compatible with}{compatible with!\idxcode{shared_ptr}} a pointer type \tcode{T*} when either \tcode{Y*} is convertible to \tcode{T*} or \tcode{Y} is \tcode{U[N]} and \tcode{T} is \cv{}~\tcode{U[]}. \rSec4[util.smartptr.shared.const]{\tcode{shared_ptr} constructors} \pnum In the constructor definitions below, enables \tcode{shared_from_this} with \tcode{p}, for a pointer \tcode{p} of type \tcode{Y*}, means that if \tcode{Y} has an unambiguous and accessible base class that is a specialization of \tcode{enable_shared_from_this}~(\ref{util.smartptr.enab}), then \tcode{remove_cv_t*} shall be implicitly convertible to \tcode{T*} and the constructor evaluates the statement: \begin{codeblock} if (p != nullptr && p->weak_this.expired()) p->weak_this = shared_ptr>(*this, const_cast*>(p)); \end{codeblock} The assignment to the \tcode{weak_this} member is not atomic and conflicts with any potentially concurrent access to the same object~(\ref{intro.multithread}). \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} constexpr shared_ptr() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Constructs an empty \tcode{shared_ptr} object. \pnum\postconditions \tcode{use_count() == 0 \&\& get() == nullptr}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} template explicit shared_ptr(Y* p); \end{itemdecl} \begin{itemdescr} \pnum\requires \tcode{Y} shall be a complete type. The expression \tcode{delete[] p}, when \tcode{T} is an array type, or \tcode{delete p}, when \tcode{T} is not an array type, shall have well-defined behavior, and shall not throw exceptions. \pnum\effects When \tcode{T} is not an array type, constructs a \tcode{shared_ptr} object that owns the pointer \tcode{p}. Otherwise, constructs a \tcode{shared_ptr} that owns \tcode{p} and a deleter of an unspecified type that calls \tcode{delete[] p}. When \tcode{T} is not an array type, enables \tcode{shared_from_this} with \tcode{p}. If an exception is thrown, \tcode{delete p} is called when \tcode{T} is not an array type, \tcode{delete[] p} otherwise. \pnum\postconditions \tcode{use_count() == 1 \&\& get() == p}. \pnum\throws \tcode{bad_alloc}, or an \impldef{exception type when \tcode{shared_ptr} constructor fails} exception when a resource other than memory could not be obtained. \pnum\remarks When \tcode{T} is an array type, this constructor shall not participate in overload resolution unless the expression \tcode{delete[] p} is well-formed and either \tcode{T} is \tcode{U[N]} and \tcode{Y(*)[N]} is convertible to \tcode{T*}, or \tcode{T} is \tcode{U[]} and \tcode{Y(*)[]} is convertible to \tcode{T*}. When \tcode{T} is not an array type, this constructor shall not participate in overload resolution unless the expression \tcode{delete p} is well-formed and \tcode{Y*} is convertible to \tcode{T*}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} template shared_ptr(Y* p, D d); template shared_ptr(Y* p, D d, A a); template shared_ptr(nullptr_t p, D d); template shared_ptr(nullptr_t p, D d, A a); \end{itemdecl} \begin{itemdescr} \pnum\requires Construction of \tcode{d} and a deleter of type \tcode{D} initialized with \tcode{std::move(d)} shall not throw exceptions. The expression \tcode{d(p)} shall have well-defined behavior and shall not throw exceptions. \tcode{A} shall be an allocator~(\ref{allocator.requirements}). \pnum\effects Constructs a \tcode{shared_ptr} object that owns the object \tcode{p} and the deleter \tcode{d}. When \tcode{T} is not an array type, the first and second constructors enable \tcode{shared_from_this} with \tcode{p}. The second and fourth constructors shall use a copy of \tcode{a} to allocate memory for internal use. If an exception is thrown, \tcode{d(p)} is called. \pnum\postconditions \tcode{use_count() == 1 \&\& get() == p}. \pnum\throws \tcode{bad_alloc}, or an \impldef{exception type when \tcode{shared_ptr} constructor fails} exception when a resource other than memory could not be obtained. \pnum\remarks When \tcode{T} is an array type, this constructor shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true}, the expression \tcode{d(p)} is well-formed, and either \tcode{T} is \tcode{U[N]} and \tcode{Y(*)[N]} is convertible to \tcode{T*}, or \tcode{T} is \tcode{U[]} and \tcode{Y(*)[]} is convertible to \tcode{T*}. When \tcode{T} is not an array type, this constructor shall not participate in overload resolution unless \tcode{is_move_constructible_v} is \tcode{true}, the expression \tcode{d(p)} is well-formed, and \tcode{Y*} is convertible to \tcode{T*}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} template shared_ptr(const shared_ptr& r, element_type* p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a \tcode{shared_ptr} instance that stores \tcode{p} and shares ownership with \tcode{r}. \pnum \postconditions \tcode{get() == p \&\& use_count() == r.use_count()}. \pnum \begin{note} To avoid the possibility of a dangling pointer, the user of this constructor must ensure that \tcode{p} remains valid at least until the ownership group of \tcode{r} is destroyed. \end{note} \pnum \begin{note} This constructor allows creation of an empty \tcode{shared_ptr} instance with a non-null stored pointer. \end{note} \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} shared_ptr(const shared_ptr& r) noexcept; template shared_ptr(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\remarks The second constructor shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*}. \pnum\effects If \tcode{r} is empty, constructs an empty \tcode{shared_ptr} object; otherwise, constructs a \tcode{shared_ptr} object that shares ownership with \tcode{r}. \pnum\postconditions \tcode{get() == r.get() \&\& use_count() == r.use_count()}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \begin{itemdecl} shared_ptr(shared_ptr&& r) noexcept; template shared_ptr(shared_ptr&& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \remarks The second constructor shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*}. \pnum \effects Move constructs a \tcode{shared_ptr} instance from \tcode{r}. \pnum \postconditions \tcode{*this} shall contain the old value of \tcode{r}. \tcode{r} shall be empty. \tcode{r.get() == nullptr}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \indexlibrary{\idxcode{weak_ptr}}% \begin{itemdecl} template explicit shared_ptr(const weak_ptr& r); \end{itemdecl} \begin{itemdescr} \pnum\effects Constructs a \tcode{shared_ptr} object that shares ownership with \tcode{r} and stores a copy of the pointer stored in \tcode{r}. If an exception is thrown, the constructor has no effect. \pnum\postconditions \tcode{use_count() == r.use_count()}. \pnum\throws \tcode{bad_weak_ptr} when \tcode{r.expired()}. \pnum\remarks This constructor shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*}. \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}!constructor}% \indexlibrary{\idxcode{unique_ptr}}% \begin{itemdecl} template shared_ptr(unique_ptr&& r); \end{itemdecl} \begin{itemdescr} \pnum\remarks This constructor shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*} and \tcode{unique_ptr::pointer} is convertible to \tcode{element_type*}. \pnum \effects If \tcode{r.get() == nullptr}, equivalent to \tcode{shared_ptr()}. Otherwise, if \tcode{D} is not a reference type, equivalent to \tcode{shared_ptr(r.release(), r.get_deleter())}. Otherwise, equivalent to \tcode{shared_ptr(r.release(), ref(r.get_deleter()))}. If an exception is thrown, the constructor has no effect. \end{itemdescr} \rSec4[util.smartptr.shared.dest]{\tcode{shared_ptr} destructor} \indexlibrary{\idxcode{shared_ptr}!destructor}% \begin{itemdecl} ~shared_ptr(); \end{itemdecl} \begin{itemdescr} \pnum\effects \begin{itemize} \item If \tcode{*this} is empty or shares ownership with another \tcode{shared_ptr} instance (\tcode{use_count() > 1}), there are no side effects. \item Otherwise, if \tcode{*this} owns an object \tcode{p} and a deleter \tcode{d}, \tcode{d(p)} is called. \item Otherwise, \tcode{*this} owns a pointer \tcode{p}, and \tcode{delete p} is called. \end{itemize} \end{itemdescr} \pnum \begin{note} Since the destruction of \tcode{*this} decreases the number of instances that share ownership with \tcode{*this} by one, after \tcode{*this} has been destroyed all \tcode{shared_ptr} instances that shared ownership with \tcode{*this} will report a \tcode{use_count()} that is one less than its previous value. \end{note} \rSec4[util.smartptr.shared.assign]{\tcode{shared_ptr} assignment} \indexlibrarymember{operator=}{shared_ptr}% \begin{itemdecl} shared_ptr& operator=(const shared_ptr& r) noexcept; template shared_ptr& operator=(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{shared_ptr(r).swap(*this)}. \pnum\returns \tcode{*this}. \pnum \begin{note} The use count updates caused by the temporary object construction and destruction are not observable side effects, so the implementation may meet the effects (and the implied guarantees) via different means, without creating a temporary. In particular, in the example: \begin{codeblock} shared_ptr p(new int); shared_ptr q(p); p = p; q = p; \end{codeblock} both assignments may be no-ops. \end{note} \end{itemdescr} \indexlibrarymember{operator=}{shared_ptr}% \begin{itemdecl} shared_ptr& operator=(shared_ptr&& r) noexcept; template shared_ptr& operator=(shared_ptr&& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{shared_ptr(std::move(r)).swap(*this)}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{shared_ptr}% \begin{itemdecl} template shared_ptr& operator=(unique_ptr&& r); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{shared_ptr(std::move(r)).swap(*this)}. \pnum \returns \tcode{*this}. \end{itemdescr} \rSec4[util.smartptr.shared.mod]{\tcode{shared_ptr} modifiers} \indexlibrarymember{swap}{shared_ptr}% \begin{itemdecl} void swap(shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Exchanges the contents of \tcode{*this} and \tcode{r}. \end{itemdescr} \indexlibrarymember{reset}{shared_ptr}% \begin{itemdecl} void reset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{shared_ptr().swap(*this)}. \end{itemdescr} \indexlibrarymember{reset}{shared_ptr}% \begin{itemdecl} template void reset(Y* p); \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{shared_ptr(p).swap(*this)}. \end{itemdescr} \indexlibrarymember{reset}{shared_ptr}% \begin{itemdecl} template void reset(Y* p, D d); \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{shared_ptr(p, d).swap(*this)}. \end{itemdescr} \indexlibrarymember{reset}{shared_ptr}% \begin{itemdecl} template void reset(Y* p, D d, A a); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{shared_ptr(p, d, a).swap(*this)}. \end{itemdescr} \rSec4[util.smartptr.shared.obs]{\tcode{shared_ptr} observers} \indexlibrarymember{get}{shared_ptr}% \begin{itemdecl} element_type* get() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns The stored pointer. \end{itemdescr} \indexlibrarymember{operator*}{shared_ptr}% \begin{itemdecl} T& operator*() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\requires \tcode{get() != 0}. \pnum\returns \tcode{*get()}. \pnum\remarks When \tcode{T} is an array type or \cv{}~\tcode{void}, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed. \end{itemdescr} \indexlibrarymember{operator->}{shared_ptr}% \begin{itemdecl} T* operator->() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\requires \tcode{get() != 0}. \pnum\returns \tcode{get()}. \pnum\remarks When \tcode{T} is an array type, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed. \end{itemdescr} \indexlibrarymember{operator[]}{shared_ptr}% \begin{itemdecl} element_type& operator[](ptrdiff_t i) const; \end{itemdecl} \begin{itemdescr} \pnum\requires \tcode{get() != 0 \&\& i >= 0}. If \tcode{T} is \tcode{U[N]}, \tcode{i < N}. \pnum\returns \tcode{get()[i]}. \pnum\remarks When \tcode{T} is not an array type, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed. \pnum\throws Nothing. \end{itemdescr} \indexlibrarymember{use_count}{shared_ptr}% \begin{itemdecl} long use_count() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns The number of \tcode{shared_ptr} objects, \tcode{*this} included, that share ownership with \tcode{*this}, or \tcode{0} when \tcode{*this} is empty. \pnum\sync None. \pnum \begin{note} \tcode{get() == nullptr} does not imply a specific return value of \tcode{use_count()}. \end{note} \pnum \begin{note} \tcode{weak_ptr::lock()} can affect the return value of \tcode{use_count()}. \end{note} \pnum \begin{note} When multiple threads can affect the return value of \tcode{use_count()}, the result should be treated as approximate. In particular, \tcode{use_count() == 1} does not imply that accesses through a previously destroyed \tcode{shared_ptr} have in any sense completed. \end{note} \end{itemdescr} \indexlibrarymember{operator bool}{shared_ptr}% \begin{itemdecl} explicit operator bool() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{get() != 0}. \end{itemdescr} \indexlibrarymember{owner_before}{shared_ptr}% \begin{itemdecl} template bool owner_before(const shared_ptr& b) const noexcept; template bool owner_before(const weak_ptr& b) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns An unspecified value such that \begin{itemize} \item \tcode{x.owner_before(y)} defines a strict weak ordering as defined in~\ref{alg.sorting}; \item under the equivalence relation defined by \tcode{owner_before}, \tcode{!a.owner_before(b) \&\& !b.owner_before(a)}, two \tcode{shared_ptr} or \tcode{weak_ptr} instances are equivalent if and only if they share ownership or are both empty. \end{itemize} \end{itemdescr} \rSec4[util.smartptr.shared.create]{\tcode{shared_ptr} creation} \indexlibrary{\idxcode{make_shared}}% \indexlibrary{\idxcode{allocate_shared}}% \begin{itemdecl} template shared_ptr make_shared(Args&&... args); template shared_ptr allocate_shared(const A& a, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{::new (pv) T(std::forward(args)...)}, where \tcode{pv} has type \tcode{void*} and points to storage suitable to hold an object of type \tcode{T}, shall be well formed. \tcode{A} shall be an allocator~(\ref{allocator.requirements}). The copy constructor and destructor of \tcode{A} shall not throw exceptions. \pnum \effects Allocates memory suitable for an object of type \tcode{T} and constructs an object in that memory via the placement \grammarterm{new-expression} \tcode{::new (pv) T(std::forward(args)...)}. The template \tcode{allocate_shared} uses a copy of \tcode{a} to allocate memory. If an exception is thrown, the functions have no effect. \pnum \returns A \tcode{shared_ptr} instance that stores and owns the address of the newly constructed object of type \tcode{T}. \pnum \postconditions \tcode{get() != 0 \&\& use_count() == 1}. \pnum \throws \tcode{bad_alloc}, or an exception thrown from \tcode{A::allocate} or from the constructor of \tcode{T}. \pnum \remarks The \tcode{shared_ptr} constructor called by this function enables \tcode{shared_from_this} with the address of the newly constructed object of type \tcode{T}. Implementations should perform no more than one memory allocation. \begin{note} This provides efficiency equivalent to an intrusive smart pointer. \end{note} \pnum \begin{note} These functions will typically allocate more memory than \tcode{sizeof(T)} to allow for internal bookkeeping structures such as the reference counts. \end{note} \end{itemdescr} \rSec4[util.smartptr.shared.cmp]{\tcode{shared_ptr} comparison} \indexlibrarymember{operator==}{shared_ptr}% \begin{itemdecl} template bool operator==(const shared_ptr& a, const shared_ptr& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{a.get() == b.get()}. \end{itemdescr} \indexlibrarymember{operator<}{shared_ptr}% \begin{itemdecl} template bool operator<(const shared_ptr& a, const shared_ptr& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{less<>()(a.get(), b.get())}. \pnum \begin{note} Defining a comparison function allows \tcode{shared_ptr} objects to be used as keys in associative containers. \end{note} \end{itemdescr} \indexlibrarymember{operator==}{shared_ptr}% \begin{itemdecl} template bool operator==(const shared_ptr& a, nullptr_t) noexcept; template bool operator==(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!a}. \end{itemdescr} \indexlibrarymember{operator"!=}{shared_ptr}% \begin{itemdecl} template bool operator!=(const shared_ptr& a, nullptr_t) noexcept; template bool operator!=(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{(bool)a}. \end{itemdescr} \indexlibrarymember{operator<}{shared_ptr}% \begin{itemdecl} template bool operator<(const shared_ptr& a, nullptr_t) noexcept; template bool operator<(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{less::element_type*>()(a.get(), nullptr)}. The second function template returns \tcode{less::element_type*>()(nullptr, a.get())}. \end{itemdescr} \indexlibrarymember{operator>}{shared_ptr}% \begin{itemdecl} template bool operator>(const shared_ptr& a, nullptr_t) noexcept; template bool operator>(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{nullptr < a}. The second function template returns \tcode{a < nullptr}. \end{itemdescr} \indexlibrarymember{operator<=}{shared_ptr}% \begin{itemdecl} template bool operator<=(const shared_ptr& a, nullptr_t) noexcept; template bool operator<=(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{!(nullptr < a)}. The second function template returns \tcode{!(a < nullptr)}. \end{itemdescr} \indexlibrarymember{operator>=}{shared_ptr}% \begin{itemdecl} template bool operator>=(const shared_ptr& a, nullptr_t) noexcept; template bool operator>=(nullptr_t, const shared_ptr& a) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The first function template returns \tcode{!(a < nullptr)}. The second function template returns \tcode{!(nullptr < a)}. \end{itemdescr} \rSec4[util.smartptr.shared.spec]{\tcode{shared_ptr} specialized algorithms} \indexlibrarymember{swap}{shared_ptr}% \begin{itemdecl} template void swap(shared_ptr& a, shared_ptr& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{a.swap(b)}. \end{itemdescr} \rSec4[util.smartptr.shared.cast]{\tcode{shared_ptr} casts} \indexlibrarymember{static_pointer_cast}{shared_ptr}% \begin{itemdecl} template shared_ptr static_pointer_cast(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{static_cast((U*)nullptr)} shall be well formed. \pnum \returns \begin{codeblock} shared_ptr(r, static_cast::element_type*>(r.get())) \end{codeblock} \pnum \begin{note} The seemingly equivalent expression \tcode{shared_ptr(static_cast(r.get()))} will eventually result in undefined behavior, attempting to delete the same object twice. \end{note} \end{itemdescr} \indexlibrarymember{dynamic_pointer_cast}{shared_ptr}% \begin{itemdecl} template shared_ptr dynamic_pointer_cast(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{dynamic_cast((U*)nullptr)} shall be well formed and shall have well defined behavior. \pnum \returns \begin{itemize} \item When \tcode{dynamic_cast::element_type*>(r.get())} returns a non-null value \tcode{p}, \tcode{shared_ptr(r, p)}. \item Otherwise, \tcode{shared_ptr()}. \end{itemize} \pnum \begin{note} The seemingly equivalent expression \tcode{shared_ptr(dynamic_cast(r.get()))} will eventually result in undefined behavior, attempting to delete the same object twice. \end{note} \end{itemdescr} \indexlibrarymember{const_pointer_cast}{shared_ptr}% \begin{itemdecl} template shared_ptr const_pointer_cast(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{const_cast((U*)nullptr)} shall be well formed. \pnum \returns \begin{codeblock} shared_ptr(r, const_cast::element_type*>(r.get())) \end{codeblock} \pnum \begin{note} The seemingly equivalent expression \tcode{shared_ptr(const_cast(r.get()))} will eventually result in undefined behavior, attempting to delete the same object twice. \end{note} \end{itemdescr} \indexlibrarymember{reinterpret_pointer_cast}{shared_ptr}% \begin{itemdecl} template shared_ptr reinterpret_pointer_cast(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \requires The expression \tcode{reinterpret_cast((U*)nullptr)} shall be well formed. \pnum\returns \begin{codeblock} shared_ptr(r, reinterpret_cast::element_type*>(r.get())) \end{codeblock} \pnum \begin{note} The seemingly equivalent expression \tcode{shared_ptr(reinterpret_cast(r.get()))} will eventually result in undefined behavior, attempting to delete the same object twice. \end{note} \end{itemdescr} \rSec4[util.smartptr.getdeleter]{\tcode{get_deleter}} \indexlibrarymember{get_deleter}{shared_ptr}% \begin{itemdecl} template D* get_deleter(const shared_ptr& p) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns If \tcode{p} owns a deleter \tcode{d} of type cv-unqualified \tcode{D}, returns \tcode{addressof(d)}; otherwise returns \tcode{nullptr}. The returned pointer remains valid as long as there exists a \tcode{shared_ptr} instance that owns \tcode{d}. \begin{note} It is unspecified whether the pointer remains valid longer than that. This can happen if the implementation doesn't destroy the deleter until all \tcode{weak_ptr} instances that share ownership with \tcode{p} have been destroyed. \end{note} \end{itemdescr} \rSec4[util.smartptr.shared.io]{\tcode{shared_ptr} I/O} \indexlibrarymember{operator<<}{shared_ptr}% \begin{itemdecl} template basic_ostream& operator<< (basic_ostream& os, const shared_ptr& p); \end{itemdecl} \begin{itemdescr} \pnum\effects As if by: \tcode{os <{}< p.get();} \pnum\returns \tcode{os}. \end{itemdescr} \rSec3[util.smartptr.weak]{Class template \tcode{weak_ptr}} \pnum \indexlibrary{\idxcode{weak_ptr}}% The \tcode{weak_ptr} class template stores a weak reference to an object that is already managed by a \tcode{shared_ptr}. To access the object, a \tcode{weak_ptr} can be converted to a \tcode{shared_ptr} using the member function \tcode{lock}. \begin{codeblock} namespace std { template class weak_ptr { public: using element_type = T; // \ref{util.smartptr.weak.const}, constructors constexpr weak_ptr() noexcept; template weak_ptr(const shared_ptr& r) noexcept; weak_ptr(const weak_ptr& r) noexcept; template weak_ptr(const weak_ptr& r) noexcept; weak_ptr(weak_ptr&& r) noexcept; template weak_ptr(weak_ptr&& r) noexcept; // \ref{util.smartptr.weak.dest}, destructor ~weak_ptr(); // \ref{util.smartptr.weak.assign}, assignment weak_ptr& operator=(const weak_ptr& r) noexcept; template weak_ptr& operator=(const weak_ptr& r) noexcept; template weak_ptr& operator=(const shared_ptr& r) noexcept; weak_ptr& operator=(weak_ptr&& r) noexcept; template weak_ptr& operator=(weak_ptr&& r) noexcept; // \ref{util.smartptr.weak.mod}, modifiers void swap(weak_ptr& r) noexcept; void reset() noexcept; // \ref{util.smartptr.weak.obs}, observers long use_count() const noexcept; bool expired() const noexcept; shared_ptr lock() const noexcept; template bool owner_before(const shared_ptr& b) const; template bool owner_before(const weak_ptr& b) const; }; template weak_ptr(shared_ptr) -> weak_ptr; // \ref{util.smartptr.weak.spec}, specialized algorithms template void swap(weak_ptr& a, weak_ptr& b) noexcept; } \end{codeblock} \pnum Specializations of \tcode{weak_ptr} shall be \tcode{CopyConstructible} and \tcode{CopyAssignable}, allowing their use in standard containers. The template parameter \tcode{T} of \tcode{weak_ptr} may be an incomplete type. \rSec4[util.smartptr.weak.const]{\tcode{weak_ptr} constructors} \indexlibrary{\idxcode{weak_ptr}!constructor}% \begin{itemdecl} constexpr weak_ptr() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Constructs an empty \tcode{weak_ptr} object. \pnum\postconditions \tcode{use_count() == 0}. \end{itemdescr} \indexlibrary{\idxcode{weak_ptr}!constructor}% \begin{itemdecl} weak_ptr(const weak_ptr& r) noexcept; template weak_ptr(const weak_ptr& r) noexcept; template weak_ptr(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\remarks The second and third constructors shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*}. \pnum\effects If \tcode{r} is empty, constructs an empty \tcode{weak_ptr} object; otherwise, constructs a \tcode{weak_ptr} object that shares ownership with \tcode{r} and stores a copy of the pointer stored in \tcode{r}. \pnum\postconditions \tcode{use_count() == r.use_count()}. \end{itemdescr} \indexlibrary{\idxcode{weak_ptr}!constructor}% \begin{itemdecl} weak_ptr(weak_ptr&& r) noexcept; template weak_ptr(weak_ptr&& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\remarks The second constructor shall not participate in overload resolution unless \tcode{Y*} is compatible with \tcode{T*}. \pnum\effects Move constructs a \tcode{weak_ptr} instance from \tcode{r}. \pnum\postconditions \tcode{*this} shall contain the old value of \tcode{r}. \tcode{r} shall be empty. \tcode{r.use_count() == 0}. \end{itemdescr} \rSec4[util.smartptr.weak.dest]{\tcode{weak_ptr} destructor} \indexlibrary{\idxcode{weak_ptr}!destructor}% \begin{itemdecl} ~weak_ptr(); \end{itemdecl} \begin{itemdescr} \pnum\effects Destroys this \tcode{weak_ptr} object but has no effect on the object its stored pointer points to. \end{itemdescr} \rSec4[util.smartptr.weak.assign]{\tcode{weak_ptr} assignment} \indexlibrarymember{operator=}{weak_ptr}% \begin{itemdecl} weak_ptr& operator=(const weak_ptr& r) noexcept; template weak_ptr& operator=(const weak_ptr& r) noexcept; template weak_ptr& operator=(const shared_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{weak_ptr(r).swap(*this)}. \pnum\remarks The implementation may meet the effects (and the implied guarantees) via different means, without creating a temporary. \pnum\returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{weak_ptr}% \begin{itemdecl} weak_ptr& operator=(weak_ptr&& r) noexcept; template weak_ptr& operator=(weak_ptr&& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{weak_ptr(std::move(r)).swap(*this)}. \pnum\returns \tcode{*this}. \end{itemdescr} \rSec4[util.smartptr.weak.mod]{\tcode{weak_ptr} modifiers} \indexlibrarymember{swap}{weak_ptr}% \begin{itemdecl} void swap(weak_ptr& r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Exchanges the contents of \tcode{*this} and \tcode{r}. \end{itemdescr} \indexlibrarymember{reset}{weak_ptr}% \begin{itemdecl} void reset() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{weak_ptr().swap(*this)}. \end{itemdescr} \rSec4[util.smartptr.weak.obs]{\tcode{weak_ptr} observers} \indexlibrarymember{use_count}{weak_ptr}% \begin{itemdecl} long use_count() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{0} if \tcode{*this} is empty; otherwise, the number of \tcode{shared_ptr} instances that share ownership with \tcode{*this}. \end{itemdescr} \indexlibrarymember{expired}{weak_ptr}% \begin{itemdecl} bool expired() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{use_count() == 0}. \end{itemdescr} \indexlibrarymember{lock}{weak_ptr}% \begin{itemdecl} shared_ptr lock() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{expired() ?\ shared_ptr() :\ shared_ptr(*this)}, executed atomically. \end{itemdescr} \indexlibrarymember{owner_before}{weak_ptr}% \begin{itemdecl} template bool owner_before(const shared_ptr& b) const; template bool owner_before(const weak_ptr& b) const; \end{itemdecl} \begin{itemdescr} \pnum \returns An unspecified value such that \begin{itemize} \item \tcode{x.owner_before(y)} defines a strict weak ordering as defined in~\ref{alg.sorting}; \item under the equivalence relation defined by \tcode{owner_before}, \tcode{!a.owner_before(b) \&\& !b.owner_before(a)}, two \tcode{shared_ptr} or \tcode{weak_ptr} instances are equivalent if and only if they share ownership or are both empty. \end{itemize} \end{itemdescr} \rSec4[util.smartptr.weak.spec]{\tcode{weak_ptr} specialized algorithms} \indexlibrarymember{swap}{weak_ptr}% \begin{itemdecl} template void swap(weak_ptr& a, weak_ptr& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Equivalent to \tcode{a.swap(b)}. \end{itemdescr} \rSec3[util.smartptr.ownerless]{Class template \tcode{owner_less}} \pnum The class template \tcode{owner_less} allows ownership-based mixed comparisons of shared and weak pointers. \indexlibrary{\idxcode{owner_less}}% \begin{codeblock} namespace std { template struct owner_less; template struct owner_less> { bool operator()(const shared_ptr&, const shared_ptr&) const noexcept; bool operator()(const shared_ptr&, const weak_ptr&) const noexcept; bool operator()(const weak_ptr&, const shared_ptr&) const noexcept; }; template struct owner_less> { bool operator()(const weak_ptr&, const weak_ptr&) const noexcept; bool operator()(const shared_ptr&, const weak_ptr&) const noexcept; bool operator()(const weak_ptr&, const shared_ptr&) const noexcept; }; template<> struct owner_less { template bool operator()(const shared_ptr&, const shared_ptr&) const noexcept; template bool operator()(const shared_ptr&, const weak_ptr&) const noexcept; template bool operator()(const weak_ptr&, const shared_ptr&) const noexcept; template bool operator()(const weak_ptr&, const weak_ptr&) const noexcept; using is_transparent = @\unspec@; }; } \end{codeblock} \indexlibrarymember{operator()}{owner_less}% \pnum \tcode{operator()(x, y)} shall return \tcode{x.owner_before(y)}. \begin{note} Note that \begin{itemize} \item \tcode{operator()} defines a strict weak ordering as defined in~\ref{alg.sorting}; \item under the equivalence relation defined by \tcode{operator()}, \tcode{!operator()(a, b) \&\& !operator()(b, a)}, two \tcode{shared_ptr} or \tcode{weak_ptr} instances are equivalent if and only if they share ownership or are both empty. \end{itemize} \end{note} \rSec3[util.smartptr.enab]{Class template \tcode{enable_shared_from_this}} \pnum \indexlibrary{\idxcode{enable_shared_from_this}}% A class \tcode{T} can inherit from \tcode{enable_shared_from_this} to inherit the \tcode{shared_from_this} member functions that obtain a \tcode{shared_ptr} instance pointing to \tcode{*this}. \pnum \begin{example} \begin{codeblock} struct X: public enable_shared_from_this { }; int main() { shared_ptr p(new X); shared_ptr q = p->shared_from_this(); assert(p == q); assert(!p.owner_before(q) && !q.owner_before(p)); // p and q share ownership } \end{codeblock} \end{example} \begin{codeblock} namespace std { template class enable_shared_from_this { protected: constexpr enable_shared_from_this() noexcept; enable_shared_from_this(const enable_shared_from_this&) noexcept; enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept; ~enable_shared_from_this(); public: shared_ptr shared_from_this(); shared_ptr shared_from_this() const; weak_ptr weak_from_this() noexcept; weak_ptr weak_from_this() const noexcept; private: mutable weak_ptr weak_this; // \expos }; } \end{codeblock} \pnum The template parameter \tcode{T} of \tcode{enable_shared_from_this} may be an incomplete type. \indexlibrary{\idxcode{enable_shared_from_this}!constructor}% \begin{itemdecl} constexpr enable_shared_from_this() noexcept; enable_shared_from_this(const enable_shared_from_this&) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Value-initializes \tcode{weak_this}. \end{itemdescr} \indexlibrarymember{operator=}{enable_shared_from_this}% \begin{itemdecl} enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{*this}. \pnum\begin{note} \tcode{weak_this} is not changed. \end{note} \end{itemdescr} \indexlibrary{\idxcode{shared_ptr}}% \indexlibrarymember{shared_from_this}{enable_shared_from_this}% \begin{itemdecl} shared_ptr shared_from_this(); shared_ptr shared_from_this() const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{shared_ptr(weak_this)}. \end{itemdescr} \indexlibrary{\idxcode{weak_ptr}}% \indexlibrarymember{weak_from_this}{enable_shared_from_this}% \begin{itemdecl} weak_ptr weak_from_this() noexcept; weak_ptr weak_from_this() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{weak_this}. \end{itemdescr} \rSec3[util.smartptr.shared.atomic]{\tcode{shared_ptr} atomic access} \pnum Concurrent access to a \tcode{shared_ptr} object from multiple threads does not introduce a data race if the access is done exclusively via the functions in this section and the instance is passed as their first argument. \pnum The meaning of the arguments of type \tcode{memory_order} is explained in~\ref{atomics.order}. \indexlibrarymember{atomic_is_lock_free}{shared_ptr}% \begin{itemdecl} template bool atomic_is_lock_free(const shared_ptr* p); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \returns \tcode{true} if atomic access to \tcode{*p} is lock-free, \tcode{false} otherwise. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_load}{shared_ptr}% \begin{itemdecl} template shared_ptr atomic_load(const shared_ptr* p); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \returns \tcode{atomic_load_explicit(p, memory_order_seq_cst)}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_load_explicit}{shared_ptr}% \begin{itemdecl} template shared_ptr atomic_load_explicit(const shared_ptr* p, memory_order mo); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \requires \tcode{mo} shall not be \tcode{memory_order_release} or \tcode{memory_order_acq_rel}. \pnum \returns \tcode{*p}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_store}{shared_ptr}% \begin{itemdecl} template void atomic_store(shared_ptr* p, shared_ptr r); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \effects As if by \tcode{atomic_store_explicit(p, r, memory_order_seq_cst)}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_store_explicit}{shared_ptr}% \begin{itemdecl} template void atomic_store_explicit(shared_ptr* p, shared_ptr r, memory_order mo); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \requires \tcode{mo} shall not be \tcode{memory_order_acquire} or \tcode{memory_order_acq_rel}. \pnum \effects As if by \tcode{p->swap(r)}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_exchange}{shared_ptr}% \begin{itemdecl} template shared_ptr atomic_exchange(shared_ptr* p, shared_ptr r); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \returns \tcode{atomic_exchange_explicit(p, r, memory_order_seq_cst)}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_exchange_explicit}{shared_ptr}% \begin{itemdecl} template shared_ptr atomic_exchange_explicit(shared_ptr* p, shared_ptr r, memory_order mo); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null. \pnum \effects As if by \tcode{p->swap(r)}. \pnum \returns The previous value of \tcode{*p}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_compare_exchange_weak}{shared_ptr}% \begin{itemdecl} template bool atomic_compare_exchange_weak(shared_ptr* p, shared_ptr* v, shared_ptr w); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null and \tcode{v} shall not be null. \pnum \returns \begin{codeblock} atomic_compare_exchange_weak_explicit(p, v, w, memory_order_seq_cst, memory_order_seq_cst) \end{codeblock} \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{atomic_compare_exchange_strong}{shared_ptr}% \begin{itemdecl} template bool atomic_compare_exchange_strong(shared_ptr* p, shared_ptr* v, shared_ptr w); \end{itemdecl} \begin{itemdescr} \pnum \returns \begin{codeblock} atomic_compare_exchange_strong_explicit(p, v, w, memory_order_seq_cst, memory_order_seq_cst) \end{codeblock} \end{itemdescr} \indexlibrarymember{atomic_compare_exchange_weak_explicit}{shared_ptr}% \indexlibrarymember{atomic_compare_exchange_strong_explicit}{shared_ptr}% \begin{itemdecl} template bool atomic_compare_exchange_weak_explicit( shared_ptr* p, shared_ptr* v, shared_ptr w, memory_order success, memory_order failure); template bool atomic_compare_exchange_strong_explicit( shared_ptr* p, shared_ptr* v, shared_ptr w, memory_order success, memory_order failure); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall not be null and \tcode{v} shall not be null. The \tcode{failure} argument shall not be \tcode{memory_order_release} nor \tcode{memory_order_acq_rel}. \pnum \effects If \tcode{*p} is equivalent to \tcode{*v}, assigns \tcode{w} to \tcode{*p} and has synchronization semantics corresponding to the value of \tcode{success}, otherwise assigns \tcode{*p} to \tcode{*v} and has synchronization semantics corresponding to the value of \tcode{failure}. \pnum \returns \tcode{true} if \tcode{*p} was equivalent to \tcode{*v}, \tcode{false} otherwise. \pnum \throws Nothing. \pnum \remarks Two \tcode{shared_ptr} objects are equivalent if they store the same pointer value and share ownership. The weak form may fail spuriously. See~\ref{atomics.types.operations}. \end{itemdescr} \rSec3[util.smartptr.hash]{Smart pointer hash support} \indexlibrary{\idxcode{hash}!\idxcode{unique_ptr}}% \begin{itemdecl} template struct hash>; \end{itemdecl} \begin{itemdescr} \pnum Letting \tcode{UP} be \tcode{unique_ptr}, the specialization \tcode{hash} is enabled~(\ref{unord.hash}) if and only if \tcode{hash} is enabled. When enabled, for an object \tcode{p} of type \tcode{UP}, \tcode{hash()(p)} shall evaluate to the same value as \tcode{hash()(p.get())}. The member functions are not guaranteed to be \tcode{noexcept}. \end{itemdescr} \indexlibrary{\idxcode{hash}!\idxcode{shared_ptr}}% \begin{itemdecl} template struct hash>; \end{itemdecl} \begin{itemdescr} \pnum For an object \tcode{p} of type \tcode{shared_ptr}, \tcode{hash>()(p)} shall evaluate to the same value as \tcode{hash::element_type*>()(p.get())}. \end{itemdescr}% \indextext{smart pointers|)} \rSec1[mem.res]{Memory resources} \rSec2[mem.res.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{memory_resource}}% \indexlibrary{\idxhdr{memory_resource}}% \begin{codeblock} namespace std::pmr { // \ref{mem.res.class}, class \tcode{memory_resource} class memory_resource; bool operator==(const memory_resource& a, const memory_resource& b) noexcept; bool operator!=(const memory_resource& a, const memory_resource& b) noexcept; // \ref{mem.poly.allocator.class}, class template \tcode{polymorphic_allocator} template class polymorphic_allocator; template bool operator==(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; template bool operator!=(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; // \ref{mem.res.global}, global memory resources memory_resource* new_delete_resource() noexcept; memory_resource* null_memory_resource() noexcept; memory_resource* set_default_resource(memory_resource* r) noexcept; memory_resource* get_default_resource() noexcept; // \ref{mem.res.pool}, pool resource classes struct pool_options; class synchronized_pool_resource; class unsynchronized_pool_resource; class monotonic_buffer_resource; } \end{codeblock} \rSec2[mem.res.class]{Class \tcode{memory_resource}} \pnum The \tcode{memory_resource} class is an abstract interface to an unbounded set of classes encapsulating memory resources. \indexlibrary{\idxcode{memory_resource}}% \begin{codeblock} class memory_resource { static constexpr size_t max_align = alignof(max_align_t); // \expos public: virtual ~memory_resource(); void* allocate(size_t bytes, size_t alignment = max_align); void deallocate(void* p, size_t bytes, size_t alignment = max_align); bool is_equal(const memory_resource& other) const noexcept; private: virtual void* do_allocate(size_t bytes, size_t alignment) = 0; virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0; virtual bool do_is_equal(const memory_resource& other) const noexcept = 0; }; \end{codeblock} \rSec3[mem.res.public]{\tcode{memory_resource} public member functions} \indexlibrary{\idxcode{memory_resource}!destructor}% \begin{itemdecl} ~memory_resource(); \end{itemdecl} \begin{itemdescr} \pnum \effects Destroys this \tcode{memory_resource}. \end{itemdescr} \indexlibrarymember{allocate}{memory_resource}% \begin{itemdecl} void* allocate(size_t bytes, size_t alignment = max_align); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return do_allocate(bytes, alignment);} \end{itemdescr} \indexlibrarymember{deallocate}{memory_resource}% \begin{itemdecl} void deallocate(void* p, size_t bytes, size_t alignment = max_align); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{do_deallocate(p, bytes, alignment);} \end{itemdescr} \indexlibrarymember{is_equal}{memory_resource}% \begin{itemdecl} bool is_equal(const memory_resource& other) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \tcode{return do_is_equal(other);} \end{itemdescr} \rSec3[mem.res.private]{\tcode{memory_resource} private virtual member functions} \indexlibrarymember{do_allocate}{memory_resource}% \begin{itemdecl} virtual void* do_allocate(size_t bytes, size_t alignment) = 0; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{alignment} shall be a power of two. \pnum \returns A derived class shall implement this function to return a pointer to allocated storage (\ref{basic.stc.dynamic.deallocation}) with a size of at least \tcode{bytes}. The returned storage is aligned to the specified alignment, if such alignment is supported (\ref{basic.align}); otherwise it is aligned to \tcode{max_align}. \pnum \throws A derived class implementation shall throw an appropriate exception if it is unable to allocate memory with the requested size and alignment. \end{itemdescr} \indexlibrarymember{do_deallocate}{memory_resource}% \begin{itemdecl} virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} shall have been returned from a prior call to \tcode{allocate(bytes, alignment)} on a memory resource equal to \tcode{*this}, and the storage at \tcode{p} shall not yet have been deallocated. \pnum \effects A derived class shall implement this function to dispose of allocated storage. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{do_is_equal}{memory_resource}% \begin{itemdecl} virtual bool do_is_equal(const memory_resource& other) const noexcept = 0; \end{itemdecl} \begin{itemdescr} \pnum \returns A derived class shall implement this function to return \tcode{true} if memory allocated from \tcode{this} can be deallocated from \tcode{other} and vice-versa, otherwise \tcode{false}. \begin{note} The most-derived type of \tcode{other} might not match the type of \tcode{this}. For a derived class \tcode{D}, a typical implementation of this function will immediately return \tcode{false} if \tcode{dynamic_cast(\&other) == nullptr}.\end{note} \end{itemdescr} \rSec3[mem.res.eq]{\tcode{memory_resource} equality} \indexlibrarymember{operator==}{memory_resource}% \begin{itemdecl} bool operator==(const memory_resource& a, const memory_resource& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\&a == \&b || a.is_equal(b)}. \end{itemdescr} \indexlibrarymember{operator"!=}{memory_resource}% \begin{itemdecl} bool operator!=(const memory_resource& a, const memory_resource& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(a == b)}. \end{itemdescr} \rSec2[mem.poly.allocator.class]{Class template \tcode{polymorphic_allocator}} \pnum A specialization of class template \tcode{pmr::polymorphic_allocator} conforms to the \tcode{Allocator} requirements (\ref{allocator.requirements}). Constructed with different memory resources, different instances of the same specialization of \tcode{pmr::polymorphic_allocator} can exhibit entirely different allocation behavior. This runtime polymorphism allows objects that use \tcode{polymorphic_allocator} to behave as if they used different allocator types at run time even though they use the same static allocator type. \indexlibrary{\idxcode{polymorphic_allocator}}% \begin{codeblock} template class polymorphic_allocator { memory_resource* memory_rsrc; // \expos public: using value_type = Tp; // \ref{mem.poly.allocator.ctor}, constructors polymorphic_allocator() noexcept; polymorphic_allocator(memory_resource* r); polymorphic_allocator(const polymorphic_allocator& other) = default; template polymorphic_allocator(const polymorphic_allocator& other) noexcept; polymorphic_allocator& operator=(const polymorphic_allocator& rhs) = delete; // \ref{mem.poly.allocator.mem}, member functions Tp* allocate(size_t n); void deallocate(Tp* p, size_t n); template void construct(T* p, Args&&... args); template void construct(pair* p, piecewise_construct_t, tuple x, tuple y); template void construct(pair* p); template void construct(pair* p, U&& x, V&& y); template void construct(pair* p, const pair& pr); template void construct(pair* p, pair&& pr); template void destroy(T* p); polymorphic_allocator select_on_container_copy_construction() const; memory_resource* resource() const; }; \end{codeblock} \rSec3[mem.poly.allocator.ctor]{\tcode{polymorphic_allocator} constructors} \indexlibrary{\idxcode{polymorphic_allocator}!constructor}% \begin{itemdecl} polymorphic_allocator() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Sets \tcode{memory_rsrc} to \tcode{get_default_resource()}. \end{itemdescr} \indexlibrary{\idxcode{polymorphic_allocator}!constructor}% \begin{itemdecl} polymorphic_allocator(memory_resource* r); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{r} is non-null. \pnum \effects Sets \tcode{memory_rsrc} to \tcode{r}. \pnum \throws Nothing. \pnum \begin{note} This constructor provides an implicit conversion from \tcode{memory_resource*}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{polymorphic_allocator}!constructor}% \begin{itemdecl} template polymorphic_allocator(const polymorphic_allocator& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Sets \tcode{memory_rsrc} to \tcode{other.resource()}. \end{itemdescr} \rSec3[mem.poly.allocator.mem]{\tcode{polymorphic_allocator} member functions} \indexlibrarymember{allocate}{polymorphic_allocator}% \begin{itemdecl} Tp* allocate(size_t n); \end{itemdecl} \begin{itemdescr} \pnum \returns Equivalent to \begin{codeblock} return static_cast(memory_rsrc->allocate(n * sizeof(Tp), alignof(Tp))); \end{codeblock} \end{itemdescr} \indexlibrarymember{deallocate}{polymorphic_allocator}% \begin{itemdecl} void deallocate(Tp* p, size_t n); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{p} was allocated from a memory resource \tcode{x}, equal to \tcode{*memory_rsrc}, using \tcode{x.allocate(n * sizeof(Tp), alignof(Tp))}. \pnum \effects Equivalent to \tcode{memory_rsrc->deallocate(p, n * sizeof(Tp), alignof(Tp))}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(T* p, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \requires Uses-allocator construction of \tcode{T} with allocator \tcode{resource()} (see~\ref{allocator.uses.construction}) and constructor arguments \tcode{std::forward(args)...} is well-formed. \begin{note} Uses-allocator construction is always well formed for types that do not use allocators.\end{note} \pnum \effects Construct a \tcode{T} object in the storage whose address is represented by \tcode{p} by uses-allocator construction with allocator \tcode{resource()} and constructor arguments \tcode{std::forward(args)...}. \pnum \throws Nothing unless the constructor for \tcode{T} throws. \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(pair* p, piecewise_construct_t, tuple x, tuple y); \end{itemdecl} \begin{itemdescr} \pnum \begin{note} This member function and the \tcode{construct} member functions that follow are overloads for piecewise construction of pairs~(\ref{pairs.pair}). \end{note} \pnum \effects Let \tcode{xprime} be a \tcode{tuple} constructed from \tcode{x} according to the appropriate rule from the following list. \begin{note} The following description can be summarized as constructing a \tcode{pair} object in the storage whose address is represented by \tcode{p}, as if by separate uses-allocator construction with allocator \tcode{resource()}~(\ref{allocator.uses.construction}) of \tcode{p->first} using the elements of \tcode{x} and \tcode{p->second} using the elements of \tcode{y}. \end{note} \begin{itemize} \item If \tcode{uses_allocator_v} is \tcode{false} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{xprime} is \tcode{x}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{xprime} is \tcode{tuple_cat(make_tuple(allocator_arg, resource()), std::move(x))}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{xprime} is \tcode{tuple_cat(std::move(x), make_tuple(resource()))}. \item Otherwise the program is ill formed. \end{itemize} Let \tcode{yprime} be a tuple constructed from \tcode{y} according to the appropriate rule from the following list: \begin{itemize} \item If \tcode{uses_allocator_v} is \tcode{false} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{yprime} is \tcode{y}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{yprime} is \tcode{tuple_cat(make_tuple(allocator_arg, resource()), std::move(y))}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} \\ and \tcode{is_constructible_v} is \tcode{true}, \\ then \tcode{yprime} is \tcode{tuple_cat(std::move(y), make_tuple(resource()))}. \item Otherwise the program is ill formed. \end{itemize} Then, using \tcode{piecewise_construct}, \tcode{xprime}, and \tcode{yprime} as the constructor arguments, this function constructs a \tcode{pair} object in the storage whose address is represented by \tcode{p}. \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(pair* p); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, tuple<>(), tuple<>()); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(pair* p, U&& x, V&& y); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(std::forward(x)), forward_as_tuple(std::forward(y))); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(pair* p, const pair& pr); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(pr.first), forward_as_tuple(pr.second)); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{polymorphic_allocator}% \begin{itemdecl} template void construct(pair* p, pair&& pr); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(std::forward(pr.first)), forward_as_tuple(std::forward(pr.second))); \end{codeblock} \end{itemdescr} \indexlibrarymember{destroy}{polymorphic_allocator}% \begin{itemdecl} template void destroy(T* p); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{p->\~T()}. \end{itemdescr} \indexlibrarymember{select_on_container_copy_construction}{polymorphic_allocator}% \begin{itemdecl} polymorphic_allocator select_on_container_copy_construction() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{polymorphic_allocator()}. \pnum \begin{note} The memory resource is not propagated. \end{note} \end{itemdescr} \indexlibrarymember{resource}{polymorphic_allocator}% \begin{itemdecl} memory_resource* resource() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{memory_rsrc}. \end{itemdescr} \rSec3[mem.poly.allocator.eq]{\tcode{polymorphic_allocator} equality} \indexlibrarymember{operator==}{polymorphic_allocator}% \begin{itemdecl} template bool operator==(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{*a.resource() == *b.resource()}. \end{itemdescr} \indexlibrarymember{operator"!=}{polymorphic_allocator}% \begin{itemdecl} template bool operator!=(const polymorphic_allocator& a, const polymorphic_allocator& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(a == b)}. \end{itemdescr} \rSec2[mem.res.global]{Access to program-wide \tcode{memory_resource} objects} \indexlibrary{\idxcode{new_delete_resource}}% \begin{itemdecl} memory_resource* new_delete_resource() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A pointer to a static-duration object of a type derived from \tcode{memory_resource} that can serve as a resource for allocating memory using \tcode{::operator new} and \tcode{::operator delete}. The same value is returned every time this function is called. For a return value \tcode{p} and a memory resource \tcode{r}, \tcode{p->is_equal(r)} returns \tcode{\&r == p}. \end{itemdescr} \indexlibrary{\idxcode{null_memory_resource}}% \begin{itemdecl} memory_resource* null_memory_resource() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A pointer to a static-duration object of a type derived from \tcode{memory_resource} for which \tcode{allocate()} always throws \tcode{bad_alloc} and for which \tcode{deallocate()} has no effect. The same value is returned every time this function is called. For a return value \tcode{p} and a memory resource \tcode{r}, \tcode{p->is_equal(r)} returns \tcode{\&r == p}. \end{itemdescr} \pnum The \defn{default memory resource pointer} is a pointer to a memory resource that is used by certain facilities when an explicit memory resource is not supplied through the interface. Its initial value is the return value of \tcode{new_delete_resource()}. \indexlibrary{\idxcode{set_default_resource}}% \begin{itemdecl} memory_resource* set_default_resource(memory_resource* r) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects If \tcode{r} is non-null, sets the value of the default memory resource pointer to \tcode{r}, otherwise sets the default memory resource pointer to \tcode{new_delete_resource()}. \pnum \postconditions \tcode{get_default_resource() == r}. \pnum \returns The previous value of the default memory resource pointer. \pnum \remarks Calling the \tcode{set_default_resource} and \tcode{get_default_resource} functions shall not incur a data race. A call to the \tcode{set_default_resource} function shall synchronize with subsequent calls to the \tcode{set_default_resource} and \tcode{get_default_resource} functions. \end{itemdescr} \indexlibrary{\idxcode{get_default_resource}}% \begin{itemdecl} memory_resource* get_default_resource() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns The current value of the default memory resource pointer. \end{itemdescr} \rSec2[mem.res.pool]{Pool resource classes} \rSec3[mem.res.pool.overview]{Classes \tcode{synchronized_pool_resource} and \tcode{unsynchronized_pool_resource}} \pnum The \tcode{synchronized_pool_resource} and \tcode{unsynchronized_pool_resource} classes (collectively called \defn{pool resource classes}) are general-purpose memory resources having the following qualities: \begin{itemize} \item Each resource frees its allocated memory on destruction, even if \tcode{deallocate} has not been called for some of the allocated blocks. \item A pool resource consists of a collection of \defn{pools}, serving requests for different block sizes. Each individual pool manages a collection of \defn{chunks} that are in turn divided into blocks of uniform size, returned via calls to \tcode{do_allocate}. Each call to \tcode{do_allocate(size, alignment)} is dispatched to the pool serving the smallest blocks accommodating at least \tcode{size} bytes. \item When a particular pool is exhausted, allocating a block from that pool results in the allocation of an additional chunk of memory from the \defn{upstream allocator} (supplied at construction), thus replenishing the pool. With each successive replenishment, the chunk size obtained increases geometrically. \begin{note} By allocating memory in chunks, the pooling strategy increases the chance that consecutive allocations will be close together in memory.\end{note} \item Allocation requests that exceed the largest block size of any pool are fulfilled directly from the upstream allocator. \item A \tcode{pool_options} struct may be passed to the pool resource constructors to tune the largest block size and the maximum chunk size. \end{itemize} \pnum A \tcode{synchronized_pool_resource} may be accessed from multiple threads without external synchronization and may have thread-specific pools to reduce synchronization costs. An \tcode{unsynchronized_pool_resource} class may not be accessed from multiple threads simultaneously and thus avoids the cost of synchronization entirely in single-threaded applications. \indexlibrary{\idxcode{pool_options}}% \indexlibrary{\idxcode{synchronized_pool_resource}}% \indexlibrary{\idxcode{unsynchronized_pool_resource}}% \begin{codeblock} struct pool_options { size_t max_blocks_per_chunk = 0; size_t largest_required_pool_block = 0; }; class synchronized_pool_resource : public memory_resource { public: synchronized_pool_resource(const pool_options& opts, memory_resource* upstream); synchronized_pool_resource() : synchronized_pool_resource(pool_options(), get_default_resource()) {} explicit synchronized_pool_resource(memory_resource* upstream) : synchronized_pool_resource(pool_options(), upstream) {} explicit synchronized_pool_resource(const pool_options& opts) : synchronized_pool_resource(opts, get_default_resource()) {} synchronized_pool_resource(const synchronized_pool_resource&) = delete; virtual ~synchronized_pool_resource(); synchronized_pool_resource& operator=(const synchronized_pool_resource&) = delete; void release(); memory_resource* upstream_resource() const; pool_options options() const; protected: void *do_allocate(size_t bytes, size_t alignment) override; void do_deallocate(void *p, size_t bytes, size_t alignment) override; bool do_is_equal(const memory_resource& other) const noexcept override; }; class unsynchronized_pool_resource : public memory_resource { public: unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream); unsynchronized_pool_resource() : unsynchronized_pool_resource(pool_options(), get_default_resource()) {} explicit unsynchronized_pool_resource(memory_resource* upstream) : unsynchronized_pool_resource(pool_options(), upstream) {} explicit unsynchronized_pool_resource(const pool_options& opts) : unsynchronized_pool_resource(opts, get_default_resource()) {} unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete; virtual ~unsynchronized_pool_resource(); unsynchronized_pool_resource& operator=(const unsynchronized_pool_resource&) = delete; void release(); memory_resource *upstream_resource() const; pool_options options() const; protected: void* do_allocate(size_t bytes, size_t alignment) override; void do_deallocate(void* p, size_t bytes, size_t alignment) override; bool do_is_equal(const memory_resource& other) const noexcept override; }; \end{codeblock} \rSec3[mem.res.pool.options]{\tcode{pool_options} data members} \pnum The members of \tcode{pool_options} comprise a set of constructor options for pool resources. The effect of each option on the pool resource behavior is described below: \indexlibrary{\idxcode{pool_options}!\idxcode{max_blocks_per_chunk}}% \begin{itemdecl} size_t max_blocks_per_chunk; \end{itemdecl} \begin{itemdescr} \pnum The maximum number of blocks that will be allocated at once from the upstream memory resource~(\ref{mem.res.monotonic.buffer}) to replenish a pool. If the value of \tcode{max_blocks_per_chunk} is zero or is greater than an \impldef{largest supported value to configure the maximum number of blocks to replenish a pool} limit, that limit is used instead. The implementation may choose to use a smaller value than is specified in this field and may use different values for different pools. \end{itemdescr} \indexlibrary{\idxcode{pool_options}!\idxcode{largest_required_pool_block}}% \begin{itemdecl} size_t largest_required_pool_block; \end{itemdecl} \begin{itemdescr} \pnum The largest allocation size that is required to be fulfilled using the pooling mechanism. Attempts to allocate a single block larger than this threshold will be allocated directly from the upstream memory resource. If \tcode{largest_required_pool_block} is zero or is greater than an \impldef{largest supported value to configure the largest allocation satisfied directly by a pool} limit, that limit is used instead. The implementation may choose a pass-through threshold larger than specified in this field. \end{itemdescr} \rSec3[mem.res.pool.ctor]{Pool resource constructors and destructors} \indexlibrary{\idxcode{synchronized_pool_resource}!constructor}% \indexlibrary{\idxcode{unsynchronized_pool_resource}!constructor}% \begin{itemdecl} synchronized_pool_resource(const pool_options& opts, memory_resource* upstream); unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{upstream} is the address of a valid memory resource. \pnum \effects Constructs a pool resource object that will obtain memory from \tcode{upstream} whenever the pool resource is unable to satisfy a memory request from its own internal data structures. The resulting object will hold a copy of \tcode{upstream}, but will not own the resource to which \tcode{upstream} points. \begin{note} The intention is that calls to \tcode{upstream->allocate()} will be substantially fewer than calls to \tcode{this->allocate()} in most cases.\end{note} The behavior of the pooling mechanism is tuned according to the value of the \tcode{opts} argument. \pnum \throws Nothing unless \tcode{upstream->allocate()} throws. It is unspecified if, or under what conditions, this constructor calls \tcode{upstream->allocate()}. \end{itemdescr} \indexlibrary{\idxcode{synchronized_pool_resource}!destructor}% \indexlibrary{\idxcode{unsynchronized_pool_resource}!destructor}% \begin{itemdecl} virtual ~synchronized_pool_resource(); virtual ~unsynchronized_pool_resource(); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{release()}. \end{itemdescr} \rSec3[mem.res.pool.mem]{Pool resource members} \indexlibrarymember{release}{synchronized_pool_resource}% \indexlibrarymember{release}{unsynchronized_pool_resource}% \begin{itemdecl} void release(); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{upstream_resource()->deallocate()} as necessary to release all allocated memory. \begin{note} The memory is released back to \tcode{upstream_resource()} even if \tcode{deallocate} has not been called for some of the allocated blocks.\end{note} \end{itemdescr} \indexlibrarymember{upstream_resource}{synchronized_pool_resource}% \indexlibrarymember{upstream_resource}{unsynchronized_pool_resource}% \begin{itemdecl} memory_resource* upstream_resource() const; \end{itemdecl} \begin{itemdescr} \pnum \returns The value of the \tcode{upstream} argument provided to the constructor of this object. \end{itemdescr} \indexlibrarymember{options}{synchronized_pool_resource}% \indexlibrarymember{options}{unsynchronized_pool_resource}% \begin{itemdecl} pool_options options() const; \end{itemdecl} \begin{itemdescr} \pnum \returns The options that control the pooling behavior of this resource. The values in the returned struct may differ from those supplied to the pool resource constructor in that values of zero will be replaced with \impldef{default configuration of a pool} defaults, and sizes may be rounded to unspecified granularity. \end{itemdescr} \indexlibrarymember{do_allocate}{synchronized_pool_resource}% \indexlibrarymember{do_allocate}{unsynchronized_pool_resource}% \begin{itemdecl} void* do_allocate(size_t bytes, size_t alignment) override; \end{itemdecl} \begin{itemdescr} \pnum \returns A pointer to allocated storage (\ref{basic.stc.dynamic.deallocation}) with a size of at least \tcode{bytes}. The size and alignment of the allocated memory shall meet the requirements for a class derived from \tcode{memory_resource}~(\ref{mem.res}). \pnum \effects If the pool selected for a block of size \tcode{bytes} is unable to satisfy the memory request from its own internal data structures, it will call \tcode{upstream_resource()->allocate()} to obtain more memory. If \tcode{bytes} is larger than that which the largest pool can handle, then memory will be allocated using \tcode{upstream_resource()->allocate()}. \pnum \throws Nothing unless \tcode{upstream_resource()->allocate()} throws. \end{itemdescr} \indexlibrarymember{do_deallocate}{synchronized_pool_resource}% \indexlibrarymember{do_deallocate}{unsynchronized_pool_resource}% \begin{itemdecl} void do_deallocate(void* p, size_t bytes, size_t alignment) override; \end{itemdecl} \begin{itemdescr} \pnum \effects Returns the memory at \tcode{p} to the pool. It is unspecified if, or under what circumstances, this operation will result in a call to \tcode{upstream_resource()->deallocate()}. \pnum \throws Nothing. \end{itemdescr} \indexlibrarymember{do_is_equal}{synchronized_pool_resource}% \begin{itemdecl} bool synchronized_pool_resource::do_is_equal( const memory_resource& other) const noexcept override; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{this == dynamic_cast(\&other)}. \end{itemdescr} \indexlibrarymember{do_is_equal}{unsynchronized_pool_resource}% \begin{itemdecl} bool unsynchronized_pool_resource::do_is_equal( const memory_resource& other) const noexcept override; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{this == dynamic_cast(\&other)}. \end{itemdescr} \rSec2[mem.res.monotonic.buffer]{Class \tcode{monotonic_buffer_resource}} \pnum A \tcode{monotonic_buffer_resource} is a special-purpose memory resource intended for very fast memory allocations in situations where memory is used to build up a few objects and then is released all at once when the memory resource object is destroyed. It has the following qualities: \begin{itemize} \item A call to \tcode{deallocate} has no effect, thus the amount of memory consumed increases monotonically until the resource is destroyed. \item The program can supply an initial buffer, which the allocator uses to satisfy memory requests. \item When the initial buffer (if any) is exhausted, it obtains additional buffers from an \defn{upstream} memory resource supplied at construction. Each additional buffer is larger than the previous one, following a geometric progression. \item It is intended for access from one thread of control at a time. Specifically, calls to \tcode{allocate} and \tcode{deallocate} do not synchronize with one another. \item It frees the allocated memory on destruction, even if \tcode{deallocate} has not been called for some of the allocated blocks. \end{itemize} \indexlibrary{\idxcode{monotonic_buffer_resource}}% \begin{codeblock} class monotonic_buffer_resource : public memory_resource { memory_resource *upstream_rsrc; // \expos void *current_buffer; // \expos size_t next_buffer_size; // \expos public: explicit monotonic_buffer_resource(memory_resource *upstream); monotonic_buffer_resource(size_t initial_size, memory_resource *upstream); monotonic_buffer_resource(void *buffer, size_t buffer_size, memory_resource *upstream); monotonic_buffer_resource() : monotonic_buffer_resource(get_default_resource()) {} explicit monotonic_buffer_resource(size_t initial_size) : monotonic_buffer_resource(initial_size, get_default_resource()) {} monotonic_buffer_resource(void *buffer, size_t buffer_size) : monotonic_buffer_resource(buffer, buffer_size, get_default_resource()) {} monotonic_buffer_resource(const monotonic_buffer_resource&) = delete; virtual ~monotonic_buffer_resource(); monotonic_buffer_resource operator=(const monotonic_buffer_resource&) = delete; void release(); memory_resource* upstream_resource() const; protected: void* do_allocate(size_t bytes, size_t alignment) override; void do_deallocate(void* p, size_t bytes, size_t alignment) override; bool do_is_equal(const memory_resource& other) const noexcept override; }; \end{codeblock} \rSec3[mem.res.monotonic.buffer.ctor]{\tcode{monotonic_buffer_resource} constructor and destructor} \indexlibrary{\idxcode{monotonic_buffer_resource}!constructor}% \begin{itemdecl} explicit monotonic_buffer_resource(memory_resource* upstream); monotonic_buffer_resource(size_t initial_size, memory_resource* upstream); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{upstream} shall be the address of a valid memory resource. \tcode{initial_size}, if specified, shall be greater than zero. \pnum \effects Sets \tcode{upstream_rsrc} to \tcode{upstream} and \tcode{current_buffer} to \tcode{nullptr}. If \tcode{initial_size} is specified, sets \tcode{next_buffer_size} to at least \tcode{initial_size}; otherwise sets \tcode{next_buffer_size} to an \impldef{default \tcode{next_buffer_size} for a \tcode{monotonic_buffer_resource}} size. \end{itemdescr} \indexlibrary{\idxcode{monotonic_buffer_resource}!constructor}% \begin{itemdecl} monotonic_buffer_resource(void* buffer, size_t buffer_size, memory_resource* upstream); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{upstream} shall be the address of a valid memory resource. \tcode{buffer_size} shall be no larger than the number of bytes in \tcode{buffer}. \pnum \effects Sets \tcode{upstream_rsrc} to \tcode{upstream}, \tcode{current_buffer} to \tcode{buffer}, and \tcode{next_buffer_size} to \tcode{buffer_size} (but not less than 1), then increases \tcode{next_buffer_size} by an \impldef{growth factor for \tcode{monotonic_buffer_resource}} growth factor (which need not be integral). \end{itemdescr} \indexlibrary{\idxcode{monotonic_buffer_resource}!destructor}% \begin{itemdecl} ~monotonic_buffer_resource(); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{release()}. \end{itemdescr} \rSec3[mem.res.monotonic.buffer.mem]{\tcode{monotonic_buffer_resource} members} \indexlibrarymember{release}{monotonic_buffer_resource}% \begin{itemdecl} void release(); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{upstream_rsrc->deallocate()} as necessary to release all allocated memory. \pnum \begin{note} The memory is released back to \tcode{upstream_rsrc} even if some blocks that were allocated from \tcode{this} have not been deallocated from \tcode{this}.\end{note} \end{itemdescr} \indexlibrarymember{upstream_resource}{monotonic_buffer_resource}% \begin{itemdecl} memory_resource* upstream_resource() const; \end{itemdecl} \begin{itemdescr} \pnum \returns The value of \tcode{upstream_rsrc}. \end{itemdescr} \indexlibrarymember{do_allocate}{monotonic_buffer_resource}% \begin{itemdecl} void* do_allocate(size_t bytes, size_t alignment) override; \end{itemdecl} \begin{itemdescr} \pnum \returns A pointer to allocated storage (\ref{basic.stc.dynamic.deallocation}) with a size of at least \tcode{bytes}. The size and alignment of the allocated memory shall meet the requirements for a class derived from \tcode{memory_resource}~(\ref{mem.res}). \pnum \effects If the unused space in \tcode{current_buffer} can fit a block with the specified \tcode{bytes} and \tcode{alignment}, then allocate the return block from \tcode{current_buffer}; otherwise set \tcode{current_buffer} to \tcode{upstream_rsrc->allocate(n, m)}, where \tcode{n} is not less than \tcode{max(bytes, next_buffer_size)} and \tcode{m} is not less than \tcode{alignment}, and increase \tcode{next_buffer_size} by an \impldef{growth factor for \tcode{monotonic_buffer_resource}} growth factor (which need not be integral), then allocate the return block from the newly-allocated \tcode{current_buffer}. \pnum \throws Nothing unless \tcode{upstream_rsrc->allocate()} throws. \end{itemdescr} \indexlibrarymember{do_deallocate}{monotonic_buffer_resource}% \begin{itemdecl} void do_deallocate(void* p, size_t bytes, size_t alignment) override; \end{itemdecl} \begin{itemdescr} \pnum \effects None. \pnum \throws Nothing. \pnum \remarks Memory used by this resource increases monotonically until its destruction. \end{itemdescr} \indexlibrarymember{do_is_equal}{monotonic_buffer_resource}% \begin{itemdecl} bool do_is_equal(const memory_resource& other) const noexcept override; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{this == dynamic_cast(\&other)}. \end{itemdescr} \rSec1[allocator.adaptor]{Class template \tcode{scoped_allocator_adaptor}} \rSec2[allocator.adaptor.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{scoped_allocator}}% \indexlibrary{\idxhdr{scoped_allocator}}% \begin{codeblock} // scoped allocator adaptor template class scoped_allocator_adaptor; template bool operator==(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; template bool operator!=(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; \end{codeblock} \pnum The class template \tcode{scoped_allocator_adaptor} is an allocator template that specifies the memory resource (the outer allocator) to be used by a container (as any other allocator does) and also specifies an inner allocator resource to be passed to the constructor of every element within the container. This adaptor is instantiated with one outer and zero or more inner allocator types. If instantiated with only one allocator type, the inner allocator becomes the \tcode{scoped_allocator_adaptor} itself, thus using the same allocator resource for the container and every element within the container and, if the elements themselves are containers, each of their elements recursively. If instantiated with more than one allocator, the first allocator is the outer allocator for use by the container, the second allocator is passed to the constructors of the container's elements, and, if the elements themselves are containers, the third allocator is passed to the elements' elements, and so on. If containers are nested to a depth greater than the number of allocators, the last allocator is used repeatedly, as in the single-allocator case, for any remaining recursions. \begin{note} The \tcode{scoped_allocator_adaptor} is derived from the outer allocator type so it can be substituted for the outer allocator type in most expressions. \end{note} \indexlibrary{\idxcode{scoped_allocator_adaptor}}% \begin{codeblock} namespace std { template class scoped_allocator_adaptor : public OuterAlloc { private: using OuterTraits = allocator_traits; // \expos scoped_allocator_adaptor inner; // \expos public: using outer_allocator_type = OuterAlloc; using inner_allocator_type = @\seebelow@; using value_type = typename OuterTraits::value_type; using size_type = typename OuterTraits::size_type; using difference_type = typename OuterTraits::difference_type; using pointer = typename OuterTraits::pointer; using const_pointer = typename OuterTraits::const_pointer; using void_pointer = typename OuterTraits::void_pointer; using const_void_pointer = typename OuterTraits::const_void_pointer; using propagate_on_container_copy_assignment = @\seebelow@; using propagate_on_container_move_assignment = @\seebelow@; using propagate_on_container_swap = @\seebelow@; using is_always_equal = @\seebelow@; template struct rebind { using other = scoped_allocator_adaptor< OuterTraits::template rebind_alloc, InnerAllocs...>; }; scoped_allocator_adaptor(); template scoped_allocator_adaptor(OuterA2&& outerAlloc, const InnerAllocs&... innerAllocs) noexcept; scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept; scoped_allocator_adaptor(scoped_allocator_adaptor&& other) noexcept; template scoped_allocator_adaptor( const scoped_allocator_adaptor& other) noexcept; template scoped_allocator_adaptor( scoped_allocator_adaptor&& other) noexcept; scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default; scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default; ~scoped_allocator_adaptor(); inner_allocator_type& inner_allocator() noexcept; const inner_allocator_type& inner_allocator() const noexcept; outer_allocator_type& outer_allocator() noexcept; const outer_allocator_type& outer_allocator() const noexcept; pointer allocate(size_type n); pointer allocate(size_type n, const_void_pointer hint); void deallocate(pointer p, size_type n); size_type max_size() const; template void construct(T* p, Args&&... args); template void construct(pair* p, piecewise_construct_t, tuple x, tuple y); template void construct(pair* p); template void construct(pair* p, U&& x, V&& y); template void construct(pair* p, const pair& x); template void construct(pair* p, pair&& x); template void destroy(T* p); scoped_allocator_adaptor select_on_container_copy_construction() const; }; template scoped_allocator_adaptor(OuterAlloc, InnerAllocs...) -> scoped_allocator_adaptor; template bool operator==(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; template bool operator!=(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; } \end{codeblock} \rSec2[allocator.adaptor.types]{Scoped allocator adaptor member types} \indexlibrarymember{inner_allocator_type}{scoped_allocator_adaptor}% \begin{itemdecl} using inner_allocator_type = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{scoped_allocator_adaptor} if \tcode{sizeof...(InnerAllocs)} is zero; otherwise,\\ \tcode{scoped_allocator_adaptor}. \end{itemdescr} \indexlibrarymember{propagate_on_container_copy_assignment}{scoped_allocator_adaptor}% \begin{itemdecl} using propagate_on_container_copy_assignment = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{true_type} if \tcode{allocator_traits::propagate_on_container_copy_assignment::value} is \tcode{true} for any \tcode{A} in the set of \tcode{OuterAlloc} and \tcode{InnerAllocs...}; otherwise, \tcode{false_type}. \end{itemdescr} \indexlibrarymember{propagate_on_container_move_assignment}{scoped_allocator_adaptor}% \begin{itemdecl} using propagate_on_container_move_assignment = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{true_type} if \tcode{allocator_traits::propagate_on_container_move_assignment::value} is \tcode{true} for any \tcode{A} in the set of \tcode{OuterAlloc} and \tcode{InnerAllocs...}; otherwise, \tcode{false_type}. \end{itemdescr} \indexlibrarymember{propagate_on_container_swap}{scoped_allocator_adaptor}% \begin{itemdecl} using propagate_on_container_swap = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{true_type} if \tcode{allocator_traits::propagate_on_container_swap::value} is \tcode{true} for any \tcode{A} in the set of \tcode{OuterAlloc} and \tcode{InnerAllocs...}; otherwise, \tcode{false_type}. \end{itemdescr} \indexlibrarymember{is_always_equal}{scoped_allocator_adaptor}% \begin{itemdecl} using is_always_equal = @\seebelow@; \end{itemdecl} \begin{itemdescr} \pnum \ctype \tcode{true_type} if \tcode{allocator_traits::is_always_equal::value} is \tcode{true} for every \tcode{A} in the set of \tcode{OuterAlloc} and \tcode{InnerAllocs...}; otherwise, \tcode{false_type}. \end{itemdescr} \rSec2[allocator.adaptor.cnstr]{Scoped allocator adaptor constructors} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} scoped_allocator_adaptor(); \end{itemdecl} \begin{itemdescr} \pnum \effects Value-initializes the \tcode{OuterAlloc} base class and the \tcode{inner} allocator object. \end{itemdescr} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} template scoped_allocator_adaptor(OuterA2&& outerAlloc, const InnerAllocs&... innerAllocs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes the \tcode{OuterAlloc} base class with \tcode{std::forward(outerAlloc)} and \tcode{inner} with \tcode{innerAllocs...} (hence recursively initializing each allocator within the adaptor with the corresponding allocator from the argument list). \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes each allocator within the adaptor with the corresponding allocator from \tcode{other}. \end{itemdescr} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} scoped_allocator_adaptor(scoped_allocator_adaptor&& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Move constructs each allocator within the adaptor with the corresponding allocator from \tcode{other}. \end{itemdescr} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} template scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes each allocator within the adaptor with the corresponding allocator from \tcode{other}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \indexlibrary{\idxcode{scoped_allocator_adaptor}!constructor}% \begin{itemdecl} template scoped_allocator_adaptor(scoped_allocator_adaptor&& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Initializes each allocator within the adaptor with the corresponding allocator rvalue from \tcode{other}. \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{is_constructible_v} is \tcode{true}. \end{itemdescr} \rSec2[allocator.adaptor.members]{Scoped allocator adaptor members} \pnum In the \tcode{construct} member functions, \tcode{\placeholdernc{OUTERMOST}(x)} is \tcode{x} if \tcode{x} does not have an \tcode{outer_allocator()} member function and \tcode{\placeholdernc{OUTERMOST}(x.outer_allocator())} otherwise; \tcode{\placeholdernc{OUTERMOST_ALLOC_TRAITS}(x)} is \tcode{allocator_traits}. \begin{note} \tcode{\placeholdernc{OUTERMOST}(x)} and \tcode{\placeholdernc{OUTERMOST_ALLOC_TRAITS}(x)} are recursive operations. It is incumbent upon the definition of \tcode{outer_allocator()} to ensure that the recursion terminates. It will terminate for all instantiations of \tcode{scoped_allocator_adaptor}. \end{note} \indexlibrarymember{inner_allocator}{scoped_allocator_adaptor}% \begin{itemdecl} inner_allocator_type& inner_allocator() noexcept; const inner_allocator_type& inner_allocator() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{*this} if \tcode{sizeof...(InnerAllocs)} is zero; otherwise, \tcode{inner}. \end{itemdescr} \indexlibrarymember{outer_allocator}{scoped_allocator_adaptor}% \begin{itemdecl} outer_allocator_type& outer_allocator() noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{static_cast(*this)}. \end{itemdescr} \indexlibrarymember{outer_allocator}{scoped_allocator_adaptor}% \begin{itemdecl} const outer_allocator_type& outer_allocator() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{static_cast(*this)}. \end{itemdescr} \indexlibrarymember{allocate}{scoped_allocator_adaptor}% \begin{itemdecl} pointer allocate(size_type n); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{allocator_traits::allocate(outer_allocator(), n)}. \end{itemdescr} \indexlibrarymember{allocate}{scoped_allocator_adaptor}% \begin{itemdecl} pointer allocate(size_type n, const_void_pointer hint); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{allocator_traits::allocate(outer_allocator(), n, hint)}. \end{itemdescr} \indexlibrarymember{deallocate}{scoped_allocator_adaptor}% \begin{itemdecl} void deallocate(pointer p, size_type n) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{allocator_traits::deallocate(outer_allocator(), p, n);} \end{itemdescr} \indexlibrarymember{max_size}{scoped_allocator_adaptor}% \begin{itemdecl} size_type max_size() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{allocator_traits::max_size(outer_allocator())}. \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(T* p, Args&&... args); \end{itemdecl} \begin{itemdescr} \pnum \effects \begin{itemize} \item If \tcode{uses_allocator_v} is \tcode{false} and \tcode{is_constructible_v} is \tcode{true}, calls: \begin{codeblock} @\placeholdernc{OUTERMOST_ALLOC_TRAITS}@(*this)::construct( @\placeholdernc{OUTERMOST}@(*this), p, std::forward(args)...) \end{codeblock} \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, calls: \begin{codeblock} @\placeholdernc{OUTERMOST_ALLOC_TRAITS}@(*this)::construct( @\placeholdernc{OUTERMOST}@(*this), p, allocator_arg, inner_allocator(), std::forward(args)...) \end{codeblock} \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, calls: \begin{codeblock} @\placeholdernc{OUTERMOST_ALLOC_TRAITS}@(*this)::construct( @\placeholdernc{OUTERMOST}@(*this), p, std::forward(args)..., inner_allocator()) \end{codeblock} \item Otherwise, the program is ill-formed. \begin{note} An error will result if \tcode{uses_allocator} evaluates to \tcode{true} but the specific constructor does not take an allocator. This definition prevents a silent failure to pass an inner allocator to a contained element. \end{note} \end{itemize} \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(pair* p, piecewise_construct_t, tuple x, tuple y); \end{itemdecl} \begin{itemdescr} \pnum \requires all of the types in \tcode{Args1} and \tcode{Args2} shall be \tcode{CopyConstructible} (Table~\ref{tab:copyconstructible}). \pnum \effects Constructs a \tcode{tuple} object \tcode{xprime} from \tcode{x} by the following rules: \begin{itemize} \item If \tcode{uses_allocator_v} is \tcode{false} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{xprime} is \tcode{x}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_construct\-ible_v} is \tcode{true}, then \tcode{xprime} is: \begin{codeblock} tuple_cat( tuple(allocator_arg, inner_allocator()), std::move(x)) \end{codeblock} \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_construct\-ible_v} is \tcode{true}, then \tcode{xprime} is: \begin{codeblock} tuple_cat(std::move(x), tuple(inner_allocator())) \end{codeblock} \item Otherwise, the program is ill-formed. \end{itemize} and constructs a \tcode{tuple} object \tcode{yprime} from \tcode{y} by the following rules: \begin{itemize} \item If \tcode{uses_allocator_v} is \tcode{false} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{yprime} is \tcode{y}. \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{yprime} is: \begin{codeblock} tuple_cat( tuple(allocator_arg, inner_allocator()), std::move(y)) \end{codeblock} \item Otherwise, if \tcode{uses_allocator_v} is \tcode{true} and \tcode{is_constructible_v} is \tcode{true}, then \tcode{yprime} is: \begin{codeblock} tuple_cat(std::move(y), tuple(inner_allocator())) \end{codeblock} \item Otherwise, the program is ill-formed. \end{itemize} then calls: \begin{codeblock} @\placeholdernc{OUTERMOST_ALLOC_TRAITS}@(*this)::construct( @\placeholdernc{OUTERMOST}@(*this), p, piecewise_construct, std::move(xprime), std::move(yprime)) \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(pair* p); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, tuple<>(), tuple<>()); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(pair* p, U&& x, V&& y); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(std::forward(x)), forward_as_tuple(std::forward(y))); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(pair* p, const pair& x); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(x.first), forward_as_tuple(x.second)); \end{codeblock} \end{itemdescr} \indexlibrarymember{construct}{scoped_allocator_adaptor}% \begin{itemdecl} template void construct(pair* p, pair&& x); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} construct(p, piecewise_construct, forward_as_tuple(std::forward(x.first)), forward_as_tuple(std::forward(x.second))); \end{codeblock} \end{itemdescr} \indexlibrarymember{destroy}{scoped_allocator_adaptor}% \begin{itemdecl} template void destroy(T* p); \end{itemdecl} \begin{itemdescr} \pnum \effects Calls \tcode{\placeholdernc{OUTERMOST_ALLOC_TRAITS}(*this)::destroy(\placeholdernc{OUTERMOST}(*this), p)}. \end{itemdescr} \indexlibrarymember{select_on_container_copy_construction}{scoped_allocator_adaptor}% \begin{itemdecl} scoped_allocator_adaptor select_on_container_copy_construction() const; \end{itemdecl} \begin{itemdescr} \pnum \returns A new \tcode{scoped_allocator_adaptor} object where each allocator \tcode{A} in the adaptor is initialized from the result of calling \tcode{allocator_traits::select_on_container_copy_construction()} on the corresponding allocator in \tcode{*this}. \end{itemdescr} \rSec2[scoped.adaptor.operators]{Scoped allocator operators} \indexlibrarymember{operator==}{scoped_allocator_adaptor}% \begin{itemdecl} template bool operator==(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns If \tcode{sizeof...(InnerAllocs)} is zero, \begin{codeblock} a.outer_allocator() == b.outer_allocator() \end{codeblock} otherwise \begin{codeblock} a.outer_allocator() == b.outer_allocator() && a.inner_allocator() == b.inner_allocator() \end{codeblock} \end{itemdescr} \indexlibrarymember{operator"!=}{scoped_allocator_adaptor}% \begin{itemdecl} template bool operator!=(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(a == b)}. \end{itemdescr} \rSec1[function.objects]{Function objects} \pnum A \indexdefn{function object!type}\term{function object type} is an object type~(\ref{basic.types}) that can be the type of the \grammarterm{postfix-expression} in a function call (\ref{expr.call},~\ref{over.match.call}).\footnote{Such a type is a function pointer or a class type which has a member \tcode{operator()} or a class type which has a conversion to a pointer to function.} A \defn{function object} is an object of a function object type. In the places where one would expect to pass a pointer to a function to an algorithmic template (Clause~\ref{algorithms}), the interface is specified to accept a function object. This not only makes algorithmic templates work with pointers to functions, but also enables them to work with arbitrary function objects. \rSec2[functional.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{functional}}% \indexlibrary{\idxhdr{functional}}% \begin{codeblock} namespace std { // \ref{func.invoke}, invoke template invoke_result_t invoke(F&& f, Args&&... args) noexcept(is_nothrow_invocable_v); // \ref{refwrap}, \tcode{reference_wrapper} template class reference_wrapper; template reference_wrapper ref(T&) noexcept; template reference_wrapper cref(const T&) noexcept; template void ref(const T&&) = delete; template void cref(const T&&) = delete; template reference_wrapper ref(reference_wrapper) noexcept; template reference_wrapper cref(reference_wrapper) noexcept; // \ref{arithmetic.operations}, arithmetic operations template struct plus; template struct minus; template struct multiplies; template struct divides; template struct modulus; template struct negate; template <> struct plus; template <> struct minus; template <> struct multiplies; template <> struct divides; template <> struct modulus; template <> struct negate; // \ref{comparisons}, comparisons template struct equal_to; template struct not_equal_to; template struct greater; template struct less; template struct greater_equal; template struct less_equal; template <> struct equal_to; template <> struct not_equal_to; template <> struct greater; template <> struct less; template <> struct greater_equal; template <> struct less_equal; // \ref{logical.operations}, logical operations template struct logical_and; template struct logical_or; template struct logical_not; template <> struct logical_and; template <> struct logical_or; template <> struct logical_not; // \ref{bitwise.operations}, bitwise operations template struct bit_and; template struct bit_or; template struct bit_xor; template struct bit_not; template <> struct bit_and; template <> struct bit_or; template <> struct bit_xor; template <> struct bit_not; // \ref{func.not_fn}, function template \tcode{not_fn} template @\unspec@ not_fn(F&& f); // \ref{func.bind}, bind template struct is_bind_expression; template struct is_placeholder; template @\unspec@ bind(F&&, BoundArgs&&...); template @\unspec@ bind(F&&, BoundArgs&&...); namespace placeholders { // \tcode{\placeholder{M}} is the \impldef{number of placeholders for bind expressions} number of placeholders @\seebelownc@ _1; @\seebelownc@ _2; . . . @\seebelownc@ _@\placeholdernc{M}@; } // \ref{func.memfn}, member function adaptors template @\unspec@ mem_fn(R T::*) noexcept; // \ref{func.wrap}, polymorphic function wrappers class bad_function_call; template class function; // not defined template class function; template void swap(function&, function&) noexcept; template bool operator==(const function&, nullptr_t) noexcept; template bool operator==(nullptr_t, const function&) noexcept; template bool operator!=(const function&, nullptr_t) noexcept; template bool operator!=(nullptr_t, const function&) noexcept; // \ref{func.search}, searchers template> class default_searcher; template::value_type>, class BinaryPredicate = equal_to<>> class boyer_moore_searcher; template::value_type>, class BinaryPredicate = equal_to<>> class boyer_moore_horspool_searcher; // \ref{unord.hash}, hash function primary template template struct hash; // \ref{func.bind}, function object binders template inline constexpr bool is_bind_expression_v = is_bind_expression::value; template inline constexpr int is_placeholder_v = is_placeholder::value; } \end{codeblock} \pnum \begin{example} If a \Cpp program wants to have a by-element addition of two vectors \tcode{a} and \tcode{b} containing \tcode{double} and put the result into \tcode{a}, it can do: \begin{codeblock} transform(a.begin(), a.end(), b.begin(), a.begin(), plus()); \end{codeblock} \end{example} \pnum \begin{example} To negate every element of \tcode{a}: \begin{codeblock} transform(a.begin(), a.end(), a.begin(), negate()); \end{codeblock} \end{example} \rSec2[func.def]{Definitions} \pnum The following definitions apply to this Clause: \pnum \indexdefn{call signature}% A \term{call signature} is the name of a return type followed by a parenthesized comma-separated list of zero or more argument types. \pnum \indexdefn{callable type}% A \term{callable type} is a function object type~(\ref{function.objects}) or a pointer to member. \pnum \indexdefn{callable object}% A \term{callable object} is an object of a callable type. \pnum \indexdefn{call wrapper!type}% A \term{call wrapper type} is a type that holds a callable object and supports a call operation that forwards to that object. \pnum \indexdefn{call wrapper}% A \term{call wrapper} is an object of a call wrapper type. \pnum \indexdefn{target object}% A \term{target object} is the callable object held by a call wrapper. \rSec2[func.require]{Requirements} \pnum \indexlibrary{invoke@\tcode{\placeholder{INVOKE}}}% Define \tcode{\placeholdernc{INVOKE}(f, t$_1$, t$_2$, $\dotsc$, t$_N$)} as follows: \begin{itemize} \item \tcode{(t$_1$.*f)(t$_2$, $\dotsc$, t$_N$)} when \tcode{f} is a pointer to a member function of a class \tcode{T} and \tcode{is_base_of_v>} is \tcode{true}; \item \tcode{(t$_1$.get().*f)(t$_2$, $\dotsc$, t$_N$)} when \tcode{f} is a pointer to a member function of a class \tcode{T} and \tcode{decay_t} is a specialization of \tcode{reference_wrapper}; \item \tcode{((*t$_1$).*f)(t$_2$, $\dotsc$, t$_N$)} when \tcode{f} is a pointer to a member function of a class \tcode{T} and \tcode{t$_1$} does not satisfy the previous two items; \item \tcode{t$_1$.*f} when \tcode{N == 1} and \tcode{f} is a pointer to data member of a class \tcode{T} and \tcode{is_base_of_v>} is \tcode{true}; \item \tcode{t$_1$.get().*f} when \tcode{N == 1} and \tcode{f} is a pointer to data member of a class \tcode{T} and \tcode{decay_t} is a specialization of \tcode{reference_wrapper}; \item \tcode{(*t$_1$).*f} when \tcode{N == 1} and \tcode{f} is a pointer to data member of a class \tcode{T} and \tcode{t$_1$} does not satisfy the previous two items; \item \tcode{f(t$_1$, t$_2$, $\dotsc$, t$_N$)} in all other cases. \end{itemize} \pnum \indexlibrary{invoke@\tcode{\placeholder{INVOKE}}}% Define \tcode{\placeholdernc{INVOKE}(f, t$_1$, t$_2$, $\dotsc$, t$_N$)} as \tcode{static_cast(\placeholdernc{INVOKE}(f, t$_1$, t$_2$, $\dotsc$, t$_N$))} if \tcode{R} is \cv{}~\tcode{void}, otherwise \tcode{\placeholdernc{INVOKE}(f, t$_1$, t$_2$, $\dotsc$, t$_N$)} implicitly converted to \tcode{R}. \pnum \indextext{call wrapper}% \indextext{call wrapper!simple}% \indextext{call wrapper!forwarding}% \indextext{simple call wrapper}% \indextext{forwarding call wrapper}% Every call wrapper~(\ref{func.def}) shall be \tcode{MoveConstructible}. A \term{forwarding call wrapper} is a call wrapper that can be called with an arbitrary argument list and delivers the arguments to the wrapped callable object as references. This forwarding step shall ensure that rvalue arguments are delivered as rvalue references and lvalue arguments are delivered as lvalue references. A \term{simple call wrapper} is a forwarding call wrapper that is \tcode{CopyConstructible} and \tcode{CopyAssignable} and whose copy constructor, move constructor, and assignment operator do not throw exceptions. \begin{note} In a typical implementation forwarding call wrappers have an overloaded function call operator of the form \begin{codeblock} template R operator()(UnBoundArgs&&... unbound_args) @\textit{cv-qual}@; \end{codeblock} \end{note} \rSec2[func.invoke]{Function template \tcode{invoke}} \indexlibrary{\idxcode{invoke}}% \indexlibrary{invoke@\tcode{\placeholder{INVOKE}}}% \begin{itemdecl} template invoke_result_t invoke(F&& f, Args&&... args) noexcept(is_nothrow_invocable_v); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholdernc{INVOKE}(std::forward(f), std::forward(args)...)}~(\ref{func.require}). \end{itemdescr} \rSec2[refwrap]{Class template \tcode{reference_wrapper}} \indexlibrary{\idxcode{reference_wrapper}}% \indextext{function object!\idxcode{reference_wrapper}}% \begin{codeblock} namespace std { template class reference_wrapper { public: // types using type = T; // construct/copy/destroy reference_wrapper(T&) noexcept; reference_wrapper(T&&) = delete; // do not bind to temporary objects reference_wrapper(const reference_wrapper& x) noexcept; // assignment reference_wrapper& operator=(const reference_wrapper& x) noexcept; // access operator T& () const noexcept; T& get() const noexcept; // invocation template invoke_result_t operator()(ArgTypes&&...) const; }; template reference_wrapper(reference_wrapper) -> reference_wrapper; } \end{codeblock} \pnum \tcode{reference_wrapper} is a \tcode{CopyConstructible} and \tcode{CopyAssignable} wrapper around a reference to an object or function of type \tcode{T}. \pnum \tcode{reference_wrapper} shall be a trivially copyable type~(\ref{basic.types}). \rSec3[refwrap.const]{\tcode{reference_wrapper} construct/copy/destroy} \indexlibrary{\idxcode{reference_wrapper}!constructor}% \begin{itemdecl} reference_wrapper(T& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a \tcode{reference_wrapper} object that stores a reference to \tcode{t}. \end{itemdescr} \indexlibrary{\idxcode{reference_wrapper}!constructor}% \begin{itemdecl} reference_wrapper(const reference_wrapper& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Constructs a \tcode{reference_wrapper} object that stores a reference to \tcode{x.get()}. \end{itemdescr} \rSec3[refwrap.assign]{\tcode{reference_wrapper} assignment} \indexlibrarymember{operator=}{reference_wrapper}% \begin{itemdecl} reference_wrapper& operator=(const reference_wrapper& x) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\postconditions \tcode{*this} stores a reference to \tcode{x.get()}. \end{itemdescr} \rSec3[refwrap.access]{\tcode{reference_wrapper} access} \indexlibrarymember{operator T\&}{reference_wrapper}% \begin{itemdecl} operator T& () const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns The stored reference. \end{itemdescr} \indexlibrarymember{get}{reference_wrapper}% \begin{itemdecl} T& get() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns The stored reference. \end{itemdescr} \rSec3[refwrap.invoke]{\tcode{reference_wrapper} invocation} \indexlibrarymember{operator()}{reference_wrapper}% \begin{itemdecl} template invoke_result_t operator()(ArgTypes&&... args) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{\placeholdernc{INVOKE}(get(), std::forward(args)...)}.~(\ref{func.require}) \end{itemdescr} \rSec3[refwrap.helpers]{\tcode{reference_wrapper} helper functions} \indexlibrarymember{ref}{reference_wrapper}% \begin{itemdecl} template reference_wrapper ref(T& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{reference_wrapper(t)}. \end{itemdescr} \indexlibrarymember{ref}{reference_wrapper}% \begin{itemdecl} template reference_wrapper ref(reference_wrapper t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{ref(t.get())}. \end{itemdescr} \indexlibrarymember{cref}{reference_wrapper}% \begin{itemdecl} template reference_wrapper cref(const T& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{reference_wrapper (t)}. \end{itemdescr} \indexlibrarymember{cref}{reference_wrapper}% \begin{itemdecl} template reference_wrapper cref(reference_wrapper t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{cref(t.get())}. \end{itemdescr} \rSec2[arithmetic.operations]{Arithmetic operations} \pnum The library provides basic function object classes for all of the arithmetic operators in the language~(\ref{expr.mul}, \ref{expr.add}). \rSec3[arithmetic.operations.plus]{Class template \tcode{plus}} \indexlibrary{\idxcode{plus}}% \begin{itemdecl} template struct plus { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{plus}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x + y}. \end{itemdescr} \indexlibrary{\idxcode{plus<>}}% \begin{itemdecl} template <> struct plus { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) + std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{plus<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) + std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) + std::forward(u)}. \end{itemdescr} \rSec3[arithmetic.operations.minus]{Class template \tcode{minus}} \indexlibrary{\idxcode{minus}}% \begin{itemdecl} template struct minus { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{minus}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x - y}. \end{itemdescr} \indexlibrary{\idxcode{minus<>}}% \begin{itemdecl} template <> struct minus { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) - std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{minus<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) - std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) - std::forward(u)}. \end{itemdescr} \rSec3[arithmetic.operations.multiplies]{Class template \tcode{multiplies}} \indexlibrary{\idxcode{multiplies}}% \begin{itemdecl} template struct multiplies { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{multiplies}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x * y}. \end{itemdescr} \indexlibrary{\idxcode{multiplies<>}}% \begin{itemdecl} template <> struct multiplies { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) * std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{multiplies<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) * std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) * std::forward(u)}. \end{itemdescr} \rSec3[arithmetic.operations.divides]{Class template \tcode{divides}} \indexlibrary{\idxcode{divides}}% \begin{itemdecl} template struct divides { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{divides}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x / y}. \end{itemdescr} \indexlibrary{\idxcode{divides<>}}% \begin{itemdecl} template <> struct divides { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) / std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{divides<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) / std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) / std::forward(u)}. \end{itemdescr} \rSec3[arithmetic.operations.modulus]{Class template \tcode{modulus}} \indexlibrary{\idxcode{modulus}}% \begin{itemdecl} template struct modulus { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{modulus}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x \% y}. \end{itemdescr} \indexlibrary{\idxcode{modulus<>}}% \begin{itemdecl} template <> struct modulus { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) % std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{modulus<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) % std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) \% std::forward(u)}. \end{itemdescr} \rSec3[arithmetic.operations.negate]{Class template \tcode{negate}} \indexlibrary{\idxcode{negate}}% \begin{itemdecl} template struct negate { constexpr T operator()(const T& x) const; }; \end{itemdecl} \indexlibrarymember{operator()}{negate}% \begin{itemdecl} constexpr T operator()(const T& x) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{-x}. \end{itemdescr} \indexlibrary{\idxcode{negate<>}}% \begin{itemdecl} template <> struct negate { template constexpr auto operator()(T&& t) const -> decltype(-std::forward(t)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{negate<>}% \begin{itemdecl} template constexpr auto operator()(T&& t) const -> decltype(-std::forward(t)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{-std::forward(t)}. \end{itemdescr} \rSec2[comparisons]{Comparisons} \pnum The library provides basic function object classes for all of the comparison operators in the language~(\ref{expr.rel}, \ref{expr.eq}). \pnum For templates \tcode{less}, \tcode{greater}, \tcode{less_equal}, and \tcode{greater_equal}, the specializations for any pointer type yield a strict total order that is consistent among those specializations and is also consistent with the partial order imposed by the built-in operators \tcode{<}, \tcode{>}, \tcode{<=}, \tcode{>=}. \begin{note} When \tcode{a < b} is well-defined for pointers \tcode{a} and \tcode{b} of type \tcode{P}, this implies \tcode{(a < b) == less

(a, b)}, \tcode{(a > b) == greater

(a, b)}, and so forth. \end{note} For template specializations \tcode{less}, \tcode{greater}, \tcode{less_equal}, and \tcode{greater_equal}, if the call operator calls a built-in operator comparing pointers, the call operator yields a strict total order that is consistent among those specializations and is also consistent with the partial order imposed by those built-in operators. \rSec3[comparisons.equal_to]{Class template \tcode{equal_to}} \indexlibrary{\idxcode{equal_to}}% \begin{itemdecl} template struct equal_to { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{equal_to}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x == y}. \end{itemdescr} \indexlibrary{\idxcode{equal_to<>}}% \begin{itemdecl} template <> struct equal_to { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) == std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{equal_to<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) == std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) == std::forward(u)}. \end{itemdescr} \rSec3[comparisons.not_equal_to]{Class template \tcode{not_equal_to}} \indexlibrary{\idxcode{not_equal_to}}% \begin{itemdecl} template struct not_equal_to { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{not_equal_to}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x != y}. \end{itemdescr} \indexlibrary{\idxcode{not_equal_to<>}}% \begin{itemdecl} template <> struct not_equal_to { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) != std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{not_equal_to<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) != std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) != std::forward(u)}. \end{itemdescr} \rSec3[comparisons.greater]{Class template \tcode{greater}} \indexlibrary{\idxcode{greater}}% \begin{itemdecl} template struct greater { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{greater}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x > y}. \end{itemdescr} \indexlibrary{\idxcode{greater<>}}% \begin{itemdecl} template <> struct greater { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) > std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{greater<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) > std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) > std::forward(u)}. \end{itemdescr} \rSec3[comparisons.less]{Class template \tcode{less}} \indexlibrary{\idxcode{less}}% \begin{itemdecl} template struct less { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{less}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x < y}. \end{itemdescr} \indexlibrary{\idxcode{less<>}}% \begin{itemdecl} template <> struct less { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) < std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{less<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) < std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) < std::forward(u)}. \end{itemdescr} \rSec3[comparisons.greater_equal]{Class template \tcode{greater_equal}} \indexlibrary{\idxcode{greater_equal}}% \begin{itemdecl} template struct greater_equal { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{greater_equal}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x >= y}. \end{itemdescr} \indexlibrary{\idxcode{greater_equal<>}}% \begin{itemdecl} template <> struct greater_equal { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) >= std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{greater_equal<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) >= std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) >= std::forward(u)}. \end{itemdescr} \rSec3[comparisons.less_equal]{Class template \tcode{less_equal}} \indexlibrary{\idxcode{less_equal}}% \begin{itemdecl} template struct less_equal { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{less_equal}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x <= y}. \end{itemdescr} \indexlibrary{\idxcode{less_equal<>}}% \begin{itemdecl} template <> struct less_equal { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) <= std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{less_equal<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) <= std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) <= std::forward(u)}. \end{itemdescr} \rSec2[logical.operations]{Logical operations} \pnum The library provides basic function object classes for all of the logical operators in the language~(\ref{expr.log.and}, \ref{expr.log.or}, \ref{expr.unary.op}). \rSec3[logical.operations.and]{Class template \tcode{logical_and}} \indexlibrary{\idxcode{logical_and}}% \begin{itemdecl} template struct logical_and { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_and}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x \&\& y}. \end{itemdescr} \indexlibrary{\idxcode{logical_and<>}}% \begin{itemdecl} template <> struct logical_and { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) && std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_and<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) && std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) \&\& std::forward(u)}. \end{itemdescr} \rSec3[logical.operations.or]{Class template \tcode{logical_or}} \indexlibrary{\idxcode{logical_or}}% \begin{itemdecl} template struct logical_or { constexpr bool operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_or}% \begin{itemdecl} constexpr bool operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x || y}. \end{itemdescr} \indexlibrary{\idxcode{logical_or<>}}% \begin{itemdecl} template <> struct logical_or { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) || std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_or<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) || std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) || std::forward(u)}. \end{itemdescr} \rSec3[logical.operations.not]{Class template \tcode{logical_not}} \indexlibrary{\idxcode{logical_not}}% \begin{itemdecl} template struct logical_not { constexpr bool operator()(const T& x) const; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_not}% \begin{itemdecl} constexpr bool operator()(const T& x) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!x}. \end{itemdescr} \indexlibrary{\idxcode{logical_not<>}}% \begin{itemdecl} template <> struct logical_not { template constexpr auto operator()(T&& t) const -> decltype(!std::forward(t)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{logical_not<>}% \begin{itemdecl} template constexpr auto operator()(T&& t) const -> decltype(!std::forward(t)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!std::forward(t)}. \end{itemdescr} \rSec2[bitwise.operations]{Bitwise operations} \pnum The library provides basic function object classes for all of the bitwise operators in the language~(\ref{expr.bit.and}, \ref{expr.or}, \ref{expr.xor}, \ref{expr.unary.op}). \rSec3[bitwise.operations.and]{Class template \tcode{bit_and}} \indexlibrary{\idxcode{bit_and}}% \begin{itemdecl} template struct bit_and { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_and}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x \& y}. \end{itemdescr} \indexlibrary{\idxcode{bit_and<>}}% \begin{itemdecl} template <> struct bit_and { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) & std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_and<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) & std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) \& std::forward(u)}. \end{itemdescr} \rSec3[bitwise.operations.or]{Class template \tcode{bit_or}} \indexlibrary{\idxcode{bit_or}}% \begin{itemdecl} template struct bit_or { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_or}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x | y}. \end{itemdescr} \indexlibrary{\idxcode{bit_or<>}}% \begin{itemdecl} template <> struct bit_or { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) | std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_or<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) | std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) | std::forward(u)}. \end{itemdescr} \rSec3[bitwise.operations.xor]{Class template \tcode{bit_xor}} \indexlibrary{\idxcode{bit_xor}}% \begin{itemdecl} template struct bit_xor { constexpr T operator()(const T& x, const T& y) const; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_xor}% \begin{itemdecl} constexpr T operator()(const T& x, const T& y) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{x \caret{} y}. \end{itemdescr} \indexlibrary{\idxcode{bit_xor<>}}% \begin{itemdecl} template <> struct bit_xor { template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) ^ std::forward(u)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_xor<>}% \begin{itemdecl} template constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward(t) ^ std::forward(u)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{std::forward(t) \caret{} std::forward(u)}. \end{itemdescr} \rSec3[bitwise.operations.not]{Class template \tcode{bit_not}} \begin{itemdecl} template struct bit_not { constexpr T operator()(const T& x) const; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_not}% \begin{itemdecl} constexpr T operator()(const T& x) const; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{\~{}x}. \end{itemdescr} \indexlibrary{\idxcode{bit_not<>}}% \begin{itemdecl} template <> struct bit_not { template constexpr auto operator()(T&& t) const -> decltype(~std::forward(t)); using is_transparent = @\unspec@; }; \end{itemdecl} \indexlibrarymember{operator()}{bit_not<>}% \begin{itemdecl} template constexpr auto operator()(T&&) const -> decltype(~std::forward(t)); \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{\~{}std::forward(t)}. \end{itemdescr} \rSec2[func.not_fn]{Function template \tcode{not_fn}} \indexlibrary{\idxcode{not_fn}}% \begin{itemdecl} template @\unspec@ not_fn(F&& f); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to \tcode{return \placeholder{call_wrapper}(std::forward(f));} where \tcode{\placeholder{call_wrapper}} is an exposition only class defined as follows: \begin{codeblock} class @\placeholder{call_wrapper}@ { using FD = decay_t; FD fd; explicit @\placeholder{call_wrapper}@(F&& f); public: @\placeholder{call_wrapper}@(@\placeholder{call_wrapper}@&&) = default; @\placeholder{call_wrapper}@(const @\placeholder{call_wrapper}@&) = default; template auto operator()(Args&&...) & -> decltype(!declval>()); template auto operator()(Args&&...) const& -> decltype(!declval>()); template auto operator()(Args&&...) && -> decltype(!declval>()); template auto operator()(Args&&...) const&& -> decltype(!declval>()); }; \end{codeblock} \end{itemdescr} \begin{itemdecl} explicit @\placeholdernc{call_wrapper}@(F&& f); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{FD} shall satisfy the requirements of \tcode{MoveConstructible}. \tcode{is_constructible_v} shall be \tcode{true}. \tcode{fd} shall be a callable object~(\ref{func.def}). \pnum \effects Initializes \tcode{fd} from \tcode{std::forward(f)}. \pnum \throws Any exception thrown by construction of \tcode{fd}. \end{itemdescr} \begin{itemdecl} template auto operator()(Args&&... args) & -> decltype(!declval>()); template auto operator()(Args&&... args) const& -> decltype(!declval>()); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return !@\placeholdernc{INVOKE}@(fd, std::forward(args)...); // see \ref{func.require} \end{codeblock} \end{itemdescr} \begin{itemdecl} template auto operator()(Args&&... args) && -> decltype(!declval>()); template auto operator()(Args&&... args) const&& -> decltype(!declval>()); \end{itemdecl} \begin{itemdescr} \pnum \effects Equivalent to: \begin{codeblock} return !@\placeholdernc{INVOKE}@(std::move(fd), std::forward(args)...); // see \ref{func.require} \end{codeblock} \end{itemdescr} \rSec2[func.bind]{Function object binders}% \indextext{function object!binders|(} \pnum This subclause describes a uniform mechanism for binding arguments of callable objects. \rSec3[func.bind.isbind]{Class template \tcode{is_bind_expression}} \indexlibrary{\idxcode{is_bind_expression}}% \begin{codeblock} namespace std { template struct is_bind_expression; // see below } \end{codeblock} \pnum The class template \tcode{is_bind_expression} can be used to detect function objects generated by \tcode{bind}. The function template \tcode{bind} uses \tcode{is_bind_expression} to detect subexpressions. \pnum Instantiations of the \tcode{is_bind_expression} template shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}). The implementation shall provide a definition that has a base characteristic of \tcode{true_type} if \tcode{T} is a type returned from \tcode{bind}, otherwise it shall have a base characteristic of \tcode{false_type}. A program may specialize this template for a user-defined type \tcode{T} to have a base characteristic of \tcode{true_type} to indicate that \tcode{T} should be treated as a subexpression in a \tcode{bind} call. \rSec3[func.bind.isplace]{Class template \tcode{is_placeholder}} \indexlibrary{\idxcode{is_placeholder}}% \begin{codeblock} namespace std { template struct is_placeholder; // see below } \end{codeblock} \pnum The class template \tcode{is_placeholder} can be used to detect the standard placeholders \tcode{_1}, \tcode{_2}, and so on. The function template \tcode{bind} uses \tcode{is_placeholder} to detect placeholders. \pnum Instantiations of the \tcode{is_placeholder} template shall meet the \tcode{UnaryTypeTrait} requirements~(\ref{meta.rqmts}). The implementation shall provide a definition that has the base characteristic of \tcode{integral_constant} if \tcode{T} is the type of \tcode{std::placeholders::_\placeholder{J}}, otherwise it shall have a base characteristic of \tcode{integral_constant}. A program may specialize this template for a user-defined type \tcode{T} to have a base characteristic of \tcode{integral_constant} with \tcode{N > 0} to indicate that \tcode{T} should be treated as a placeholder type. \rSec3[func.bind.bind]{Function template \tcode{bind}} \indexlibrary{\idxcode{bind}|(} \pnum In the text that follows: \begin{itemize} \item \tcode{FD} is the type \tcode{decay_t}, \item \tcode{fd} is an lvalue of type \tcode{FD} constructed from \tcode{std::forward(f)}, \item $\tcode{T}_i$ is the $i^\text{th}$ type in the template parameter pack \tcode{BoundArgs}, \item $\tcode{TD}_i$ is the type \tcode{decay_t<$\tcode{T}_i$>}, \item $\tcode{t}_i$ is the $i^\text{th}$ argument in the function parameter pack \tcode{bound_args}, \item $\tcode{td}_i$ is an lvalue of type $\tcode{TD}_i$ constructed from \tcode{std::forward<$\tcode{T}_i$>($\tcode{t}_i$)}, \item $\tcode{U}_j$ is the $j^\text{th}$ deduced type of the \tcode{UnBoundArgs\&\&...} parameter of the forwarding call wrapper, and \item $\tcode{u}_j$ is the $j^\text{th}$ argument associated with $\tcode{U}_j$. \end{itemize} \indexlibrary{\idxcode{bind}}% \begin{itemdecl} template @\unspec@ bind(F&& f, BoundArgs&&... bound_args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_constructible_v} shall be \tcode{true}. For each $\tcode{T}_i$ in \tcode{BoundArgs}, \tcode{is_cons\-tructible_v<$\tcode{TD}_i$, $\tcode{T}_i$>} shall be \tcode{true}. \tcode{\placeholdernc{INVOKE}(fd, $\tcode{w}_1$, $\tcode{w}_2$, $\dotsc$, $\tcode{w}_N$)}~(\ref{func.require}) shall be a valid expression for some values $\tcode{w}_1$, $\tcode{w}_2$, $\dotsc{}$, $\tcode{w}_N$, where $N$ has the value \tcode{sizeof...(bound_args)}. The cv-qualifiers \cv{} of the call wrapper \tcode{g}, as specified below, shall be neither \tcode{volatile} nor \tcode{const volatile}. \pnum\returns A forwarding call wrapper \tcode{g}~(\ref{func.require}). The effect of \tcode{g($\tcode{u}_1$, $\tcode{u}_2$, $\dotsc$, $\tcode{u}_M$)} shall be \begin{codeblock} @\placeholdernc{INVOKE}@(fd, std::forward<@$\tcode{V}_1$@>(@$\tcode{v}_1$@), std::forward<@$\tcode{V}_2$@>(@$\tcode{v}_2$@), @$\dotsc$@, std::forward<@$\tcode{V}_N$@>(@$\tcode{v}_N$@)) \end{codeblock} where the values and types of the bound arguments $\tcode{v}_1$, $\tcode{v}_2$, $\dotsc$, $\tcode{v}_N$ are determined as specified below. The copy constructor and move constructor of the forwarding call wrapper shall throw an exception if and only if the corresponding constructor of \tcode{FD} or of any of the types $\tcode{TD}_i$ throws an exception. \pnum \throws Nothing unless the construction of \tcode{fd} or of one of the values $\tcode{td}_i$ throws an exception. \pnum \remarks The return type shall satisfy the requirements of \tcode{MoveConstructible}. If all of \tcode{FD} and $\tcode{TD}_i$ satisfy the requirements of \tcode{CopyConstructible}, then the return type shall satisfy the requirements of \tcode{CopyConstructible}. \begin{note} This implies that all of \tcode{FD} and $\tcode{TD}_i$ are \tcode{MoveConstructible}. \end{note} \end{itemdescr} \indexlibrary{\idxcode{bind}}% \begin{itemdecl} template @\unspec@ bind(F&& f, BoundArgs&&... bound_args); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{is_constructible_v} shall be \tcode{true}. For each $\tcode{T}_i$ in \tcode{BoundArgs}, \tcode{is_con\-structible_v<$\tcode{TD}_i$, $\tcode{T}_i$>} shall be \tcode{true}. \tcode{\placeholdernc{INVOKE}(fd, $\tcode{w}_1$, $\tcode{w}_2$, $\dotsc$, $\tcode{w}_N$)} shall be a valid expression for some values $\tcode{w}_1$, $\tcode{w}_2$, $\dotsc$, $\tcode{w}_N$, where $N$ has the value \tcode{sizeof...(bound_args)}. The cv-qualifiers \cv{} of the call wrapper \tcode{g}, as specified below, shall be neither \tcode{volatile} nor \tcode{const volatile}. \pnum \returns A forwarding call wrapper \tcode{g}~(\ref{func.require}). The effect of \tcode{g($\tcode{u}_1$, $\tcode{u}_2$, $\dotsc$, $\tcode{u}_M$)} shall be \begin{codeblock} @\placeholdernc{INVOKE}@(fd, std::forward<@$\tcode{V}_1$@>(@$\tcode{v}_1$@), std::forward<@$\tcode{V}_2$@>(@$\tcode{v}_2$@), @$\dotsc$@, std::forward<@$\tcode{V}_N$@>(@$\tcode{v}_N$@)) \end{codeblock} where the values and types of the bound arguments $\tcode{v}_1$, $\tcode{v}_2$, $\dotsc$, $\tcode{v}_N$ are determined as specified below. The copy constructor and move constructor of the forwarding call wrapper shall throw an exception if and only if the corresponding constructor of \tcode{FD} or of any of the types $\tcode{TD}_i$ throws an exception. \pnum \throws Nothing unless the construction of \tcode{fd} or of one of the values $\tcode{td}_i$ throws an exception. \pnum \remarks The return type shall satisfy the requirements of \tcode{MoveConstructible}. If all of \tcode{FD} and $\tcode{TD}_i$ satisfy the requirements of \tcode{CopyConstructible}, then the return type shall satisfy the requirements of \tcode{CopyConstructible}. \begin{note} This implies that all of \tcode{FD} and $\tcode{TD}_i$ are \tcode{MoveConstructible}. \end{note} \end{itemdescr} \pnum \indextext{bound arguments}% The values of the \techterm{bound arguments} $\tcode{v}_1$, $\tcode{v}_2$, $\dotsc$, $\tcode{v}_N$ and their corresponding types $\tcode{V}_1$, $\tcode{V}_2$, $\dotsc$, $\tcode{V}_N$ depend on the types $\tcode{TD}_i$ derived from the call to \tcode{bind} and the cv-qualifiers \cv{} of the call wrapper \tcode{g} as follows: \begin{itemize} \item if $\tcode{TD}_i$ is \tcode{reference_wrapper}, the argument is \tcode{$\tcode{td}_i$.get()} and its type $\tcode{V}_i$ is \tcode{T\&}; \item if the value of \tcode{is_bind_expression_v<$\tcode{TD}_i$>} is \tcode{true}, the argument is \tcode{$\tcode{td}_i$(std::forward<$\tcode{U}_j$>($\tcode{u}_j$)...)} and its type $\tcode{V}_i$ is \tcode{invoke_result_t<$\tcode{TD}_i$ \cv{} \&, $\tcode{U}_j$...>\&\&}; \item if the value \tcode{j} of \tcode{is_placeholder_v<$\tcode{TD}_i$>} is not zero, the argument is \tcode{std::forward<$\tcode{U}_j$>($\tcode{u}_j$)} and its type $\tcode{V}_i$ is \tcode{$\tcode{U}_j$\&\&}; \item otherwise, the value is $\tcode{td}_i$ and its type $\tcode{V}_i$ is \tcode{$\tcode{TD}_i$ \cv{} \&}. \end{itemize} \indexlibrary{\idxcode{bind}|)}% \rSec3[func.bind.place]{Placeholders} \indexlibrary{\idxcode{placeholders}}% \indexlibrary{1@\tcode{_1}}% \begin{codeblock} namespace std::placeholders { // M is the \impldef{number of placeholders for bind expressions} number of placeholders @\seebelow@ _1; @\seebelow@ _2; . . . @\seebelow@ _M; } \end{codeblock} \pnum All placeholder types shall be \tcode{DefaultConstructible} and \tcode{CopyConstructible}, and their default constructors and copy/move constructors shall not throw exceptions. It is \impldef{assignability of placeholder objects} whether placeholder types are \tcode{CopyAssignable}. \tcode{CopyAssignable} placeholders' copy assignment operators shall not throw exceptions. \pnum Placeholders should be defined as: \begin{codeblock} inline constexpr @\unspec@ _1{}; \end{codeblock} If they are not, they shall be declared as: \begin{codeblock} extern @\unspec@ _1; \end{codeblock}% \indextext{function object!binders|)} \rSec2[func.memfn]{Function template \tcode{mem_fn}}% \indextext{function object!\idxcode{mem_fn}|(} \indexlibrary{\idxcode{mem_fn}}% \begin{itemdecl} template @\unspec@ mem_fn(R T::* pm) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A simple call wrapper~(\ref{func.def}) \tcode{fn} such that the expression \tcode{fn(t, a$_2$, $\dotsc$, a$_N$)} is equivalent to \tcode{\placeholdernc{INVOKE}(pm, t, a$_2$, $\dotsc$, a$_N$)}~(\ref{func.require}). \end{itemdescr} \indextext{function object!\idxcode{mem_fn}|)} \rSec2[func.wrap]{Polymorphic function wrappers}% \indextext{function object!wrapper|(} \pnum This subclause describes a polymorphic wrapper class that encapsulates arbitrary callable objects. \rSec3[func.wrap.badcall]{Class \tcode{bad_function_call}}% \indexlibrary{\idxcode{bad_function_call}}% \pnum An exception of type \tcode{bad_function_call} is thrown by \tcode{function::operator()}~(\ref{func.wrap.func.inv}) when the function wrapper object has no target. \begin{codeblock} namespace std { class bad_function_call : public exception { public: // \ref{func.wrap.badcall.const}, constructor bad_function_call() noexcept; }; } \end{codeblock} \rSec4[func.wrap.badcall.const]{\tcode{bad_function_call} constructor} \indexlibrary{\idxcode{bad_function_call}!constructor}% \indexlibrarymember{what}{bad_function_call}% \begin{itemdecl} bad_function_call() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects Constructs a \tcode{bad_function_call} object. \end{itemdescr} \begin{itemdescr} \pnum\postconditions \tcode{what()} returns an \impldef{return value of \tcode{bad_function_call::what}} \ntbs. \end{itemdescr} \rSec3[func.wrap.func]{Class template \tcode{function}} \indexlibrary{\idxcode{function}}% \begin{codeblock} namespace std { template class function; // not defined template class function { public: using result_type = R; // \ref{func.wrap.func.con}, construct/copy/destroy function() noexcept; function(nullptr_t) noexcept; function(const function&); function(function&&); template function(F); function& operator=(const function&); function& operator=(function&&); function& operator=(nullptr_t) noexcept; template function& operator=(F&&); template function& operator=(reference_wrapper) noexcept; ~function(); // \ref{func.wrap.func.mod}, function modifiers void swap(function&) noexcept; // \ref{func.wrap.func.cap}, function capacity explicit operator bool() const noexcept; // \ref{func.wrap.func.inv}, function invocation R operator()(ArgTypes...) const; // \ref{func.wrap.func.targ}, function target access const type_info& target_type() const noexcept; template T* target() noexcept; template const T* target() const noexcept; }; template function(R(*)(ArgTypes...)) -> function; template function(F) -> function<@\seebelow@>; // \ref{func.wrap.func.nullptr}, Null pointer comparisons template bool operator==(const function&, nullptr_t) noexcept; template bool operator==(nullptr_t, const function&) noexcept; template bool operator!=(const function&, nullptr_t) noexcept; template bool operator!=(nullptr_t, const function&) noexcept; // \ref{func.wrap.func.alg}, specialized algorithms template void swap(function&, function&) noexcept; } \end{codeblock} \pnum The \tcode{function} class template provides polymorphic wrappers that generalize the notion of a function pointer. Wrappers can store, copy, and call arbitrary callable objects~(\ref{func.def}), given a call signature~(\ref{func.def}), allowing functions to be first-class objects. \pnum \indextext{callable type}% A callable type~(\ref{func.def}) \tcode{F} is \defn{Lvalue-Callable} for argument types \tcode{ArgTypes} and return type \tcode{R} if the expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)}, considered as an unevaluated operand (Clause~\ref{expr}), is well formed~(\ref{func.require}). \pnum The \tcode{function} class template is a call wrapper~(\ref{func.def}) whose call signature~(\ref{func.def}) is \tcode{R(ArgTypes...)}. \pnum \begin{note} The types deduced by the deduction guides for \tcode{function} may change in future versions of this International Standard. \end{note} \rSec4[func.wrap.func.con]{\tcode{function} construct/copy/destroy} \indexlibrary{\idxcode{function}!constructor}% \begin{itemdecl} function() noexcept; \end{itemdecl} \begin{itemdescr} \pnum\postconditions \tcode{!*this}. \end{itemdescr} \indexlibrary{\idxcode{function}!constructor}% \begin{itemdecl} function(nullptr_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \postconditions \tcode{!*this}. \end{itemdescr} \indexlibrary{\idxcode{function}!constructor}% \begin{itemdecl} function(const function& f); \end{itemdecl} \begin{itemdescr} \pnum \postconditions \tcode{!*this} if \tcode{!f}; otherwise, \tcode{*this} targets a copy of \tcode{f.target()}. \pnum \throws shall not throw exceptions if \tcode{f}'s target is a specialization of \tcode{reference_wrapper} or a function pointer. Otherwise, may throw \tcode{bad_alloc} or any exception thrown by the copy constructor of the stored callable object. \begin{note} Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where \tcode{f}'s target is an object holding only a pointer or reference to an object and a member function pointer. \end{note} \end{itemdescr} \indexlibrary{\idxcode{function}!constructor}% \begin{itemdecl} function(function&& f); \end{itemdecl} \begin{itemdescr} \pnum \postconditions If \tcode{!f}, \tcode{*this} has no target; otherwise, the target of \tcode{*this} is equivalent to the target of \tcode{f} before the construction, and \tcode{f} is in a valid state with an unspecified value. \pnum \throws shall not throw exceptions if \tcode{f}'s target is a specialization of \tcode{reference_wrapper} or a function pointer. Otherwise, may throw \tcode{bad_alloc} or any exception thrown by the copy or move constructor of the stored callable object. \begin{note} Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where \tcode{f}'s target is an object holding only a pointer or reference to an object and a member function pointer. \end{note} \end{itemdescr} \indexlibrary{\idxcode{function}!constructor}% \begin{itemdecl} template function(F f); \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{F} shall be \tcode{CopyConstructible}. \pnum \remarks This constructor template shall not participate in overload resolution unless \tcode{F} is Lvalue-Callable~(\ref{func.wrap.func}) for argument types \tcode{ArgTypes...} and return type \tcode{R}. \pnum \postconditions \tcode{!*this} if any of the following hold: \begin{itemize} \item \tcode{f} is a null function pointer value. \item \tcode{f} is a null member pointer value. \item \tcode{F} is an instance of the \tcode{function} class template, and \tcode{!f}. \end{itemize} \pnum Otherwise, \tcode{*this} targets a copy of \tcode{f} initialized with \tcode{std::move(f)}. \begin{note} Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where \tcode{f} is an object holding only a pointer or reference to an object and a member function pointer. \end{note} \pnum \throws shall not throw exceptions when \tcode{f} is a function pointer or a \tcode{reference_wrapper} for some \tcode{T}. Otherwise, may throw \tcode{bad_alloc} or any exception thrown by \tcode{F}'s copy or move constructor. \end{itemdescr} \begin{itemdecl} template function(F) -> function<@\seebelow@>; \end{itemdecl} \begin{itemdescr} \pnum \remarks This deduction guide participates in overload resolution only if \tcode{\&F::operator()} is well-formed when treated as an unevaluated operand. In that case, if \tcode{decltype(\&F::operator())} is of the form \tcode{R(G::*)(A...)}~\cv{}~\tcode{\&\opt{}~noexcept\opt} for a class type \tcode{G}, then the deduced type is \tcode{function}. \pnum \begin{example} \begin{codeblock} void f() { int i{5}; function g = [&](double) { return i; }; // deduces \tcode{function} } \end{codeblock} \end{example} \end{itemdescr} \indexlibrarymember{operator=}{function}% \begin{itemdecl} function& operator=(const function& f); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{function(f).swap(*this);} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{function}% \begin{itemdecl} function& operator=(function&& f); \end{itemdecl} \begin{itemdescr} \pnum \effects Replaces the target of \tcode{*this} with the target of \tcode{f}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{function}% \begin{itemdecl} function& operator=(nullptr_t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects If \tcode{*this != nullptr}, destroys the target of \tcode{this}. \pnum\postconditions \tcode{!(*this)}. \pnum\returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator=}{function}% \begin{itemdecl} template function& operator=(F&& f); \end{itemdecl} \begin{itemdescr} \pnum\effects As if by: \tcode{function(std::forward(f)).swap(*this);} \pnum\returns \tcode{*this}. \pnum\remarks This assignment operator shall not participate in overload resolution unless \tcode{decay_t} is Lvalue-Callable~(\ref{func.wrap.func}) for argument types \tcode{ArgTypes...} and return type \tcode{R}. \end{itemdescr} \indexlibrarymember{operator=}{function}% \begin{itemdecl} template function& operator=(reference_wrapper f) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects As if by: \tcode{function(f).swap(*this);} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrary{\idxcode{function}!destructor}% \begin{itemdecl} ~function(); \end{itemdecl} \begin{itemdescr} \pnum\effects If \tcode{*this != nullptr}, destroys the target of \tcode{this}. \end{itemdescr} \rSec4[func.wrap.func.mod]{\tcode{function} modifiers} \indexlibrarymember{swap}{function}% \begin{itemdecl} void swap(function& other) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects interchanges the targets of \tcode{*this} and \tcode{other}. \end{itemdescr} \rSec4[func.wrap.func.cap]{\tcode{function} capacity} \indexlibrarymember{operator bool}{function}% \begin{itemdecl} explicit operator bool() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{true} if \tcode{*this} has a target, otherwise \tcode{false}. \end{itemdescr} \rSec4[func.wrap.func.inv]{\tcode{function} invocation} \indexlibrary{\idxcode{function}!invocation}% \indexlibrarymember{operator()}{function}% \begin{itemdecl} R operator()(ArgTypes... args) const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholdernc{INVOKE}(f, std::forward(args)...)}~(\ref{func.require}), where \tcode{f} is the target object~(\ref{func.def}) of \tcode{*this}. \pnum\throws \tcode{bad_function_call} if \tcode{!*this}; otherwise, any exception thrown by the wrapped callable object. \end{itemdescr} \rSec4[func.wrap.func.targ]{\tcode{function} target access} \indexlibrarymember{target_type}{function}% \begin{itemdecl} const type_info& target_type() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns If \tcode{*this} has a target of type \tcode{T}, \tcode{typeid(T)}; otherwise, \tcode{typeid(void)}. \end{itemdescr} \indexlibrarymember{target}{function}% \begin{itemdecl} template T* target() noexcept; template const T* target() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns If \tcode{target_type() == typeid(T)} a pointer to the stored function target; otherwise a null pointer. \end{itemdescr} \rSec4[func.wrap.func.nullptr]{null pointer comparison functions} \indexlibrarymember{operator==}{function}% \begin{itemdecl} template bool operator==(const function& f, nullptr_t) noexcept; template bool operator==(nullptr_t, const function& f) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{!f}. \end{itemdescr} \indexlibrarymember{operator"!=}{function}% \begin{itemdecl} template bool operator!=(const function& f, nullptr_t) noexcept; template bool operator!=(nullptr_t, const function& f) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\returns \tcode{(bool)f}. \end{itemdescr} \rSec4[func.wrap.func.alg]{specialized algorithms} \indexlibrarymember{swap}{function}% \begin{itemdecl} template void swap(function& f1, function& f2) noexcept; \end{itemdecl} \begin{itemdescr} \pnum\effects As if by: \tcode{f1.swap(f2);} \end{itemdescr}% \indextext{function object!wrapper|)} \rSec2[func.search]{Searchers} \pnum This subclause provides function object types (\ref{function.objects}) for operations that search for a sequence \range{pat\textunderscore\nobreak first}{pat_last} in another sequence \range{first}{last} that is provided to the object's function call operator. The first sequence (the pattern to be searched for) is provided to the object's constructor, and the second (the sequence to be searched) is provided to the function call operator. \pnum Each specialization of a class template specified in this subclause \ref{func.search} shall meet the \tcode{CopyConstructible} and \tcode{CopyAssignable} requirements. Template parameters named \begin{itemize} \item \tcode{ForwardIterator}, \item \tcode{ForwardIterator1}, \item \tcode{ForwardIterator2}, \item \tcode{RandomAccessIterator}, \item \tcode{RandomAccessIterator1}, \item \tcode{RandomAccessIterator2}, and \item \tcode{BinaryPredicate} \end{itemize} of templates specified in this subclause \ref{func.search} shall meet the same requirements and semantics as specified in \ref{algorithms.general}. Template parameters named \tcode{Hash} shall meet the requirements as specified in \ref{hash.requirements}. \pnum The Boyer-Moore searcher implements the Boyer-Moore search algorithm. The Boyer-Moore-Horspool searcher implements the Boyer-Moore-Horspool search algorithm. In general, the Boyer-Moore searcher will use more memory and give better runtime performance than Boyer-Moore-Horspool. \rSec3[func.search.default]{Class template \tcode{default_searcher}} \indexlibrary{\idxcode{default_searcher}}% \begin{codeblock} template > class default_searcher { public: default_searcher(ForwardIterator1 pat_first, ForwardIterator1 pat_last, BinaryPredicate pred = BinaryPredicate()); template pair operator()(ForwardIterator2 first, ForwardIterator2 last) const; private: ForwardIterator1 pat_first_; // \expos ForwardIterator1 pat_last_; // \expos BinaryPredicate pred_; // \expos }; \end{codeblock} \indexlibrary{\idxcode{default_searcher}!constructor}% \begin{itemdecl} default_searcher(ForwardIterator pat_first, ForwardIterator pat_last, BinaryPredicate pred = BinaryPredicate()); \end{itemdecl} \begin{itemdescr} \pnum \effects % FIXME: The mbox prevents TeX from adding a bizarre hyphen after pat_last_. Constructs a \tcode{default_searcher} object, initializing \tcode{pat_first_} with \tcode{pat_first}, \mbox{\tcode{pat_last_}} with \tcode{pat_last}, and \tcode{pred_} with \tcode{pred}. \pnum \throws Any exception thrown by the copy constructor of \tcode{BinaryPredicate} or \tcode{ForwardIterator1}. \end{itemdescr} \indexlibrarymember{operator()}{default_searcher}% \begin{itemdecl} template pair operator()(ForwardIterator2 first, ForwardIterator2 last) const; \end{itemdecl} \begin{itemdescr} \pnum \effects Returns a pair of iterators \tcode{i} and \tcode{j} such that \begin{itemize} \item \tcode{i == search(first, last, pat_first_, pat_last_, pred_)}, and \item if \tcode{i == last}, then \tcode{j == last}, otherwise \tcode{j == next(i, distance(pat_first_, pat_last_))}. \end{itemize} \end{itemdescr} \rSec3[func.search.bm]{Class template \tcode{boyer_moore_searcher}} \indexlibrary{\idxcode{boyer_moore_searcher}}% \begin{codeblock} template ::value_type>, class BinaryPredicate = equal_to<>> class boyer_moore_searcher { public: boyer_moore_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); template pair operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const; private: RandomAccessIterator1 pat_first_; // \expos RandomAccessIterator1 pat_last_; // \expos Hash hash_; // \expos BinaryPredicate pred_; // \expos }; \end{codeblock} \indexlibrary{\idxcode{boyer_moore_searcher}!constructor}% \begin{itemdecl} boyer_moore_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); \end{itemdecl} \begin{itemdescr} \pnum \requires The value type of \tcode{RandomAccessIterator1} shall meet the \tcode{DefaultConstructible} requirements, the \tcode{CopyConstructible} requirements, and the \tcode{CopyAssignable} requirements. \pnum \requires For any two values \tcode{A} and \tcode{B} of the type \tcode{iterator_traits::val\-ue_type}, if \tcode{pred(A, B) == true}, then \tcode{hf(A) == hf(B)} shall be \tcode{true}. \pnum \effects Constructs a \tcode{boyer_moore_searcher} object, initializing \tcode{pat_first_} with \tcode{pat_first}, \tcode{pat_last_} with \tcode{pat_last}, \tcode{hash_} with \tcode{hf}, and \tcode{pred_} with \tcode{pred}. \pnum \throws Any exception thrown by the copy constructor of \tcode{RandomAccessIterator1}, or by the default constructor, copy constructor, or the copy assignment operator of the value type of \tcode{RandomAccess\-Iterator1}, or the copy constructor or \tcode{operator()} of \tcode{BinaryPredicate} or \tcode{Hash}. May throw \tcode{bad_alloc} if additional memory needed for internal data structures cannot be allocated. \end{itemdescr} \indexlibrarymember{operator()}{boyer_moore_searcher}% \begin{itemdecl} template pair operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{RandomAccessIterator1} and \tcode{RandomAccessIterator2} shall have the same value type. \pnum \effects Finds a subsequence of equal values in a sequence. \pnum \returns A pair of iterators \tcode{i} and \tcode{j} such that \begin{itemize} \item \tcode{i} is the first iterator in the range \range{first}{last - (pat_last_ - pat_first_)} such that for every non-negative integer \tcode{n} less than \tcode{pat_last_ - pat_first_} the following condition holds: \tcode{pred(*(i + n), *(pat_first_ + n)) != false}, and \item \tcode{j == next(i, distance(pat_first_, pat_last_))}. \end{itemize} Returns \tcode{make_pair(first, first)} if \range{pat_first_}{pat_last_} is empty, otherwise returns \tcode{make_pair(last, last)} if no such iterator is found. \pnum \complexity At most \tcode{(last - first) * (pat_last_ - pat_first_)} applications of the predicate. \end{itemdescr} \rSec3[func.search.bmh]{Class template \tcode{boyer_moore_horspool_searcher}} \indexlibrary{\idxcode{boyer_moore_horspool_searcher}}% \begin{codeblock} template ::value_type>, class BinaryPredicate = equal_to<>> class boyer_moore_horspool_searcher { public: boyer_moore_horspool_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); template pair operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const; private: RandomAccessIterator1 pat_first_; // \expos RandomAccessIterator1 pat_last_; // \expos Hash hash_; // \expos BinaryPredicate pred_; // \expos }; \end{codeblock} \indexlibrary{\idxcode{boyer_moore_horspool_searcher}!constructor}% \begin{itemdecl} boyer_moore_horspool_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); \end{itemdecl} \begin{itemdescr} \pnum \requires The value type of \tcode{RandomAccessIterator1} shall meet the \tcode{DefaultConstructible}, \tcode{Copy\-Constructible}, and \tcode{CopyAssignable} requirements. \pnum \requires For any two values \tcode{A} and \tcode{B} of the type \tcode{iterator_traits::val\-ue_type}, if \tcode{pred(A, B) == true}, then \tcode{hf(A) == hf(B)} shall be \tcode{true}. \pnum \effects Constructs a \tcode{boyer_moore_horspool_searcher} object, initializing \tcode{pat_first_} with \tcode{pat_first}, \tcode{pat_last_} with \tcode{pat_last}, \tcode{hash_} with \tcode{hf}, and \tcode{pred_} with \tcode{pred}. \pnum \throws Any exception thrown by the copy constructor of \tcode{RandomAccessIterator1}, or by the default constructor, copy constructor, or the copy assignment operator of the value type of \tcode{RandomAccess\-Iterator1} or the copy constructor or \tcode{operator()} of \tcode{BinaryPredicate} or \tcode{Hash}. May throw \tcode{bad_alloc} if additional memory needed for internal data structures cannot be allocated. \end{itemdescr} \indexlibrarymember{operator()}{boyer_moore_horspool_searcher}% \begin{itemdecl} template pair operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{RandomAccessIterator1} and \tcode{RandomAccessIterator2} shall have the same value type. \pnum \effects Finds a subsequence of equal values in a sequence. \pnum \returns A pair of iterators \tcode{i} and \tcode{j} such that \begin{itemize} \item \tcode{i} is the first iterator \tcode{i} in the range \range{first}{last - (pat_last_ - pat_first_)} such that for every non-negative integer \tcode{n} less than \tcode{pat_last_ - pat_first_} the following condition holds: \tcode{pred(*(i + n), *(pat_first_ + n)) != false}, and \item \tcode{j == next(i, distance(pat_first_, pat_last_))}. \end{itemize} Returns \tcode{make_pair(first, first)} if \range{pat_first_}{pat_last_} is empty, otherwise returns \tcode{make_pair(last, last)} if no such iterator is found. \pnum \complexity At most \tcode{(last - first) * (pat_last_ - pat_first_)} applications of the predicate. \end{itemdescr} \rSec2[unord.hash]{Class template \tcode{hash}} \pnum \indexlibrary{\idxcode{hash}}% \indextext{\idxcode{hash}!instantiation restrictions}% The unordered associative containers defined in \ref{unord} use specializations of the class template \tcode{hash} (\ref{functional.syn}) as the default hash function. \pnum Each specialization of \tcode{hash} is either enabled or disabled, as described below. \begin{note} Enabled specializations meet the requirements of \tcode{Hash}, and disabled specializations do not. \end{note} Each header that declares the template \tcode{hash} provides enabled specializations of \tcode{hash} for \tcode{nullptr_t} and all cv-unqualified arithmetic, enumeration, and pointer types. For any type \tcode{Key} for which neither the library nor the user provides an explicit or partial specialization of the class template \tcode{hash}, \tcode{hash} is disabled. \pnum If the library provides an explicit or partial specialization of \tcode{hash}, that specialization is enabled except as noted otherwise, and its member functions are \tcode{noexcept} except as noted otherwise. \pnum If \tcode{H} is a disabled specialization of \tcode{hash}, these values are \tcode{false}: \tcode{is_default_constructible_v}, \tcode{is_copy_constructible_v}, \tcode{is_move_constructible_v}, \tcode{is_copy_assignable_v}, and \tcode{is_move_assignable_v}. Disabled specializations of \tcode{hash} are not function object types~(\ref{function.objects}). \begin{note} This means that the specialization of \tcode{hash} exists, but any attempts to use it as a \tcode{Hash} will be ill-formed. \end{note} \pnum An enabled specialization \tcode{hash} will: \begin{itemize} \item satisfy the \tcode{Hash} requirements~(\ref{hash.requirements}), with \tcode{Key} as the function call argument type, the \tcode{Default\-Constructible} requirements (Table~\ref{tab:defaultconstructible}), the \tcode{CopyAssignable} requirements (Table~\ref{tab:copyassignable}), \item be swappable~(\ref{swappable.requirements}) for lvalues, \item satisfy the requirement that if \tcode{k1 == k2} is \tcode{true}, \tcode{h(k1) == h(k2)} is also \tcode{true}, where \tcode{h} is an object of type \tcode{hash} and \tcode{k1} and \tcode{k2} are objects of type \tcode{Key}; \item satisfy the requirement that the expression \tcode{h(k)}, where \tcode{h} is an object of type \tcode{hash} and \tcode{k} is an object of type \tcode{Key}, shall not throw an exception unless \tcode{hash} is a user-defined specialization that depends on at least one user-defined type. \end{itemize} \rSec1[meta]{Metaprogramming and type traits} \pnum This subclause describes components used by \Cpp programs, particularly in templates, to support the widest possible range of types, optimise template code usage, detect type related user errors, and perform type inference and transformation at compile time. It includes type classification traits, type property inspection traits, and type transformations. The type classification traits describe a complete taxonomy of all possible \Cpp types, and state where in that taxonomy a given type belongs. The type property inspection traits allow important characteristics of types or of combinations of types to be inspected. The type transformations allow certain properties of types to be manipulated. \pnum \indextext{signal-safe!type traits}% All functions specified in this subclause are signal-safe~(\ref{csignal.syn}). \rSec2[meta.rqmts]{Requirements} \pnum A \defn{UnaryTypeTrait} describes a property of a type. It shall be a class template that takes one template type argument and, optionally, additional arguments that help define the property being described. It shall be \tcode{DefaultConstructible}, \tcode{CopyConstructible}, and publicly and unambiguously derived, directly or indirectly, from its \defn{base characteristic}, which is a specialization of the template \tcode{integral_constant}~(\ref{meta.help}), with the arguments to the template \tcode{integral_constant} determined by the requirements for the particular property being described. The member names of the base characteristic shall not be hidden and shall be unambiguously available in the \tcode{UnaryTypeTrait}. \pnum A \defn{BinaryTypeTrait} describes a relationship between two types. It shall be a class template that takes two template type arguments and, optionally, additional arguments that help define the relationship being described. It shall be \tcode{DefaultConstructible}, \tcode{CopyConstructible}, and publicly and unambiguously derived, directly or indirectly, from its \term{base characteristic}, which is a specialization of the template \tcode{integral_constant}~(\ref{meta.help}), with the arguments to the template \tcode{integral_constant} determined by the requirements for the particular relationship being described. The member names of the base characteristic shall not be hidden and shall be unambiguously available in the \tcode{BinaryTypeTrait}. \pnum A \defn{TransformationTrait} modifies a property of a type. It shall be a class template that takes one template type argument and, optionally, additional arguments that help define the modification. It shall define a publicly accessible nested type named \tcode{type}, which shall be a synonym for the modified type. \rSec2[meta.type.synop]{Header \tcode{} synopsis} \indextext{\idxhdr{type_traits}}% \indexlibrary{\idxhdr{type_traits}}% \begin{codeblock} namespace std { // \ref{meta.help}, helper class template struct integral_constant; template using bool_constant = integral_constant; using true_type = bool_constant; using false_type = bool_constant; // \ref{meta.unary.cat}, primary type categories template struct is_void; template struct is_null_pointer; template struct is_integral; template struct is_floating_point; template struct is_array; template struct is_pointer; template struct is_lvalue_reference; template struct is_rvalue_reference; template struct is_member_object_pointer; template struct is_member_function_pointer; template struct is_enum; template struct is_union; template struct is_class; template struct is_function; // \ref{meta.unary.comp}, composite type categories template struct is_reference; template struct is_arithmetic; template struct is_fundamental; template struct is_object; template struct is_scalar; template struct is_compound; template struct is_member_pointer; // \ref{meta.unary.prop}, type properties template struct is_const; template struct is_volatile; template struct is_trivial; template struct is_trivially_copyable; template struct is_standard_layout; template struct is_pod; template struct is_empty; template struct is_polymorphic; template struct is_abstract; template struct is_final; template struct is_aggregate; template struct is_signed; template struct is_unsigned; template struct is_constructible; template struct is_default_constructible; template struct is_copy_constructible; template struct is_move_constructible; template struct is_assignable; template struct is_copy_assignable; template struct is_move_assignable; template struct is_swappable_with; template struct is_swappable; template struct is_destructible; template struct is_trivially_constructible; template struct is_trivially_default_constructible; template struct is_trivially_copy_constructible; template struct is_trivially_move_constructible; template struct is_trivially_assignable; template struct is_trivially_copy_assignable; template struct is_trivially_move_assignable; template struct is_trivially_destructible; template struct is_nothrow_constructible; template struct is_nothrow_default_constructible; template struct is_nothrow_copy_constructible; template struct is_nothrow_move_constructible; template struct is_nothrow_assignable; template struct is_nothrow_copy_assignable; template struct is_nothrow_move_assignable; template struct is_nothrow_swappable_with; template struct is_nothrow_swappable; template struct is_nothrow_destructible; template struct has_virtual_destructor; template struct has_unique_object_representations; // \ref{meta.unary.prop.query}, type property queries template struct alignment_of; template struct rank; template struct extent; // \ref{meta.rel}, type relations template struct is_same; template struct is_base_of; template struct is_convertible; template struct is_invocable; template struct is_invocable_r; template struct is_nothrow_invocable; template struct is_nothrow_invocable_r; // \ref{meta.trans.cv}, const-volatile modifications template struct remove_const; template struct remove_volatile; template struct remove_cv; template struct add_const; template struct add_volatile; template struct add_cv; template using remove_const_t = typename remove_const::type; template using remove_volatile_t = typename remove_volatile::type; template using remove_cv_t = typename remove_cv::type; template using add_const_t = typename add_const::type; template using add_volatile_t = typename add_volatile::type; template using add_cv_t = typename add_cv::type; // \ref{meta.trans.ref}, reference modifications template struct remove_reference; template struct add_lvalue_reference; template struct add_rvalue_reference; template using remove_reference_t = typename remove_reference::type; template using add_lvalue_reference_t = typename add_lvalue_reference::type; template using add_rvalue_reference_t = typename add_rvalue_reference::type; // \ref{meta.trans.sign}, sign modifications template struct make_signed; template struct make_unsigned; template using make_signed_t = typename make_signed::type; template using make_unsigned_t = typename make_unsigned::type; // \ref{meta.trans.arr}, array modifications template struct remove_extent; template struct remove_all_extents; template using remove_extent_t = typename remove_extent::type; template using remove_all_extents_t = typename remove_all_extents::type; // \ref{meta.trans.ptr}, pointer modifications template struct remove_pointer; template struct add_pointer; template using remove_pointer_t = typename remove_pointer::type; template using add_pointer_t = typename add_pointer::type; // \ref{meta.trans.other}, other transformations template // see \ref{meta.trans.other} struct aligned_storage; template struct aligned_union; template struct decay; template struct enable_if; template struct conditional; template struct common_type; template struct underlying_type; template struct invoke_result; template // see \ref{meta.trans.other} using aligned_storage_t = typename aligned_storage::type; template using aligned_union_t = typename aligned_union::type; template using decay_t = typename decay::type; template using enable_if_t = typename enable_if::type; template using conditional_t = typename conditional::type; template using common_type_t = typename common_type::type; template using underlying_type_t = typename underlying_type::type; template using invoke_result_t = typename invoke_result::type; template using void_t = void; // \ref{meta.logical}, logical operator traits template struct conjunction; template struct disjunction; template struct negation; // \ref{meta.unary.cat}, primary type categories template inline constexpr bool is_void_v = is_void::value; template inline constexpr bool is_null_pointer_v = is_null_pointer::value; template inline constexpr bool is_integral_v = is_integral::value; template inline constexpr bool is_floating_point_v = is_floating_point::value; template inline constexpr bool is_array_v = is_array::value; template inline constexpr bool is_pointer_v = is_pointer::value; template inline constexpr bool is_lvalue_reference_v = is_lvalue_reference::value; template inline constexpr bool is_rvalue_reference_v = is_rvalue_reference::value; template inline constexpr bool is_member_object_pointer_v = is_member_object_pointer::value; template inline constexpr bool is_member_function_pointer_v = is_member_function_pointer::value; template inline constexpr bool is_enum_v = is_enum::value; template inline constexpr bool is_union_v = is_union::value; template inline constexpr bool is_class_v = is_class::value; template inline constexpr bool is_function_v = is_function::value; // \ref{meta.unary.comp}, composite type categories template inline constexpr bool is_reference_v = is_reference::value; template inline constexpr bool is_arithmetic_v = is_arithmetic::value; template inline constexpr bool is_fundamental_v = is_fundamental::value; template inline constexpr bool is_object_v = is_object::value; template inline constexpr bool is_scalar_v = is_scalar::value; template inline constexpr bool is_compound_v = is_compound::value; template inline constexpr bool is_member_pointer_v = is_member_pointer::value; // \ref{meta.unary.prop}, type properties template inline constexpr bool is_const_v = is_const::value; template inline constexpr bool is_volatile_v = is_volatile::value; template inline constexpr bool is_trivial_v = is_trivial::value; template inline constexpr bool is_trivially_copyable_v = is_trivially_copyable::value; template inline constexpr bool is_standard_layout_v = is_standard_layout::value; template inline constexpr bool is_pod_v = is_pod::value; template inline constexpr bool is_empty_v = is_empty::value; template inline constexpr bool is_polymorphic_v = is_polymorphic::value; template inline constexpr bool is_abstract_v = is_abstract::value; template inline constexpr bool is_final_v = is_final::value; template inline constexpr bool is_aggregate_v = is_aggregate::value; template inline constexpr bool is_signed_v = is_signed::value; template inline constexpr bool is_unsigned_v = is_unsigned::value; template inline constexpr bool is_constructible_v = is_constructible::value; template inline constexpr bool is_default_constructible_v = is_default_constructible::value; template inline constexpr bool is_copy_constructible_v = is_copy_constructible::value; template inline constexpr bool is_move_constructible_v = is_move_constructible::value; template inline constexpr bool is_assignable_v = is_assignable::value; template inline constexpr bool is_copy_assignable_v = is_copy_assignable::value; template inline constexpr bool is_move_assignable_v = is_move_assignable::value; template inline constexpr bool is_swappable_with_v = is_swappable_with::value; template inline constexpr bool is_swappable_v = is_swappable::value; template inline constexpr bool is_destructible_v = is_destructible::value; template inline constexpr bool is_trivially_constructible_v = is_trivially_constructible::value; template inline constexpr bool is_trivially_default_constructible_v = is_trivially_default_constructible::value; template inline constexpr bool is_trivially_copy_constructible_v = is_trivially_copy_constructible::value; template inline constexpr bool is_trivially_move_constructible_v = is_trivially_move_constructible::value; template inline constexpr bool is_trivially_assignable_v = is_trivially_assignable::value; template inline constexpr bool is_trivially_copy_assignable_v = is_trivially_copy_assignable::value; template inline constexpr bool is_trivially_move_assignable_v = is_trivially_move_assignable::value; template inline constexpr bool is_trivially_destructible_v = is_trivially_destructible::value; template inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible::value; template inline constexpr bool is_nothrow_default_constructible_v = is_nothrow_default_constructible::value; template inline constexpr bool is_nothrow_copy_constructible_v = is_nothrow_copy_constructible::value; template inline constexpr bool is_nothrow_move_constructible_v = is_nothrow_move_constructible::value; template inline constexpr bool is_nothrow_assignable_v = is_nothrow_assignable::value; template inline constexpr bool is_nothrow_copy_assignable_v = is_nothrow_copy_assignable::value; template inline constexpr bool is_nothrow_move_assignable_v = is_nothrow_move_assignable::value; template inline constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with::value; template inline constexpr bool is_nothrow_swappable_v = is_nothrow_swappable::value; template inline constexpr bool is_nothrow_destructible_v = is_nothrow_destructible::value; template inline constexpr bool has_virtual_destructor_v = has_virtual_destructor::value; template inline constexpr bool has_unique_object_representations_v = has_unique_object_representations::value; // \ref{meta.unary.prop.query}, type property queries template inline constexpr size_t alignment_of_v = alignment_of::value; template inline constexpr size_t rank_v = rank::value; template inline constexpr size_t extent_v = extent::value; // \ref{meta.rel}, type relations template inline constexpr bool is_same_v = is_same::value; template inline constexpr bool is_base_of_v = is_base_of::value; template inline constexpr bool is_convertible_v = is_convertible::value; template inline constexpr bool is_invocable_v = is_invocable::value; template inline constexpr bool is_invocable_r_v = is_invocable_r::value; template inline constexpr bool is_nothrow_invocable_v = is_nothrow_invocable::value; template inline constexpr bool is_nothrow_invocable_r_v = is_nothrow_invocable_r::value; // \ref{meta.logical}, logical operator traits template inline constexpr bool conjunction_v = conjunction::value; template inline constexpr bool disjunction_v = disjunction::value; template inline constexpr bool negation_v = negation::value; } \end{codeblock} \pnum The behavior of a program that adds specializations for any of the templates defined in this subclause is undefined unless otherwise specified. \pnum Unless otherwise specified, an incomplete type may be used to instantiate a template in this subclause. \rSec2[meta.help]{Helper classes} \begin{codeblock} namespace std { template struct integral_constant { static constexpr T value = v; using value_type = T; using type = integral_constant; constexpr operator value_type() const noexcept { return value; } constexpr value_type operator()() const noexcept { return value; } }; } \end{codeblock} \indexlibrary{\idxcode{integral_constant}}% \indexlibrary{\idxcode{bool_constant}}% \indexlibrary{\idxcode{true_type}}% \indexlibrary{\idxcode{false_type}}% \pnum The class template \tcode{integral_constant}, alias template \tcode{bool_constant}, and its associated \grammarterm{typedef-name}{s} \tcode{true_type} and \tcode{false_type} are used as base classes to define the interface for various type traits. \rSec2[meta.unary]{Unary type traits} \pnum This subclause contains templates that may be used to query the properties of a type at compile time. \pnum Each of these templates shall be a \tcode{UnaryTypeTrait}~(\ref{meta.rqmts}) with a base characteristic of \tcode{true_type} if the corresponding condition is \tcode{true}, otherwise \tcode{false_type}. \rSec3[meta.unary.cat]{Primary type categories} \pnum The primary type categories correspond to the descriptions given in section~\ref{basic.types} of the \Cpp standard. \pnum For any given type \tcode{T}, the result of applying one of these templates to \tcode{T} and to \cv{}~\tcode{T} shall yield the same result. \pnum \begin{note} For any given type \tcode{T}, exactly one of the primary type categories has a \tcode{value} member that evaluates to \tcode{true}. \end{note} \begin{libreqtab3e}{Primary type category predicates}{tab:type-traits.primary} \\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\\capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{is_void}}% \tcode{template }\br \tcode{struct is_void;} & \tcode{T} is \tcode{void} & \\ \rowsep \indexlibrary{\idxcode{is_null_pointer}}% \tcode{template }\br \tcode{struct is_null_pointer;} & \tcode{T} is \tcode{nullptr_t}~(\ref{basic.fundamental}) & \\ \rowsep \indexlibrary{\idxcode{is_integral}}% \tcode{template }\br \tcode{struct is_integral;} & \tcode{T} is an integral type~(\ref{basic.fundamental}) & \\ \rowsep \indexlibrary{\idxcode{is_floating_point}}% \tcode{template }\br \tcode{struct is_floating_point;} & \tcode{T} is a floating-point type~(\ref{basic.fundamental}) & \\ \rowsep \indexlibrary{\idxcode{is_array}}% \tcode{template }\br \tcode{struct is_array;} & \tcode{T} is an array type~(\ref{basic.compound}) of known or unknown extent & Class template \tcode{array}~(\ref{array}) is not an array type. \\ \rowsep \indexlibrary{\idxcode{is_pointer}}% \tcode{template }\br \tcode{struct is_pointer;} & \tcode{T} is a pointer type~(\ref{basic.compound}) & Includes pointers to functions but not pointers to non-static members. \\ \rowsep \indexlibrary{\idxcode{is_lvalue_reference}}% \tcode{template }\br \tcode{struct is_lvalue_reference;} & \tcode{T} is an lvalue reference type~(\ref{dcl.ref}) & \\ \rowsep \indexlibrary{\idxcode{is_rvalue_reference}}% \tcode{template }\br \tcode{struct is_rvalue_reference;} & \tcode{T} is an rvalue reference type~(\ref{dcl.ref}) & \\ \rowsep \indexlibrary{\idxcode{is_member_object_pointer}}% \tcode{template }\br \tcode{struct is_member_object_pointer;}& \tcode{T} is a pointer to non-static data member & \\ \rowsep \indexlibrary{\idxcode{is_member_function_pointer}}% \tcode{template }\br \tcode{struct is_member_function_pointer;}& \tcode{T} is a pointer to non-static member function & \\ \rowsep \indexlibrary{\idxcode{is_enum}}% \tcode{template }\br \tcode{struct is_enum;} & \tcode{T} is an enumeration type~(\ref{basic.compound}) & \\ \rowsep \indexlibrary{\idxcode{is_union}}% \tcode{template }\br \tcode{struct is_union;} & \tcode{T} is a union type~(\ref{basic.compound}) & \\ \rowsep \indexlibrary{\idxcode{is_class}}% \tcode{template }\br \tcode{struct is_class;} & \tcode{T} is a non-union class type~(\ref{basic.compound}) & \\ \rowsep \indexlibrary{\idxcode{is_function}}% \tcode{template }\br \tcode{struct is_function;} & \tcode{T} is a function type~(\ref{basic.compound}) & \\ \end{libreqtab3e} \rSec3[meta.unary.comp]{Composite type traits} \pnum These templates provide convenient compositions of the primary type categories, corresponding to the descriptions given in section~\ref{basic.types}. \pnum For any given type \tcode{T}, the result of applying one of these templates to \tcode{T} and to \cv{}~\tcode{T} shall yield the same result. \begin{libreqtab3b}{Composite type category predicates}{tab:type-traits.composite} \\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{is_reference}}% \tcode{template }\br \tcode{struct is_reference;} & \tcode{T} is an lvalue reference or an rvalue reference & \\ \rowsep \indexlibrary{\idxcode{is_arithmetic}}% \tcode{template }\br \tcode{struct is_arithmetic;} & \tcode{T} is an arithmetic type~(\ref{basic.fundamental}) & \\ \rowsep \indexlibrary{\idxcode{is_fundamental}}% \tcode{template }\br \tcode{struct is_fundamental;} & \tcode{T} is a fundamental type~(\ref{basic.fundamental}) & \\ \rowsep \indexlibrary{\idxcode{is_object}}% \tcode{template }\br \tcode{struct is_object;} & \tcode{T} is an object type~(\ref{basic.types}) & \\ \rowsep \indexlibrary{\idxcode{is_scalar}}% \tcode{template }\br \tcode{struct is_scalar;} & \tcode{T} is a scalar type~(\ref{basic.types}) & \\ \rowsep \indexlibrary{\idxcode{is_compound}}% \tcode{template }\br \tcode{struct is_compound;} & \tcode{T} is a compound type~(\ref{basic.compound}) & \\ \rowsep \indexlibrary{\idxcode{is_member_pointer}}% \tcode{template }\br \tcode{struct is_member_pointer;} & \tcode{T} is a pointer to non-static data member or non-static member function & \\ \end{libreqtab3b} \rSec3[meta.unary.prop]{Type properties} \pnum These templates provide access to some of the more important properties of types. \pnum It is unspecified whether the library defines any full or partial specializations of any of these templates. \pnum For all of the class templates \tcode{X} declared in this subclause, instantiating that template with a template-argument that is a class template specialization may result in the implicit instantiation of the template argument if and only if the semantics of \tcode{X} require that the argument must be a complete type. \pnum For the purpose of defining the templates in this subclause, a function call expression \tcode{declval()} for any type \tcode{T} is considered to be a trivial~(\ref{basic.types}, \ref{special}) function call that is not an odr-use~(\ref{basic.def.odr}) of \tcode{declval} in the context of the corresponding definition notwithstanding the restrictions of~\ref{declval}. \begin{libreqtab3b}{Type property predicates}{tab:type-traits.properties} \\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Preconditions} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Preconditions} \\ \capsep \endhead \indexlibrary{\idxcode{is_const}}% \tcode{template }\br \tcode{struct is_const;} & \tcode{T} is const-qualified~(\ref{basic.type.qualifier}) & \\ \rowsep \indexlibrary{\idxcode{is_volatile}}% \tcode{template }\br \tcode{struct is_volatile;} & \tcode{T} is volatile-qualified~(\ref{basic.type.qualifier}) & \\ \rowsep \indexlibrary{\idxcode{is_trivial}}% \tcode{template }\br \tcode{struct is_trivial;} & \tcode{T} is a trivial type~(\ref{basic.types}) & \tcode{remove_all_extents_t} shall be a complete type or \cv{}~\tcode{void}. \\ \rowsep \indexlibrary{\idxcode{is_trivially_copyable}}% \tcode{template }\br \tcode{struct is_trivially_copyable;} & \tcode{T} is a trivially copyable type~(\ref{basic.types}) & \tcode{remove_all_extents_t} shall be a complete type or \cv{}~\tcode{void}. \\ \rowsep \indexlibrary{\idxcode{is_standard_layout}}% \tcode{template }\br \tcode{struct is_standard_layout;} & \tcode{T} is a standard-layout type~(\ref{basic.types}) & \tcode{remove_all_extents_t} shall be a complete type or \cv{}~\tcode{void}. \\ \rowsep \indexlibrary{\idxcode{is_pod}}% \tcode{template }\br \tcode{struct is_pod;} & \tcode{T} is a POD type~(\ref{basic.types}) & \tcode{remove_all_extents_t} shall be a complete type or \cv{}~\tcode{void}. \\ \rowsep \indexlibrary{\idxcode{is_empty}!class}% \tcode{template }\br \tcode{struct is_empty;} & \tcode{T} is a class type, but not a union type, with no non-static data members other than bit-fields of length 0, no virtual member functions, no virtual base classes, and no base class \tcode{B} for which \tcode{is_empty_v} is \tcode{false}. & If \tcode{T} is a non-union class type, \tcode{T} shall be a complete type. \\ \rowsep \indexlibrary{\idxcode{is_polymorphic}}% \tcode{template }\br \tcode{struct is_polymorphic;} & \tcode{T} is a polymorphic class~(\ref{class.virtual}) & If \tcode{T} is a non-union class type, \tcode{T} shall be a complete type. \\ \rowsep \indexlibrary{\idxcode{is_abstract}}% \tcode{template }\br \tcode{struct is_abstract;} & \tcode{T} is an abstract class~(\ref{class.abstract}) & If \tcode{T} is a non-union class type, \tcode{T} shall be a complete type. \\ \rowsep \indexlibrary{\idxcode{is_final}}% \tcode{template }\br \tcode{struct is_final;} & \tcode{T} is a class type marked with the \grammarterm{class-virt-specifier} \tcode{final} (Clause~\ref{class}). \begin{note} A union is a class type that can be marked with \tcode{final}. \end{note} & If \tcode{T} is a class type, \tcode{T} shall be a complete type. \\ \rowsep \indexlibrary{\idxcode{is_aggregate}}% \tcode{template }\br \tcode{struct is_aggregate;} & \tcode{T} is an aggregate type~(\ref{dcl.init.aggr}) & \tcode{remove_all_extents_t} shall be a complete type or \cv~\tcode{void}. \\ \rowsep \indexlibrary{\idxcode{is_signed}!class}% \tcode{template }\br \tcode{struct is_signed;} & If \tcode{is_arithmetic_v} is \tcode{true}, the same result as \tcode{T(-1) < T(0)}; otherwise, \tcode{false} & \\ \rowsep \indexlibrary{\idxcode{is_unsigned}}% \tcode{template }\br \tcode{struct is_unsigned;} & If \tcode{is_arithmetic_v} is \tcode{true}, the same result as \tcode{T(0) < T(-1)}; otherwise, \tcode{false} & \\ \rowsep \indexlibrary{\idxcode{is_constructible}}% \tcode{template }\br \tcode{struct is_constructible;} & For a function type \tcode{T} or for a \cv{}~\tcode{void} type \tcode{T}, \tcode{is_constructible_v} is \tcode{false}, otherwise \seebelow & \tcode{T} and all types in the parameter pack \tcode{Args} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_default_constructible}}% \tcode{template }\br \tcode{struct is_default_constructible;} & \tcode{is_constructible_v} is \tcode{true}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_copy_constructible}}% \tcode{template }\br \tcode{struct is_copy_constructible;} & For a referenceable type \tcode{T} (\ref{defns.referenceable}), the same result as \tcode{is_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_move_constructible}}% \tcode{template }\br \tcode{struct is_move_constructible;} & For a referenceable type \tcode{T}, the same result as \tcode{is_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_assignable}}% \tcode{template }\br \tcode{struct is_assignable;} & The expression \tcode{declval() =} \tcode{declval()} is well-formed when treated as an unevaluated operand (Clause~\ref{expr}). Access checking is performed as if in a context unrelated to \tcode{T} and \tcode{U}. Only the validity of the immediate context of the assignment expression is considered. \begin{note} The compilation of the expression can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} & \tcode{T} and \tcode{U} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_copy_assignable}}% \tcode{template }\br \tcode{struct is_copy_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_move_assignable}}% \tcode{template }\br \tcode{struct is_move_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_swappable_with}}% \tcode{template }\br \tcode{struct is_swappable_with;} & The expressions \tcode{swap(declval(), declval())} and \tcode{swap(declval(), declval())} are each well-formed when treated as an unevaluated operand (Clause~\ref{expr}) in an overload-resolution context for swappable values~(\ref{swappable.requirements}). Access checking is performed as if in a context unrelated to \tcode{T} and \tcode{U}. Only the validity of the immediate context of the \tcode{swap} expressions is considered. \begin{note} The compilation of the expressions can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} & \tcode{T} and \tcode{U} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_swappable}}% \tcode{template }\br \tcode{struct is_swappable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_swappable_with_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_destructible}}% \tcode{template }\br \tcode{struct is_destructible;} & Either \tcode{T} is a reference type, or \tcode{T} is a complete object type for which the expression \tcode{declval().\~U()} is well-formed when treated as an unevaluated operand (Clause \ref{expr}), where \tcode{U} is \tcode{remove_all_extents}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_constructible}}% \tcode{template }\br \tcode{struct}\br \tcode{is_trivially_constructible;} & \tcode{is_constructible_v} is \tcode{true} and the variable definition for \tcode{is_constructible}, as defined below, is known to call no operation that is not trivial~(\ref{basic.types},~\ref{special}). & \tcode{T} and all types in the parameter pack \tcode{Args} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_default_constructible}}% \tcode{template }\br \tcode{struct is_trivially_default_constructible;} & \tcode{is_trivially_constructible_v} is \tcode{true}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_copy_constructible}}% \tcode{template }\br \tcode{struct is_trivially_copy_constructible;} & For a referenceable type \tcode{T}, the same result as \tcode{is_trivially_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_move_constructible}}% \tcode{template }\br \tcode{struct is_trivially_move_constructible;} & For a referenceable type \tcode{T}, the same result as \tcode{is_trivially_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_assignable}}% \tcode{template }\br \tcode{struct is_trivially_assignable;} & \tcode{is_assignable_v} is \tcode{true} and the assignment, as defined by \tcode{is_assignable}, is known to call no operation that is not trivial (\ref{basic.types},~\ref{special}). & \tcode{T} and \tcode{U} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_copy_assignable}}% \tcode{template }\br \tcode{struct is_trivially_copy_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_trivially_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_move_assignable}}% \tcode{template }\br \tcode{struct is_trivially_move_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_trivially_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_trivially_destructible}}% \tcode{template }\br \tcode{struct is_trivially_destructible;} & \tcode{is_destructible_v} is \tcode{true} and the indicated destructor is known to be trivial. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_constructible}}% \tcode{template }\br \tcode{struct is_nothrow_constructible;} & \tcode{is_constructible_v} is \tcode{true} and the variable definition for \tcode{is_constructible}, as defined below, is known not to throw any exceptions~(\ref{expr.unary.noexcept}). & \tcode{T} and all types in the parameter pack \tcode{Args} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_default_constructible}}% \tcode{template }\br \tcode{struct is_nothrow_default_constructible;} & \tcode{is_nothrow_constructible_v} is \tcode{true}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_copy_constructible}}% \tcode{template }\br \tcode{struct is_nothrow_copy_constructible;} & For a referenceable type \tcode{T}, the same result as \tcode{is_nothrow_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_move_constructible}}% \tcode{template }\br \tcode{struct is_nothrow_move_constructible;} & For a referenceable type \tcode{T}, the same result as \tcode{is_nothrow_constructible_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_assignable}}% \tcode{template }\br \tcode{struct is_nothrow_assignable;} & \tcode{is_assignable_v} is \tcode{true} and the assignment is known not to throw any exceptions~(\ref{expr.unary.noexcept}). & \tcode{T} and \tcode{U} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_copy_assignable}}% \tcode{template }\br \tcode{struct is_nothrow_copy_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_nothrow_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_move_assignable}}% \tcode{template }\br \tcode{struct is_nothrow_move_assignable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_nothrow_assignable_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_swappable_with}}% \tcode{template }\br \tcode{struct is_nothrow_swappable_with;} & \tcode{is_swappable_with_v} is \tcode{true} and each \tcode{swap} expression of the definition of \tcode{is_swappable_with} is known not to throw any exceptions~(\ref{expr.unary.noexcept}). & \tcode{T} and \tcode{U} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_swappable}}% \tcode{template }\br \tcode{struct is_nothrow_swappable;} & For a referenceable type \tcode{T}, the same result as \tcode{is_nothrow_swappable_with_v}, otherwise \tcode{false}. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_destructible}}% \tcode{template }\br \tcode{struct is_nothrow_destructible;} & \tcode{is_destructible_v} is \tcode{true} and the indicated destructor is known not to throw any exceptions~(\ref{expr.unary.noexcept}). & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{has_virtual_destructor}}% \tcode{template }\br \tcode{struct has_virtual_destructor;} & \tcode{T} has a virtual destructor~(\ref{class.dtor}) & If \tcode{T} is a non-union class type, \tcode{T} shall be a complete type. \\ \rowsep \indexlibrary{\idxcode{has_unique_object_representations}}% \tcode{template }\br \tcode{struct has_unique_object_representations;} & For an array type \tcode{T}, the same result as \tcode{has_unique_object_representations_v>}, otherwise \seebelow. & \tcode{T} shall be a complete type, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \end{libreqtab3b} \pnum \begin{example} \begin{codeblock} is_const_v // \tcode{true} is_const_v // \tcode{false} is_const_v // \tcode{false} is_const_v // \tcode{false} is_const_v // \tcode{true} \end{codeblock} \end{example} \pnum \begin{example} \begin{codeblock} remove_const_t // \tcode{volatile int} remove_const_t // \tcode{const int*} remove_const_t // \tcode{const int\&} remove_const_t // \tcode{int[3]} \end{codeblock} \end{example} \pnum \begin{example} \begin{codeblock} // Given: struct P final { }; union U1 { }; union U2 final { }; // the following assertions hold: static_assert(!is_final_v); static_assert(is_final_v

); static_assert(!is_final_v); static_assert(is_final_v); \end{codeblock} \end{example} \indexlibrary{\idxcode{is_constructible}}% \pnum The predicate condition for a template specialization \tcode{is_constructible} shall be satisfied if and only if the following variable definition would be well-formed for some invented variable \tcode{t}: \begin{codeblock} T t(declval()...); \end{codeblock} \begin{note} These tokens are never interpreted as a function declaration. \end{note} Access checking is performed as if in a context unrelated to \tcode{T} and any of the \tcode{Args}. Only the validity of the immediate context of the variable initialization is considered. \begin{note} The evaluation of the initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} \indexlibrary{\idxcode{has_unique_object_representations}}% \pnum The predicate condition for a template specialization \tcode{has_unique_object_representations} shall be satisfied if and only if: \begin{itemize} \item \tcode{T} is trivially copyable, and \item any two objects of type \tcode{T} with the same value have the same object representation, where two objects of array or non-union class type are considered to have the same value if their respective sequences of direct subobjects have the same values, and two objects of union type are considered to have the same value if they have the same active member and the corresponding members have the same value. \end{itemize} The set of scalar types for which this condition holds is \impldef{which scalar types have unique object representations}. \begin{note} If a type has padding bits, the condition does not hold; otherwise, the condition holds true for unsigned integral types. \end{note} \rSec2[meta.unary.prop.query]{Type property queries} \pnum This subclause contains templates that may be used to query properties of types at compile time. \begin{libreqtab2a}{Type property queries}{tab:type-traits.properties.queries} \\ \topline \lhdr{Template} & \rhdr{Value} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Value} \\ \capsep \endhead \indexlibrary{\idxcode{alignment_of}}% \tcode{template \br struct alignment_of;} & \tcode{alignof(T)}.\br \requires{} \tcode{alignof(T)} shall be a valid expression~(\ref{expr.alignof}) \\ \rowsep \indexlibrary{\idxcode{rank}}% \tcode{template \br struct rank;} & If \tcode{T} names an array type, an integer value representing the number of dimensions of \tcode{T}; otherwise, 0. \\ \rowsep \indexlibrary{\idxcode{extent}}% \tcode{template \br struct extent;} & If \tcode{T} is not an array type, or if it has rank less than or equal to \tcode{I}, or if \tcode{I} is 0 and \tcode{T} has type ``array of unknown bound of \tcode{U}'', then 0; otherwise, the bound~(\ref{dcl.array}) of the \tcode{I}'th dimension of \tcode{T}, where indexing of \tcode{I} is zero-based \\ \end{libreqtab2a} \pnum Each of these templates shall be a \tcode{UnaryTypeTrait}~(\ref{meta.rqmts}) with a base characteristic of \tcode{integral_constant}. \pnum \begin{example} \begin{codeblock} // the following assertions hold: assert(rank_v == 0); assert(rank_v == 1); assert(rank_v == 2); \end{codeblock} \end{example} \pnum \begin{example} \begin{codeblock} // the following assertions hold: assert(extent_v == 0); assert(extent_v == 2); assert(extent_v == 2); assert(extent_v == 0); assert((extent_v) == 0); assert((extent_v) == 0); assert((extent_v) == 4); assert((extent_v) == 4); \end{codeblock} \end{example} \rSec2[meta.rel]{Relationships between types} \pnum This subclause contains templates that may be used to query relationships between types at compile time. \pnum Each of these templates shall be a \tcode{BinaryTypeTrait}~(\ref{meta.rqmts}) with a base characteristic of \tcode{true_type} if the corresponding condition is true, otherwise \tcode{false_type}. \begin{libreqtab3f}{Type relationship predicates}{tab:type-traits.relationship} \\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \chdr{Condition} & \rhdr{Comments} \\ \capsep \endhead \tcode{template }\br \tcode{struct is_same;} & \tcode{T} and \tcode{U} name the same type with the same cv-qualifications & \\ \rowsep \indexlibrary{\idxcode{is_base_of}}% \tcode{template }\br \tcode{struct is_base_of;} & \tcode{Base} is a base class of \tcode{Derived} (Clause~\ref{class.derived}) without regard to cv-qualifiers or \tcode{Base} and \tcode{Derived} are not unions and name the same class type without regard to cv-qualifiers & If \tcode{Base} and \tcode{Derived} are non-union class types and are not possibly cv-qualified versions of the same type, \tcode{Derived} shall be a complete type. \begin{note} Base classes that are private, protected, or ambiguous are, nonetheless, base classes. \end{note} \\ \rowsep \indexlibrary{\idxcode{is_convertible}}% \tcode{template }\br \tcode{struct is_convertible;} & \seebelow & \tcode{From} and \tcode{To} shall be complete types, arrays of unknown bound, or \cv{}~\tcode{void} types. \\ \rowsep \indexlibrary{\idxcode{is_invocable}}% \tcode{template }\br \tcode{struct is_invocable;} & The expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)} is well formed when treated as an unevaluated operand & \tcode{Fn} and all types in the parameter pack \tcode{ArgTypes} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_invocable_r}}% \tcode{template }\br \tcode{struct is_invocable_r;} & The expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)} is well formed when treated as an unevaluated operand & \tcode{Fn}, \tcode{R}, and all types in the parameter pack \tcode{ArgTypes} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_invocable}}% \tcode{template }\br \tcode{struct is_nothrow_invocable;} & \tcode{is_invocable_v<}\br\tcode{Fn, ArgTypes...>} is \tcode{true} and the expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)} is known not to throw any exceptions & \tcode{Fn} and all types in the parameter pack \tcode{ArgTypes} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \rowsep \indexlibrary{\idxcode{is_nothrow_invocable_r}}% \tcode{template }\br \tcode{struct is_nothrow_invocable_r;} & \tcode{is_invocable_r_v<}\br\tcode{R, Fn, ArgTypes...>} is \tcode{true} and the expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)} is known not to throw any exceptions & \tcode{Fn}, \tcode{R}, and all types in the parameter pack \tcode{ArgTypes} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound. \\ \end{libreqtab3f} \pnum For the purpose of defining the templates in this subclause, a function call expression \tcode{declval()} for any type \tcode{T} is considered to be a trivial~(\ref{basic.types}, \ref{special}) function call that is not an odr-use~(\ref{basic.def.odr}) of \tcode{declval} in the context of the corresponding definition notwithstanding the restrictions of~\ref{declval}. \pnum \begin{example} \begin{codeblock} struct B {}; struct B1 : B {}; struct B2 : B {}; struct D : private B1, private B2 {}; is_base_of_v // \tcode{true} is_base_of_v // \tcode{true} is_base_of_v // \tcode{true} is_base_of_v // \tcode{true} is_base_of_v // \tcode{false} is_base_of_v // \tcode{false} is_base_of_v // \tcode{false} is_base_of_v // \tcode{false} \end{codeblock} \end{example} \indexlibrary{\idxcode{is_convertible}}% \pnum The predicate condition for a template specialization \tcode{is_convertible} shall be satisfied if and only if the return expression in the following code would be well-formed, including any implicit conversions to the return type of the function: \begin{codeblock} To test() { return declval(); } \end{codeblock} \begin{note} This requirement gives well defined results for reference types, void types, array types, and function types.\end{note} Access checking is performed in a context unrelated to \tcode{To} and \tcode{From}. Only the validity of the immediate context of the \grammarterm{expression} of the \tcode{return} statement (including initialization of the returned object or reference) is considered. \begin{note} The initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} \rSec2[meta.trans]{Transformations between types} \pnum This subclause contains templates that may be used to transform one type to another following some predefined rule. \pnum Each of the templates in this subclause shall be a \tcode{TransformationTrait}~(\ref{meta.rqmts}). \rSec3[meta.trans.cv]{Const-volatile modifications} \begin{libreqtab2a}{Const-volatile modifications}{tab:type-traits.const-volatile} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{remove_const}}% \tcode{template \br struct remove_const;} & The member typedef \tcode{type} names the same type as \tcode{T} except that any top-level const-qualifier has been removed. \begin{example} \tcode{remove_const_t} evaluates to \tcode{volatile int}, whereas \tcode{remove_const_t} evaluates to \tcode{const int*}. \end{example} \\ \rowsep \indexlibrary{\idxcode{remove_volatile}}% \tcode{template \br struct remove_volatile;} & The member typedef \tcode{type} names the same type as \tcode{T} except that any top-level volatile-qualifier has been removed. \begin{example} \tcode{remove_volatile_t} evaluates to \tcode{const int}, whereas \tcode{remove_volatile_t} evaluates to \tcode{volatile int*}. \end{example} \\ \rowsep \indexlibrary{\idxcode{remove_cv}}% \tcode{template \br struct remove_cv;} & The member typedef \tcode{type} shall be the same as \tcode{T} except that any top-level cv-qualifier has been removed. \begin{example} \tcode{remove_cv_t} evaluates to \tcode{int}, whereas \tcode{remove_cv_t} evaluates to \tcode{const volatile int*}. \end{example} \\ \rowsep \indexlibrary{\idxcode{add_const}}% \tcode{template \br struct add_const;} & If \tcode{T} is a reference, function, or top-level const-qualified type, then \tcode{type} names the same type as \tcode{T}, otherwise \tcode{T const}. \\ \rowsep \indexlibrary{\idxcode{add_volatile}}% \tcode{template \br struct add_volatile;} & If \tcode{T} is a reference, function, or top-level volatile-qualified type, then \tcode{type} names the same type as \tcode{T}, otherwise \tcode{T volatile}. \\ \rowsep \indexlibrary{\idxcode{add_cv}}% \tcode{template \br struct add_cv;} & The member typedef \tcode{type} names the same type as \tcode{add_const_t{>}}. \\ \end{libreqtab2a} \rSec3[meta.trans.ref]{Reference modifications} \begin{libreqtab2a}{Reference modifications}{tab:type-traits.reference} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{remove_reference}}% \tcode{template \br struct remove_reference;} & If \tcode{T} has type ``reference to \tcode{T1}'' then the member typedef \tcode{type} names \tcode{T1}; otherwise, \tcode{type} names \tcode{T}.\\ \rowsep \indexlibrary{\idxcode{add_lvalue_reference}}% \tcode{template \br struct add_lvalue_reference;} & If \tcode{T} names a referenceable type (\ref{defns.referenceable}) then the member typedef \tcode{type} names \tcode{T\&}; otherwise, \tcode{type} names \tcode{T}. \begin{note} This rule reflects the semantics of reference collapsing~(\ref{dcl.ref}). \end{note}\\ \rowsep \indexlibrary{\idxcode{add_rvalue_reference}}% \tcode{template }\br \tcode{struct add_rvalue_reference;} & If \tcode{T} names a referenceable type then the member typedef \tcode{type} names \tcode{T\&\&}; otherwise, \tcode{type} names \tcode{T}. \begin{note} This rule reflects the semantics of reference collapsing~(\ref{dcl.ref}). For example, when a type \tcode{T} names a type \tcode{T1\&}, the type \tcode{add_rvalue_reference_t} is not an rvalue reference. \end{note} \\ \end{libreqtab2a} \rSec3[meta.trans.sign]{Sign modifications} \begin{libreqtab2a}{Sign modifications}{tab:type-traits.sign} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{make_signed}}% \tcode{template }\br \tcode{struct make_signed;} & If \tcode{T} names a (possibly cv-qualified) signed integer type~(\ref{basic.fundamental}) then the member typedef \tcode{type} names the type \tcode{T}; otherwise, if \tcode{T} names a (possibly cv-qualified) unsigned integer type then \tcode{type} names the corresponding signed integer type, with the same cv-qualifiers as \tcode{T}; otherwise, \tcode{type} names the signed integer type with smallest rank~(\ref{conv.rank}) for which \tcode{sizeof(T) == sizeof(type)}, with the same cv-qualifiers as \tcode{T}.\br \requires{} \tcode{T} shall be a (possibly cv-qualified) integral type or enumeration but not a \tcode{bool} type.\\ \rowsep \indexlibrary{\idxcode{make_unsigned}}% \tcode{template }\br \tcode{struct make_unsigned;} & If \tcode{T} names a (possibly cv-qualified) unsigned integer type~(\ref{basic.fundamental}) then the member typedef \tcode{type} names the type \tcode{T}; otherwise, if \tcode{T} names a (possibly cv-qualified) signed integer type then \tcode{type} names the corresponding unsigned integer type, with the same cv-qualifiers as \tcode{T}; otherwise, \tcode{type} names the unsigned integer type with smallest rank~(\ref{conv.rank}) for which \tcode{sizeof(T) == sizeof(type)}, with the same cv-qualifiers as \tcode{T}.\br \requires{} \tcode{T} shall be a (possibly cv-qualified) integral type or enumeration but not a \tcode{bool} type.\\ \end{libreqtab2a} \clearpage \rSec3[meta.trans.arr]{Array modifications} \begin{libreqtab2a}{Array modifications}{tab:type-traits.array} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{remove_extent}}% \tcode{template \br struct remove_extent;} & If \tcode{T} names a type ``array of \tcode{U}'', the member typedef \tcode{type} shall be \tcode{U}, otherwise \tcode{T}. \begin{note} For multidimensional arrays, only the first array dimension is removed. For a type ``array of \tcode{const U}'', the resulting type is \tcode{const U}. \end{note} \\ \rowsep \indexlibrary{\idxcode{remove_all_extents}}% \tcode{template \br struct remove_all_extents;} & If \tcode{T} is ``multi-dimensional array of \tcode{U}'', the resulting member typedef \tcode{type} is \tcode{U}, otherwise \tcode{T}. \\ \end{libreqtab2a} \pnum \begin{example} \begin{codeblock} // the following assertions hold: assert((is_same_v, int>)); assert((is_same_v, int>)); assert((is_same_v, int[3]>)); assert((is_same_v, int[3]>)); \end{codeblock} \end{example} \pnum \begin{example} \begin{codeblock} // the following assertions hold: assert((is_same_v, int>)); assert((is_same_v, int>)); assert((is_same_v, int>)); assert((is_same_v, int>)); \end{codeblock} \end{example} \rSec3[meta.trans.ptr]{Pointer modifications} \begin{libreqtab2a}{Pointer modifications}{tab:type-traits.pointer} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{remove_pointer}}% \tcode{template \br struct remove_pointer;} & If \tcode{T} has type ``(possibly cv-qualified) pointer to \tcode{T1}'' then the member typedef \tcode{type} names \tcode{T1}; otherwise, it names \tcode{T}.\\ \rowsep \indexlibrary{\idxcode{add_pointer}}% \tcode{template \br struct add_pointer;} & If \tcode{T} names a referenceable type (\ref{defns.referenceable}) or a \cv{}~\tcode{void} type then the member typedef \tcode{type} names the same type as \tcode{remove_reference_t*}; otherwise, \tcode{type} names \tcode{T}. \\ \end{libreqtab2a} \clearpage \rSec3[meta.trans.other]{Other transformations} \begin{libreqtab2a}{Other transformations}{tab:type-traits.other} \\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endfirsthead \continuedcaption\\ \topline \lhdr{Template} & \rhdr{Comments} \\ \capsep \endhead \indexlibrary{\idxcode{aligned_storage}}% \tcode{template \br struct aligned_storage;} & The value of \textit{default-alignment} shall be the most stringent alignment requirement for any \Cpp object type whose size is no greater than \tcode{Len}~(\ref{basic.types}). The member typedef \tcode{type} shall be a POD type suitable for use as uninitialized storage for any object whose size is at most \tcode{Len} and whose alignment is a divisor of \tcode{Align}.\br \requires{} \tcode{Len} shall not be zero. \tcode{Align} shall be equal to \tcode{alignof(T)} for some type \tcode{T} or to \textit{default-alignment}.\\ \rowsep \indexlibrary{\idxcode{aligned_union}}% \tcode{template \br struct aligned_union;} & The member typedef \tcode{type} shall be a POD type suitable for use as uninitialized storage for any object whose type is listed in \tcode{Types}; its size shall be at least \tcode{Len}. The static member \tcode{alignment_value} shall be an integral constant of type \tcode{size_t} whose value is the strictest alignment of all types listed in \tcode{Types}.\br \requires{} At least one type is provided. \\ \rowsep \indexlibrary{\idxcode{decay}}% \tcode{template \br struct decay;} & Let \tcode{U} be \tcode{remove_reference_t}. If \tcode{is_array_v} is \tcode{true}, the member typedef \tcode{type} shall equal \tcode{remove_extent_t*}. If \tcode{is_function_v} is \tcode{true}, the member typedef \tcode{type} shall equal \tcode{add_pointer_t}. Otherwise the member typedef \tcode{type} equals \tcode{remove_cv_t}. \begin{note} This behavior is similar to the lvalue-to-rvalue~(\ref{conv.lval}), array-to-pointer~(\ref{conv.array}), and function-to-pointer~(\ref{conv.func}) conversions applied when an lvalue expression is used as an rvalue, but also strips \cv-qualifiers from class types in order to more closely model by-value argument passing. \end{note} \\ \rowsep \indexlibrary{\idxcode{enable_if}}% \tcode{template } \tcode{struct enable_if;} & If \tcode{B} is \tcode{true}, the member typedef \tcode{type} shall equal \tcode{T}; otherwise, there shall be no member \tcode{type}. \\ \rowsep \tcode{template }\br \tcode{struct conditional;} & If \tcode{B} is \tcode{true}, the member typedef \tcode{type} shall equal \tcode{T}. If \tcode{B} is \tcode{false}, the member typedef \tcode{type} shall equal \tcode{F}. \\ \rowsep \tcode{template } \tcode{struct common_type;} & Unless this trait is specialized (as specified in Note B, below), the member \tcode{type} shall be defined or omitted as specified in Note A, below. If it is omitted, there shall be no member \tcode{type}. Each type in the parameter pack \tcode{T} shall be complete, \cv{}~\tcode{void}, or an array of unknown bound. \\ \rowsep \indexlibrary{\idxcode{underlying_type}}% \tcode{template }\br \tcode{struct underlying_type;} & The member typedef \tcode{type} names the underlying type of \tcode{T}.\br \requires{} \tcode{T} shall be a complete enumeration type~(\ref{dcl.enum}) \\ \rowsep \tcode{template }\br \tcode{struct invoke_result;} & If the expression \tcode{\placeholdernc{INVOKE}(declval(), declval()...)} is well formed when treated as an unevaluated operand (Clause~\ref{expr}), the member typedef \tcode{type} names the type \tcode{decltype(\placeholdernc{INVOKE}(declval(), declval()...))}; otherwise, there shall be no member \tcode{type}. Access checking is performed as if in a context unrelated to \tcode{Fn} and \tcode{ArgTypes}. Only the validity of the immediate context of the expression is considered. \begin{note} The compilation of the expression can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the ``immediate context'' and can result in the program being ill-formed. \end{note} \br \requires{} \tcode{Fn} and all types in the parameter pack \tcode{ArgTypes} shall be complete types, \cv{}~\tcode{void}, or arrays of unknown bound.\\ \end{libreqtab2a} \indexlibrary{\idxcode{aligned_storage}}% \pnum \begin{note} A typical implementation would define \tcode{aligned_storage} as: \begin{codeblock} template struct aligned_storage { typedef struct { alignas(Alignment) unsigned char __data[Len]; } type; }; \end{codeblock} \end{note} \pnum It is \impldef{support for extended alignment} whether any extended alignment is supported~(\ref{basic.align}). \indexlibrary{\idxcode{common_type}}% \pnum Note A: For the \tcode{common_type} trait applied to a parameter pack \tcode{T} of types, the member \tcode{type} shall be either defined or not present as follows: \begin{itemize} \item If \tcode{sizeof...(T)} is zero, there shall be no member \tcode{type}. \item If \tcode{sizeof...(T)} is one, let \tcode{T0} denote the sole type constituting the pack \tcode{T}. The member \grammarterm{typedef-name} \tcode{type} shall denote the same type, if any, as \tcode{common_type_t}; otherwise there shall be no member \tcode{type}. \item If \tcode{sizeof...(T)} is two, let the first and second types constituting \tcode{T} be denoted by \tcode{T1} and \tcode{T2}, respectively, and let \tcode{D1} and \tcode{D2} denote the same types as \tcode{decay_t} and \tcode{decay_t}, respectively. \begin{itemize} \item If \tcode{is_same_v} is \tcode{false} or \tcode{is_same_v} is \tcode{false}, let \tcode{C} denote the same type, if any, as \tcode{common_type_t}. \item Otherwise, let \tcode{C} denote the same type, if any, as \begin{codeblock} decay_t() : declval())> \end{codeblock} \begin{note} This will not apply if there is a specialization \tcode{common_type}. \end{note} \end{itemize} In either case, the member \grammarterm{typedef-name} \tcode{type} shall denote the same type, if any, as \tcode{C}. Otherwise, there shall be no member \tcode{type}. \item If \tcode{sizeof...(T)} is greater than two, let \tcode{T1}, \tcode{T2}, and \tcode{R}, respectively, denote the first, second, and (pack of) remaining types constituting \tcode{T}. Let \tcode{C} denote the same type, if any, as \tcode{common_type_t}. If there is such a type \tcode{C}, the member \grammarterm{typedef-name} \tcode{type} shall denote the same type, if any, as \tcode{common_type_t}. Otherwise, there shall be no member \tcode{type}. \end{itemize} \pnum Note B: Notwithstanding the provisions of \ref{meta.type.synop}, and pursuant to \ref{namespace.std}, a program may specialize \tcode{common_type} for types \tcode{T1} and \tcode{T2} such that \tcode{is_same_v>} and \tcode{is_same_v>} are each \tcode{true}. \begin{note} Such specializations are needed when only explicit conversions are desired between the template arguments. \end{note} Such a specialization need not have a member named \tcode{type}, but if it does, that member shall be a \grammarterm{typedef-name} for an accessible and unambiguous cv-unqualified non-reference type \tcode{C} to which each of the types \tcode{T1} and \tcode{T2} is explicitly convertible. Moreover, \tcode{common_type_t} shall denote the same type, if any, as does \tcode{common_type_t}. No diagnostic is required for a violation of this Note's rules. \pnum \begin{example} Given these definitions: \begin{codeblock} using PF1 = bool (&)(); using PF2 = short (*)(long); struct S { operator PF2() const; double operator()(char, int&); void fn(long) const; char data; }; using PMF = void (S::*)(long) const; using PMD = char S::*; \end{codeblock} the following assertions will hold: \begin{codeblock} static_assert(is_same_v, short>); static_assert(is_same_v, double>); static_assert(is_same_v, bool>); static_assert(is_same_v, int>, void>); static_assert(is_same_v, char&&>); static_assert(is_same_v, const char&>); \end{codeblock} \end{example} \rSec2[meta.logical]{Logical operator traits} \pnum This subclause describes type traits for applying logical operators to other type traits. \indexlibrary{\idxcode{conjunction}}% \begin{itemdecl} template struct conjunction : @\seebelow@ { }; \end{itemdecl} \begin{itemdescr} \pnum The class template \tcode{conjunction} forms the logical conjunction of its template type arguments. \pnum For a specialization \tcode{conjunction}, if there is a template type argument \tcode{Bi} for which \tcode{bool(Bi::value)} is \tcode{false}, then instantiating \tcode{conjunction::value} does not require the instantiation of \tcode{Bj::value} for \tcode{j > i}. \begin{note} This is analogous to the short-circuiting behavior of the built-in operator \tcode{\&\&}. \end{note} \pnum Every template type argument for which \tcode{Bi::value} is instantiated shall be usable as a base class and shall have a member \tcode{value} which is convertible to \tcode{bool}, is not hidden, and is unambiguously available in the type. \pnum The specialization \tcode{conjunction} has a public and unambiguous base that is either \begin{itemize} \item the first type \tcode{Bi} in the list \tcode{true_type, B1, ..., BN} for which \tcode{bool(Bi::value)} is \tcode{false}, or \item if there is no such \tcode{Bi}, the last type in the list. \end{itemize} \begin{note} This means a specialization of \tcode{conjunction} does not necessarily inherit from either \tcode{true_type} or \tcode{false_type}. \end{note} \pnum The member names of the base class, other than \tcode{conjunction} and \tcode{operator=}, shall not be hidden and shall be unambiguously available in \tcode{conjunction}. \end{itemdescr} \indexlibrary{\idxcode{disjunction}}% \begin{itemdecl} template struct disjunction : @\seebelow@ { }; \end{itemdecl} \begin{itemdescr} \pnum The class template \tcode{disjunction} forms the logical disjunction of its template type arguments. \pnum For a specialization \tcode{disjunction}, if there is a template type argument \tcode{Bi} for which \tcode{bool(Bi::value)} is \tcode{true}, then instantiating \tcode{disjunction::value} does not require the instantiation of \tcode{Bj::value} for \tcode{j > i}. \begin{note} This is analogous to the short-circuiting behavior of the built-in operator \tcode{||}. \end{note} \pnum Every template type argument for which \tcode{Bi::value} is instantiated shall be usable as a base class and shall have a member \tcode{value} which is convertible to \tcode{bool}, is not hidden, and is unambiguously available in the type. \pnum The specialization \tcode{disjunction} has a public and unambiguous base that is either \begin{itemize} \item the first type \tcode{Bi} in the list \tcode{false_type, B1, ..., BN} for which \tcode{bool(Bi::value)} is \tcode{true}, or \item if there is no such \tcode{Bi}, the last type in the list. \end{itemize} \begin{note} This means a specialization of \tcode{disjunction} does not necessarily inherit from either \tcode{true_type} or \tcode{false_type}. \end{note} \pnum The member names of the base class, other than \tcode{disjunction} and \tcode{operator=}, shall not be hidden and shall be unambiguously available in \tcode{disjunction}. \end{itemdescr} \indexlibrary{\idxcode{negation}}% \begin{itemdecl} template struct negation : @\seebelow@ { }; \end{itemdecl} \begin{itemdescr} \pnum The class template \tcode{negation} forms the logical negation of its template type argument. The type \tcode{negation} is a \tcode{UnaryTypeTrait} with a base characteristic of \tcode{bool_constant}. \end{itemdescr} \rSec1[ratio]{Compile-time rational arithmetic} \rSec2[ratio.general]{In general} \pnum \indexlibrary{\idxcode{ratio}}% This subclause describes the ratio library. It provides a class template \tcode{ratio} which exactly represents any finite rational number with a numerator and denominator representable by compile-time constants of type \tcode{intmax_t}. \pnum Throughout this subclause, the names of template parameters are used to express type requirements. If a template parameter is named \tcode{R1} or \tcode{R2}, and the template argument is not a specialization of the \tcode{ratio} template, the program is ill-formed. \rSec2[ratio.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{ratio}}% \indexlibrary{\idxhdr{ratio}}% \begin{codeblockdigitsep} namespace std { // \ref{ratio.ratio}, class template \tcode{ratio} template class ratio; // \ref{ratio.arithmetic}, ratio arithmetic template using ratio_add = @\seebelow@; template using ratio_subtract = @\seebelow@; template using ratio_multiply = @\seebelow@; template using ratio_divide = @\seebelow@; // \ref{ratio.comparison}, ratio comparison template struct ratio_equal; template struct ratio_not_equal; template struct ratio_less; template struct ratio_less_equal; template struct ratio_greater; template struct ratio_greater_equal; template inline constexpr bool ratio_equal_v = ratio_equal::value; template inline constexpr bool ratio_not_equal_v = ratio_not_equal::value; template inline constexpr bool ratio_less_v = ratio_less::value; template inline constexpr bool ratio_less_equal_v = ratio_less_equal::value; template inline constexpr bool ratio_greater_v = ratio_greater::value; template inline constexpr bool ratio_greater_equal_v = ratio_greater_equal::value; // \ref{ratio.si}, convenience SI typedefs using yocto = ratio<1, 1'000'000'000'000'000'000'000'000>; // see below using zepto = ratio<1, 1'000'000'000'000'000'000'000>; // see below using atto = ratio<1, 1'000'000'000'000'000'000>; using femto = ratio<1, 1'000'000'000'000'000>; using pico = ratio<1, 1'000'000'000'000>; using nano = ratio<1, 1'000'000'000>; using micro = ratio<1, 1'000'000>; using milli = ratio<1, 1'000>; using centi = ratio<1, 100>; using deci = ratio<1, 10>; using deca = ratio< 10, 1>; using hecto = ratio< 100, 1>; using kilo = ratio< 1'000, 1>; using mega = ratio< 1'000'000, 1>; using giga = ratio< 1'000'000'000, 1>; using tera = ratio< 1'000'000'000'000, 1>; using peta = ratio< 1'000'000'000'000'000, 1>; using exa = ratio< 1'000'000'000'000'000'000, 1>; using zetta = ratio< 1'000'000'000'000'000'000'000, 1>; // see below using yotta = ratio<1'000'000'000'000'000'000'000'000, 1>; // see below } \end{codeblockdigitsep} \rSec2[ratio.ratio]{Class template \tcode{ratio}} \indexlibrary{\idxcode{ratio}}% \begin{codeblock} namespace std { template class ratio { public: static constexpr intmax_t num; static constexpr intmax_t den; using type = ratio; }; } \end{codeblock} \pnum \indextext{signed integer representation!two's complement}% If the template argument \tcode{D} is zero or the absolute values of either of the template arguments \tcode{N} and \tcode{D} is not representable by type \tcode{intmax_t}, the program is ill-formed. \begin{note} These rules ensure that infinite ratios are avoided and that for any negative input, there exists a representable value of its absolute value which is positive. In a two's complement representation, this excludes the most negative value. \end{note} \pnum The static data members \tcode{num} and \tcode{den} shall have the following values, where \tcode{gcd} represents the greatest common divisor of the absolute values of \tcode{N} and \tcode{D}: \begin{itemize} \item \tcode{num} shall have the value \tcode{sign(N) * sign(D) * abs(N) / gcd}. \item \tcode{den} shall have the value \tcode{abs(D) / gcd}. \end{itemize} \rSec2[ratio.arithmetic]{Arithmetic on \tcode{ratio}{s}} \pnum Each of the alias templates \tcode{ratio_add}, \tcode{ratio_subtract}, \tcode{ratio_multiply}, and \tcode{ratio_divide} denotes the result of an arithmetic computation on two \tcode{ratio}{s} \tcode{R1} and \tcode{R2}. With \tcode{X} and \tcode{Y} computed (in the absence of arithmetic overflow) as specified by Table~\ref{tab:ratio.arithmetic}, each alias denotes a \tcode{ratio} such that \tcode{U} is the same as \tcode{ratio::num} and \tcode{V} is the same as \tcode{ratio::den}. \pnum If it is not possible to represent \tcode{U} or \tcode{V} with \tcode{intmax_t}, the program is ill-formed. Otherwise, an implementation should yield correct values of \tcode{U} and \tcode{V}. If it is not possible to represent \tcode{X} or \tcode{Y} with \tcode{intmax_t}, the program is ill-formed unless the implementation yields correct values of \tcode{U} and \tcode{V}. \begin{floattable}{Expressions used to perform ratio arithmetic}{tab:ratio.arithmetic} {lll} \topline \lhdr{Type} & \chdr{Value of \tcode{X}} & \rhdr{Value of \tcode{Y}} \\ \rowsep \tcode{ratio_add} & \tcode{R1::num * R2::den +} & \tcode{R1::den * R2::den} \\ & \tcode{R2::num * R1::den} & \\ \rowsep \tcode{ratio_subtract} & \tcode{R1::num * R2::den -} & \tcode{R1::den * R2::den} \\ & \tcode{R2::num * R1::den} & \\ \rowsep \tcode{ratio_multiply} & \tcode{R1::num * R2::num} & \tcode{R1::den * R2::den} \\ \rowsep \tcode{ratio_divide} & \tcode{R1::num * R2::den} & \tcode{R1::den * R2::num} \\ \end{floattable} \pnum \begin{example} \begin{codeblock} static_assert(ratio_add, ratio<1, 6>>::num == 1, "1/3+1/6 == 1/2"); static_assert(ratio_add, ratio<1, 6>>::den == 2, "1/3+1/6 == 1/2"); static_assert(ratio_multiply, ratio<3, 2>>::num == 1, "1/3*3/2 == 1/2"); static_assert(ratio_multiply, ratio<3, 2>>::den == 2, "1/3*3/2 == 1/2"); // The following cases may cause the program to be ill-formed under some implementations static_assert(ratio_add, ratio<1, INT_MAX>>::num == 2, "1/MAX+1/MAX == 2/MAX"); static_assert(ratio_add, ratio<1, INT_MAX>>::den == INT_MAX, "1/MAX+1/MAX == 2/MAX"); static_assert(ratio_multiply, ratio>::num == 1, "1/MAX * MAX/2 == 1/2"); static_assert(ratio_multiply, ratio>::den == 2, "1/MAX * MAX/2 == 1/2"); \end{codeblock} \end{example} \rSec2[ratio.comparison]{Comparison of \tcode{ratio}{s}} \indexlibrary{\idxcode{ratio_equal}}% \begin{itemdecl} template struct ratio_equal : bool_constant { }; \end{itemdecl} \indexlibrary{\idxcode{ratio_not_equal}}% \begin{itemdecl} template struct ratio_not_equal : bool_constant> { }; \end{itemdecl} \indexlibrary{\idxcode{ratio_less}}% \begin{itemdecl} template struct ratio_less : bool_constant<@\seebelow@> { }; \end{itemdecl} \begin{itemdescr} \pnum If \tcode{R1::num} $\times$ \tcode{R2::den} is less than \tcode{R2::num} $\times$ \tcode{R1::den}, \tcode{ratio_less} shall be derived from \tcode{bool_constant}; otherwise it shall be derived from \tcode{bool_constant}. Implementations may use other algorithms to compute this relationship to avoid overflow. If overflow occurs, the program is ill-formed. \end{itemdescr} \indexlibrary{\idxcode{ratio_less_equal}}% \begin{itemdecl} template struct ratio_less_equal : bool_constant> { }; \end{itemdecl} \indexlibrary{\idxcode{ratio_greater}}% \begin{itemdecl} template struct ratio_greater : bool_constant> { }; \end{itemdecl} \indexlibrary{\idxcode{ratio_greater_equal}}% \begin{itemdecl} template struct ratio_greater_equal : bool_constant> { }; \end{itemdecl} \rSec2[ratio.si]{SI types for \tcode{ratio}} \pnum For each of the \grammarterm{typedef-name}{s} \tcode{yocto}, \tcode{zepto}, \tcode{zetta}, and \tcode{yotta}, if both of the constants used in its specification are representable by \tcode{intmax_t}, the typedef shall be defined; if either of the constants is not representable by \tcode{intmax_t}, the typedef shall not be defined. \rSec1[time]{Time utilities} \rSec2[time.general]{In general} \pnum \indexlibrary{\idxcode{chrono}}% This subclause describes the chrono library~(\ref{time.syn}) and various C functions~(\ref{ctime.syn}) that provide generally useful time utilities. \rSec2[time.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{chrono}}% \indexlibrary{\idxhdr{chrono}}% \begin{codeblock} namespace std { namespace chrono { // \ref{time.duration}, class template \tcode{duration} template > class duration; // \ref{time.point}, class template \tcode{time_point} template class time_point; } // \ref{time.traits.specializations}, \tcode{common_type} specializations template struct common_type, chrono::duration>; template struct common_type, chrono::time_point>; namespace chrono { // \ref{time.traits}, customization traits template struct treat_as_floating_point; template struct duration_values; template inline constexpr bool treat_as_floating_point_v = treat_as_floating_point::value; // \ref{time.duration.nonmember}, \tcode{duration} arithmetic template common_type_t, duration> constexpr operator+(const duration& lhs, const duration& rhs); template common_type_t, duration> constexpr operator-(const duration& lhs, const duration& rhs); template duration, Period> constexpr operator*(const duration& d, const Rep2& s); template duration, Period> constexpr operator*(const Rep1& s, const duration& d); template duration, Period> constexpr operator/(const duration& d, const Rep2& s); template common_type_t constexpr operator/(const duration& lhs, const duration& rhs); template duration, Period> constexpr operator%(const duration& d, const Rep2& s); template common_type_t, duration> constexpr operator%(const duration& lhs, const duration& rhs); // \ref{time.duration.comparisons}, \tcode{duration} comparisons template constexpr bool operator==(const duration& lhs, const duration& rhs); template constexpr bool operator!=(const duration& lhs, const duration& rhs); template constexpr bool operator< (const duration& lhs, const duration& rhs); template constexpr bool operator<=(const duration& lhs, const duration& rhs); template constexpr bool operator> (const duration& lhs, const duration& rhs); template constexpr bool operator>=(const duration& lhs, const duration& rhs); // \ref{time.duration.cast}, \tcode{duration_cast} template constexpr ToDuration duration_cast(const duration& d); template constexpr ToDuration floor(const duration& d); template constexpr ToDuration ceil(const duration& d); template constexpr ToDuration round(const duration& d); // convenience typedefs using nanoseconds = duration<@\term{signed integer type of at least 64 bits}@, nano>; using microseconds = duration<@\term{signed integer type of at least 55 bits}@, micro>; using milliseconds = duration<@\term{signed integer type of at least 45 bits}@, milli>; using seconds = duration<@\term{signed integer type of at least 35 bits}@>; using minutes = duration<@\term{signed integer type of at least 29 bits}@, ratio< 60>>; using hours = duration<@\term{signed integer type of at least 23 bits}@, ratio<3600>>; // \ref{time.point.nonmember}, \tcode{time_point} arithmetic template constexpr time_point>> operator+(const time_point& lhs, const duration& rhs); template constexpr time_point, Duration2>> operator+(const duration& lhs, const time_point& rhs); template constexpr time_point>> operator-(const time_point& lhs, const duration& rhs); template constexpr common_type_t operator-(const time_point& lhs, const time_point& rhs); // \ref{time.point.comparisons}, \tcode{time_point} comparisons template constexpr bool operator==(const time_point& lhs, const time_point& rhs); template constexpr bool operator!=(const time_point& lhs, const time_point& rhs); template constexpr bool operator< (const time_point& lhs, const time_point& rhs); template constexpr bool operator<=(const time_point& lhs, const time_point& rhs); template constexpr bool operator> (const time_point& lhs, const time_point& rhs); template constexpr bool operator>=(const time_point& lhs, const time_point& rhs); // \ref{time.point.cast}, \tcode{time_point_cast} template constexpr time_point time_point_cast(const time_point& t); template constexpr time_point floor(const time_point& tp); template constexpr time_point ceil(const time_point& tp); template constexpr time_point round(const time_point& tp); // \ref{time.duration.alg}, specialized algorithms template constexpr duration abs(duration d); // \ref{time.clock}, clocks class system_clock; class steady_clock; class high_resolution_clock; } inline namespace literals { inline namespace chrono_literals { // \ref{time.duration.literals}, suffixes for duration literals constexpr chrono::hours operator""h(unsigned long long); constexpr chrono::duration<@\unspec,@ ratio<3600,1>> operator""h(long double); constexpr chrono::minutes operator""min(unsigned long long); constexpr chrono::duration<@\unspec,@ ratio<60,1>> operator""min(long double); constexpr chrono::seconds operator""s(unsigned long long); constexpr chrono::duration<@\unspec@> @\itcorr[-1]@ operator""s(long double); constexpr chrono::milliseconds operator""ms(unsigned long long); constexpr chrono::duration<@\unspec,@ milli> operator""ms(long double); constexpr chrono::microseconds operator""us(unsigned long long); constexpr chrono::duration<@\unspec,@ micro> operator""us(long double); constexpr chrono::nanoseconds operator""ns(unsigned long long); constexpr chrono::duration<@\unspec,@ nano> operator""ns(long double); } } namespace chrono { using namespace literals::chrono_literals; } } \end{codeblock} \rSec2[time.clock.req]{Clock requirements} \pnum A clock is a bundle consisting of a \tcode{duration}, a \tcode{time_point}, and a function \tcode{now()} to get the current \tcode{time_point}. The origin of the clock's \tcode{time_point} is referred to as the clock's \defn{epoch}. A clock shall meet the requirements in Table~\ref{tab:time.clock}. \pnum In Table~\ref{tab:time.clock} \tcode{C1} and \tcode{C2} denote clock types. \tcode{t1} and \tcode{t2} are values returned by \tcode{C1::now()} where the call returning \tcode{t1} happens before~(\ref{intro.multithread}) the call returning \tcode{t2} and both of these calls occur before \tcode{C1::time_point::max()}. \begin{note} This means \tcode{C1} did not wrap around between \tcode{t1} and \tcode{t2}. \end{note} \begin{libreqtab3a} {Clock requirements} {tab:time.clock} \\ \topline \lhdr{Expression} & \chdr{Return type} & \rhdr{Operational semantics} \\ \capsep \endfirsthead \continuedcaption\\ \hline \lhdr{Expression} & \chdr{Return type} & \rhdr{Operational semantics} \\ \capsep \endhead \tcode{C1::rep} & An arithmetic type or a class emulating an arithmetic type & The representation type of \tcode{C1::duration}. \\ \rowsep \tcode{C1::period} & a specialization of \tcode{ratio} & The tick period of the clock in seconds. \\ \rowsep \tcode{C1::duration} & \tcode{chrono::duration} & The \tcode{duration} type of the clock. \\ \rowsep \tcode{C1::time_point} & \tcode{chrono::time_point} or \tcode{chrono::time_point} & The \tcode{time_point} type of the clock. \tcode{C1} and \tcode{C2} shall refer to the same epoch. \\ \rowsep \tcode{C1::is_steady} & \tcode{const bool} & \tcode{true} if \tcode{t1 <= t2} is always \tcode{true} and the time between clock ticks is constant, otherwise \tcode{false}. \\ \rowsep \tcode{C1::now()} & \tcode{C1::time_point} & Returns a \tcode{time_point} object representing the current point in time. \\ \end{libreqtab3a} \pnum \begin{note} The relative difference in durations between those reported by a given clock and the SI definition is a measure of the quality of implementation. \end{note} \pnum A type \tcode{TC} meets the \tcode{TrivialClock} requirements if: \begin{itemize} \item \tcode{TC} satisfies the \tcode{Clock} requirements~(\ref{time.clock.req}), \item the types \tcode{TC::rep}, \tcode{TC::duration}, and \tcode{TC::time_point} satisfy the requirements of \tcode{EqualityCom\-parable} (Table~\ref{tab:equalitycomparable}), \tcode{LessThanComparable} (Table~\ref{tab:lessthancomparable}), \tcode{DefaultConstructible} (Table~\ref{tab:defaultconstructible}), \tcode{CopyCon\-structible} (Table~\ref{tab:copyconstructible}), \tcode{CopyAssignable} (Table~\ref{tab:copyassignable}), \tcode{Destructible} (Table~\ref{tab:destructible}), and the requirements of numeric types~(\ref{numeric.requirements}). \begin{note} This means, in particular, that operations on these types will not throw exceptions. \end{note} \item lvalues of the types \tcode{TC::rep}, \tcode{TC::duration}, and \tcode{TC::time_point} are swappable~(\ref{swappable.requirements}), \item the function \tcode{TC::now()} does not throw exceptions, and \item the type \tcode{TC::time_point::clock} meets the \tcode{TrivialClock} requirements, recursively. \end{itemize} \rSec2[time.traits]{Time-related traits} \rSec3[time.traits.is_fp]{\tcode{treat_as_floating_point}} \indexlibrary{\idxcode{treat_as_floating_point}}% \begin{itemdecl} template struct treat_as_floating_point : is_floating_point { }; \end{itemdecl} \pnum The \tcode{duration} template uses the \tcode{treat_as_floating_point} trait to help determine if a \tcode{duration} object can be converted to another \tcode{duration} with a different tick \tcode{period}. If \tcode{treat_as_floating_point_v} is \tcode{true}, then implicit conversions are allowed among \tcode{duration}s. Otherwise, the implicit convertibility depends on the tick \tcode{period}s of the \tcode{duration}s. \begin{note} The intention of this trait is to indicate whether a given class behaves like a floating-point type, and thus allows division of one value by another with acceptable loss of precision. If \tcode{treat_as_floating_point_v} is \tcode{false}, \tcode{Rep} will be treated as if it behaved like an integral type for the purpose of these conversions. \end{note} \rSec3[time.traits.duration_values]{\tcode{duration_values}} \indexlibrary{\idxcode{duration_values}}% \begin{itemdecl} template struct duration_values { public: static constexpr Rep zero(); static constexpr Rep min(); static constexpr Rep max(); }; \end{itemdecl} \pnum The \tcode{duration} template uses the \tcode{duration_values} trait to construct special values of the durations representation (\tcode{Rep}). This is done because the representation might be a class type with behavior which requires some other implementation to return these special values. In that case, the author of that class type should specialize \tcode{duration_values} to return the indicated values. \indexlibrarymember{zero}{duration_values}% \begin{itemdecl} static constexpr Rep zero(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{Rep(0)}. \begin{note} \tcode{Rep(0)} is specified instead of \tcode{Rep()} because \tcode{Rep()} may have some other meaning, such as an uninitialized value. \end{note} \pnum \remarks The value returned shall be the additive identity. \end{itemdescr} \indexlibrarymember{min}{duration_values}% \begin{itemdecl} static constexpr Rep min(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{numeric_limits::lowest()}. \pnum \remarks The value returned shall compare less than or equal to \tcode{zero()}. \end{itemdescr} \indexlibrarymember{max}{duration_values}% \begin{itemdecl} static constexpr Rep max(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{numeric_limits::max()}. \pnum \remarks The value returned shall compare greater than \tcode{zero()}. \end{itemdescr} \rSec3[time.traits.specializations]{Specializations of \tcode{common_type}} \indexlibrary{\idxcode{common_type}}% \begin{itemdecl} template struct common_type, chrono::duration> { using type = chrono::duration, @\seebelow@>; }; \end{itemdecl} \pnum The \tcode{period} of the \tcode{duration} indicated by this specialization of \tcode{common_type} shall be the greatest common divisor of \tcode{Period1} and \tcode{Period2}. \begin{note} This can be computed by forming a ratio of the greatest common divisor of \tcode{Period1::num} and \tcode{Period2::num} and the least common multiple of \tcode{Period1::den} and \tcode{Period2::den}. \end{note} \pnum \begin{note} The \tcode{typedef} name \tcode{type} is a synonym for the \tcode{duration} with the largest tick \tcode{period} possible where both \tcode{duration} arguments will convert to it without requiring a division operation. The representation of this type is intended to be able to hold any value resulting from this conversion with no truncation error, although floating-point durations may have round-off errors. \end{note} \indexlibrary{\idxcode{common_type}}% \begin{itemdecl} template struct common_type, chrono::time_point> { using type = chrono::time_point>; }; \end{itemdecl} \pnum The common type of two \tcode{time_point} types is a \tcode{time_point} with the same clock as the two types and the common type of their two \tcode{duration}s. \rSec2[time.duration]{Class template \tcode{duration}} \pnum A \tcode{duration} type measures time between two points in time (\tcode{time_point}s). A \tcode{duration} has a representation which holds a count of ticks and a tick period. The tick period is the amount of time which occurs from one tick to the next, in units of seconds. It is expressed as a rational constant using the template \tcode{ratio}. \indexlibrary{\idxcode{duration}}% \begin{codeblock} template > class duration { public: using rep = Rep; using period = typename Period::type; private: rep rep_; // \expos public: // \ref{time.duration.cons}, construct/copy/destroy constexpr duration() = default; template constexpr explicit duration(const Rep2& r); template constexpr duration(const duration& d); ~duration() = default; duration(const duration&) = default; duration& operator=(const duration&) = default; // \ref{time.duration.observer}, observer constexpr rep count() const; // \ref{time.duration.arithmetic}, arithmetic constexpr common_type_t operator+() const; constexpr common_type_t operator-() const; constexpr duration& operator++(); constexpr duration operator++(int); constexpr duration& operator--(); constexpr duration operator--(int); constexpr duration& operator+=(const duration& d); constexpr duration& operator-=(const duration& d); constexpr duration& operator*=(const rep& rhs); constexpr duration& operator/=(const rep& rhs); constexpr duration& operator%=(const rep& rhs); constexpr duration& operator%=(const duration& rhs); // \ref{time.duration.special}, special values static constexpr duration zero(); static constexpr duration min(); static constexpr duration max(); }; \end{codeblock} \pnum \tcode{Rep} shall be an arithmetic type or a class emulating an arithmetic type. If \tcode{duration} is instantiated with a \tcode{duration} type as the argument for the template parameter \tcode{Rep}, the program is ill-formed. \pnum If \tcode{Period} is not a specialization of \tcode{ratio}, the program is ill-formed. If \tcode{Period::num} is not positive, the program is ill-formed. \pnum Members of \tcode{duration} shall not throw exceptions other than those thrown by the indicated operations on their representations. \pnum The defaulted copy constructor of duration shall be a constexpr function if and only if the required initialization of the member \tcode{rep_} for copy and move, respectively, would satisfy the requirements for a constexpr function. \pnum \begin{example} \begin{codeblock} duration> d0; // holds a count of minutes using a \tcode{long} duration d1; // holds a count of milliseconds using a \tcode{long long} duration> d2; // holds a count with a tick period of $\frac{1}{30}$ of a second // (30 Hz) using a \tcode{double} \end{codeblock} \end{example} \rSec3[time.duration.cons]{\tcode{duration} constructors} \indexlibrary{\idxcode{duration}!constructor}% \begin{itemdecl} template constexpr explicit duration(const Rep2& r); \end{itemdecl} \begin{itemdescr} \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{Rep2} is implicitly convertible to \tcode{rep} and \begin{itemize} \item \tcode{treat_as_floating_point_v} is \tcode{true} or \item \tcode{treat_as_floating_point_v} is \tcode{false}. \end{itemize} \begin{example} \begin{codeblock} duration d(3); // OK duration d(3.5); // error \end{codeblock} \end{example} \pnum \effects Constructs an object of type \tcode{duration}. \pnum \postconditions \tcode{count() == static_cast(r)}. \end{itemdescr} \indexlibrary{\idxcode{duration}!constructor}% \begin{itemdecl} template constexpr duration(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This constructor shall not participate in overload resolution unless no overflow is induced in the conversion and \tcode{treat_as_floating_point_v} is \tcode{true} or both \tcode{ratio_divide::den} is \tcode{1} and \tcode{treat_as_floating_point_v} is \tcode{false}. \begin{note} This requirement prevents implicit truncation error when converting between integral-based \tcode{duration} types. Such a construction could easily lead to confusion about the value of the \tcode{duration}. \end{note} \begin{example} \begin{codeblock} duration ms(3); duration us = ms; // OK duration ms2 = us; // error \end{codeblock} \end{example} \pnum \effects Constructs an object of type \tcode{duration}, constructing \tcode{rep_} from\\ \tcode{duration_cast(d).count()}. \end{itemdescr} \rSec3[time.duration.observer]{\tcode{duration} observer} \indexlibrarymember{count}{duration}% \begin{itemdecl} constexpr rep count() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rep_}. \end{itemdescr} \rSec3[time.duration.arithmetic]{\tcode{duration} arithmetic} \indexlibrarymember{operator+}{duration}% \begin{itemdecl} constexpr common_type_t operator+() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{common_type_t(*this)}. \end{itemdescr} \indexlibrarymember{operator-}{duration}% \begin{itemdecl} constexpr common_type_t operator-() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{common_type_t(-rep_)}. \end{itemdescr} \indexlibrarymember{operator++}{duration}% \begin{itemdecl} constexpr duration& operator++(); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{++rep_}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator++}{duration}% \begin{itemdecl} constexpr duration operator++(int); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{duration(rep_++)}. \end{itemdescr} \indexlibrarymember{operator\dcr}{duration}% \begin{itemdecl} constexpr duration& operator--(); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by \tcode{--rep_}. \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator\dcr}{duration}% \begin{itemdecl} constexpr duration operator--(int); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{duration(rep_-{}-)}. \end{itemdescr} \indexlibrarymember{operator+=}{duration}% \begin{itemdecl} constexpr duration& operator+=(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ += d.count();} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator-=}{duration}% \begin{itemdecl} constexpr duration& operator-=(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ -= d.count();} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator*=}{duration}% \begin{itemdecl} constexpr duration& operator*=(const rep& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ *= rhs;} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator/=}{duration}% \begin{itemdecl} constexpr duration& operator/=(const rep& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ /= rhs;} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator\%=}{duration}% \begin{itemdecl} constexpr duration& operator%=(const rep& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ \%= rhs;} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator\%=}{duration}% \begin{itemdecl} constexpr duration& operator%=(const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{rep_ \%= rhs.count();} \pnum \returns \tcode{*this}. \end{itemdescr} \rSec3[time.duration.special]{\tcode{duration} special values} \indexlibrarymember{zero}{duration}% \begin{itemdecl} static constexpr duration zero(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{duration(duration_values::zero())}. \end{itemdescr} \indexlibrarymember{min}{duration}% \begin{itemdecl} static constexpr duration min(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{duration(duration_values::min())}. \end{itemdescr} \indexlibrarymember{max}{duration}% \begin{itemdecl} static constexpr duration max(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{duration(duration_values::max())}. \end{itemdescr} \rSec3[time.duration.nonmember]{\tcode{duration} non-member arithmetic} \pnum In the function descriptions that follow, \tcode{CD} represents the return type of the function. \tcode{CR(A, B)} represents \tcode{common_type_t}. \indexlibrary{\idxcode{common_type}}% \begin{itemdecl} template constexpr common_type_t, duration> operator+(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{CD(CD(lhs).count() + CD(rhs).count())}. \end{itemdescr} \indexlibrary{\idxcode{common_type}}% \begin{itemdecl} template constexpr common_type_t, duration> operator-(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{CD(CD(lhs).count() - CD(rhs).count())}. \end{itemdescr} \indexlibrarymember{operator*}{duration}% \begin{itemdecl} template constexpr duration, Period> operator*(const duration& d, const Rep2& s); \end{itemdecl} \begin{itemdescr} \pnum \remarks This operator shall not participate in overload resolution unless \tcode{Rep2} is implicitly convertible to \tcode{CR(Rep1, Rep2)}. \pnum \returns \tcode{CD(CD(d).count() * s)}. \end{itemdescr} \indexlibrarymember{operator*}{duration}% \begin{itemdecl} template constexpr duration, Period> operator*(const Rep1& s, const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This operator shall not participate in overload resolution unless \tcode{Rep1} is implicitly convertible to \tcode{CR(Rep1, Rep2)}. \pnum \returns \tcode{d * s}. \end{itemdescr} \indexlibrarymember{operator/}{duration}% \begin{itemdecl} template constexpr duration, Period> operator/(const duration& d, const Rep2& s); \end{itemdecl} \begin{itemdescr} \pnum \remarks This operator shall not participate in overload resolution unless \tcode{Rep2} is implicitly convertible to \tcode{CR(Rep1, Rep2)} and \tcode{Rep2} is not a specialization of \tcode{duration}. \pnum \returns \tcode{CD(CD(d).count() / s)}. \end{itemdescr} \indexlibrarymember{operator/}{duration}% \begin{itemdecl} template constexpr common_type_t operator/(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{CD(lhs).count() / CD(rhs).count()}. \end{itemdescr} \indexlibrarymember{operator\%}{duration}% \begin{itemdecl} template constexpr duration, Period> operator%(const duration& d, const Rep2& s); \end{itemdecl} \begin{itemdescr} \pnum \remarks This operator shall not participate in overload resolution unless \tcode{Rep2} is implicitly convertible to \tcode{CR(Rep1, Rep2)} and \tcode{Rep2} is not a specialization of \tcode{duration}. \pnum \returns \tcode{CD(CD(d).count() \% s)}. \end{itemdescr} \indexlibrarymember{operator\%}{duration}% \begin{itemdecl} template constexpr common_type_t, duration> operator%(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{CD(CD(lhs).count() \% CD(rhs).count())}. \end{itemdescr} \rSec3[time.duration.comparisons]{\tcode{duration} comparisons} \pnum In the function descriptions that follow, \tcode{\placeholder{CT}} represents \tcode{common_type_t}, where \tcode{A} and \tcode{B} are the types of the two arguments to the function. \indexlibrarymember{operator==}{duration}% \begin{itemdecl} template constexpr bool operator==(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholder{CT}(lhs).count() == \placeholder{CT}(rhs).count()}. \end{itemdescr} \indexlibrarymember{operator"!=}{duration}% \begin{itemdecl} template constexpr bool operator!=(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(lhs == rhs)}. \end{itemdescr} \indexlibrarymember{operator<}{duration}% \begin{itemdecl} template constexpr bool operator<(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholder{CT}(lhs).count() < \placeholder{CT}(rhs).count()}. \end{itemdescr} \indexlibrarymember{operator<=}{duration}% \begin{itemdecl} template constexpr bool operator<=(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(rhs < lhs)}. \end{itemdescr} \indexlibrarymember{operator>}{duration}% \begin{itemdecl} template constexpr bool operator>(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rhs < lhs}. \end{itemdescr} \indexlibrarymember{operator>=}{duration}% \begin{itemdecl} template constexpr bool operator>=(const duration& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(lhs < rhs)}. \end{itemdescr} \rSec3[time.duration.cast]{\tcode{duration_cast}} \indexlibrary{\idxcode{duration}!\idxcode{duration_cast}}% \indexlibrary{\idxcode{duration_cast}}% \begin{itemdecl} template constexpr ToDuration duration_cast(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns Let \tcode{CF} be \tcode{ratio_divide}, and \tcode{CR} be \tcode{common_type::type}. \begin{itemize} \item If \tcode{CF::num == 1} and \tcode{CF::den == 1}, returns \begin{codeblock} ToDuration(static_cast(d.count())) \end{codeblock} \item otherwise, if \tcode{CF::num != 1} and \tcode{CF::den == 1}, returns \begin{codeblock} ToDuration(static_cast( static_cast(d.count()) * static_cast(CF::num))) \end{codeblock} \item otherwise, if \tcode{CF::num == 1} and \tcode{CF::den != 1}, returns \begin{codeblock} ToDuration(static_cast( static_cast(d.count()) / static_cast(CF::den))) \end{codeblock} \item otherwise, returns \begin{codeblock} ToDuration(static_cast( static_cast(d.count()) * static_cast(CF::num) / static_cast(CF::den))) \end{codeblock} \end{itemize} \pnum \begin{note} This function does not use any implicit conversions; all conversions are done with \tcode{static_cast}. It avoids multiplications and divisions when it is known at compile time that one or more arguments is 1. Intermediate computations are carried out in the widest representation and only converted to the destination representation at the final step. \end{note} \end{itemdescr} \indexlibrarymember{floor}{duration}% \begin{itemdecl} template constexpr ToDuration floor(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns The greatest result \tcode{t} representable in \tcode{ToDuration} for which \tcode{t <= d}. \end{itemdescr} \indexlibrarymember{ceil}{duration}% \begin{itemdecl} template constexpr ToDuration ceil(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns The least result \tcode{t} representable in \tcode{ToDuration} for which \tcode{t >= d}. \end{itemdescr} \indexlibrarymember{round}{duration}% \begin{itemdecl} template constexpr ToDuration round(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}, and \tcode{treat_as_floating_point_v} is \tcode{false}. \pnum \returns The value of \tcode{ToDuration} that is closest to \tcode{d}. If there are two closest values, then return the value \tcode{t} for which \tcode{t \% 2 == 0}. \end{itemdescr} \rSec3[time.duration.literals]{Suffixes for duration literals} \pnum This section describes literal suffixes for constructing duration literals. The suffixes \tcode{h}, \tcode{min}, \tcode{s}, \tcode{ms}, \tcode{us}, \tcode{ns} denote duration values of the corresponding types \tcode{hours}, \tcode{minutes}, \tcode{seconds}, \tcode{milliseconds}, \tcode{microseconds}, and \tcode{nanoseconds} respectively if they are applied to integral literals. \pnum If any of these suffixes are applied to a floating-point literal the result is a \tcode{chrono::duration} literal with an unspecified floating-point representation. \pnum If any of these suffixes are applied to an integer literal and the resulting \tcode{chrono::duration} value cannot be represented in the result type because of overflow, the program is ill-formed. \pnum \begin{example} The following code shows some duration literals. \begin{codeblock} using namespace std::chrono_literals; auto constexpr aday=24h; auto constexpr lesson=45min; auto constexpr halfanhour=0.5h; \end{codeblock} \end{example} \indexlibrarymember{operator""""h}{duration}% \begin{itemdecl} constexpr chrono::hours operator""h(unsigned long long hours); constexpr chrono::duration<@\unspec,@ ratio<3600, 1>> operator""h(long double hours); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{hours} hours. \end{itemdescr} \indexlibrarymember{operator""""min}{duration}% \begin{itemdecl} constexpr chrono::minutes operator""min(unsigned long long minutes); constexpr chrono::duration<@\unspec,@ ratio<60, 1>> operator""min(long double minutes); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{minutes} minutes. \end{itemdescr} \indexlibrarymember{operator""""s}{duration}% \begin{itemdecl} constexpr chrono::seconds @\itcorr@ operator""s(unsigned long long sec); constexpr chrono::duration<@\unspec@> operator""s(long double sec); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{sec} seconds. \pnum \begin{note} The same suffix \tcode{s} is used for \tcode{basic_string} but there is no conflict, since duration suffixes apply to numbers and string literal suffixes apply to character array literals. \end{note} \end{itemdescr} \indexlibrarymember{operator""""ms}{duration}% \begin{itemdecl} constexpr chrono::milliseconds operator""ms(unsigned long long msec); constexpr chrono::duration<@\unspec,@ milli> operator""ms(long double msec); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{msec} milliseconds. \end{itemdescr} \indexlibrarymember{operator""""us}{duration}% \begin{itemdecl} constexpr chrono::microseconds operator""us(unsigned long long usec); constexpr chrono::duration<@\unspec,@ micro> operator""us(long double usec); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{usec} microseconds. \end{itemdescr} \indexlibrarymember{operator""""ns}{duration}% \begin{itemdecl} constexpr chrono::nanoseconds operator""ns(unsigned long long nsec); constexpr chrono::duration<@\unspec,@ nano> operator""ns(long double nsec); \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{duration} literal representing \tcode{nsec} nanoseconds. \end{itemdescr} \rSec3[time.duration.alg]{\tcode{duration} algorithms} \indexlibrarymember{abs}{duration}% \begin{itemdecl} template constexpr duration abs(duration d); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{numeric_limits::is_signed} is \tcode{true}. \pnum \returns If \tcode{d >= d.zero()}, return \tcode{d}, otherwise return \tcode{-d}. \end{itemdescr} \rSec2[time.point]{Class template \tcode{time_point}} \indexlibrary{\idxcode{time_point}}% \begin{codeblock} template class time_point { public: using clock = Clock; using duration = Duration; using rep = typename duration::rep; using period = typename duration::period; private: duration d_; // \expos public: // \ref{time.point.cons}, construct constexpr time_point(); // has value epoch constexpr explicit time_point(const duration& d); // same as \tcode{time_point() + d} template constexpr time_point(const time_point& t); // \ref{time.point.observer}, observer constexpr duration time_since_epoch() const; // \ref{time.point.arithmetic}, arithmetic constexpr time_point& operator+=(const duration& d); constexpr time_point& operator-=(const duration& d); // \ref{time.point.special}, special values static constexpr time_point min(); static constexpr time_point max(); }; \end{codeblock} \pnum \tcode{Clock} shall meet the Clock requirements~(\ref{time.clock.req}). \pnum If \tcode{Duration} is not an instance of \tcode{duration}, the program is ill-formed. \rSec3[time.point.cons]{\tcode{time_point} constructors} \indexlibrary{\idxcode{time_point}!constructor}% \begin{itemdecl} constexpr time_point(); \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of type \tcode{time_point}, initializing \tcode{d_} with \tcode{duration::zero()}. Such a \tcode{time_point} object represents the epoch. \end{itemdescr} \indexlibrary{\idxcode{time_point}!constructor}% \begin{itemdecl} constexpr explicit time_point(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs an object of type \tcode{time_point}, initializing \tcode{d_} with \tcode{d}. Such a \tcode{time_point} object represents the epoch \tcode{+ d}. \end{itemdescr} \indexlibrary{\idxcode{time_point}!constructor}% \begin{itemdecl} template constexpr time_point(const time_point& t); \end{itemdecl} \begin{itemdescr} \pnum \remarks This constructor shall not participate in overload resolution unless \tcode{Duration2} is implicitly convertible to \tcode{duration}. \pnum \effects Constructs an object of type \tcode{time_point}, initializing \tcode{d_} with \tcode{t.time_since_epoch()}. \end{itemdescr} \rSec3[time.point.observer]{\tcode{time_point} observer} \indexlibrarymember{time_since_epoch}{time_point}% \begin{itemdecl} constexpr duration time_since_epoch() const; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{d_}. \end{itemdescr} \rSec3[time.point.arithmetic]{\tcode{time_point} arithmetic} \indexlibrarymember{operator+=}{time_point}% \begin{itemdecl} constexpr time_point& operator+=(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{d_ += d;} \pnum \returns \tcode{*this}. \end{itemdescr} \indexlibrarymember{operator-=}{time_point}% \begin{itemdecl} constexpr time_point& operator-=(const duration& d); \end{itemdecl} \begin{itemdescr} \pnum \effects As if by: \tcode{d_ -= d;} \pnum \returns \tcode{*this}. \end{itemdescr} \rSec3[time.point.special]{\tcode{time_point} special values} \indexlibrarymember{min}{time_point}% \begin{itemdecl} static constexpr time_point min(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{time_point(duration::min())}. \end{itemdescr} \indexlibrarymember{max}{time_point}% \begin{itemdecl} static constexpr time_point max(); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{time_point(duration::max())}. \end{itemdescr} \rSec3[time.point.nonmember]{\tcode{time_point} non-member arithmetic} \indexlibrarymember{operator+}{time_point}% \indexlibrarymember{operator+}{duration}% \begin{itemdecl} template constexpr time_point>> operator+(const time_point& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholder{CT}(lhs.time_since_epoch() + rhs)}, where \tcode{\placeholder{CT}} is the type of the return value. \end{itemdescr} \indexlibrarymember{operator+}{time_point}% \indexlibrarymember{operator+}{duration}% \begin{itemdecl} template constexpr time_point, Duration2>> operator+(const duration& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rhs + lhs}. \end{itemdescr} \indexlibrarymember{operator-}{time_point}% \indexlibrarymember{operator-}{duration}% \begin{itemdecl} template constexpr time_point>> operator-(const time_point& lhs, const duration& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{\placeholder{CT}(lhs.time_since_epoch() - rhs)}, where \tcode{\placeholder{CT}} is the type of the return value. \end{itemdescr} \indexlibrarymember{operator-}{time_point}% \begin{itemdecl} template constexpr common_type_t operator-(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{lhs.time_since_epoch() - rhs.time_since_epoch()}. \end{itemdescr} \rSec3[time.point.comparisons]{\tcode{time_point} comparisons} \indexlibrarymember{operator==}{time_point}% \begin{itemdecl} template constexpr bool operator==(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{lhs.time_since_epoch() == rhs.time_since_epoch()}. \end{itemdescr} \indexlibrarymember{operator"!=}{time_point}% \begin{itemdecl} template constexpr bool operator!=(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(lhs == rhs)}. \end{itemdescr} \indexlibrarymember{operator<}{time_point}% \begin{itemdecl} template constexpr bool operator<(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{lhs.time_since_epoch() < rhs.time_since_epoch()}. \end{itemdescr} \indexlibrarymember{operator<=}{time_point}% \begin{itemdecl} template constexpr bool operator<=(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(rhs < lhs)}. \end{itemdescr} \indexlibrarymember{operator>}{time_point}% \begin{itemdecl} template constexpr bool operator>(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rhs < lhs}. \end{itemdescr} \indexlibrarymember{operator>=}{time_point}% \begin{itemdecl} template constexpr bool operator>=(const time_point& lhs, const time_point& rhs); \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!(lhs < rhs)}. \end{itemdescr} \rSec3[time.point.cast]{\tcode{time_point_cast}} \indexlibrary{\idxcode{time_point}!\idxcode{time_point_cast}}% \indexlibrary{\idxcode{time_point_cast}}% \begin{itemdecl} template constexpr time_point time_point_cast(const time_point& t); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns \begin{codeblock} time_point(duration_cast(t.time_since_epoch())) \end{codeblock} \end{itemdescr} \indexlibrarymember{floor}{time_point}% \begin{itemdecl} template constexpr time_point floor(const time_point& tp); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns \tcode{time_point(floor(tp.time_since_epoch()))}. \end{itemdescr} \indexlibrarymember{ceil}{time_point}% \begin{itemdecl} template constexpr time_point ceil(const time_point& tp); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}. \pnum \returns \tcode{time_point(ceil(tp.time_since_epoch()))}. \end{itemdescr} \indexlibrarymember{round}{time_point}% \begin{itemdecl} template constexpr time_point round(const time_point& tp); \end{itemdecl} \begin{itemdescr} \pnum \remarks This function shall not participate in overload resolution unless \tcode{ToDuration} is a specialization of \tcode{duration}, and \tcode{treat_as_floating_point_v} is \tcode{false}. \pnum \returns \tcode{time_point(round(tp.time_since_epoch()))}. \end{itemdescr} \rSec2[time.clock]{Clocks} \pnum The types defined in this subclause shall satisfy the \tcode{TrivialClock} requirements~(\ref{time.clock.req}). \rSec3[time.clock.system]{Class \tcode{system_clock}} \indexlibrary{\idxcode{system_clock}}% \pnum Objects of class \tcode{system_clock} represent wall clock time from the system-wide realtime clock. \begin{codeblock} class system_clock { public: using rep = @\seebelow@; using period = ratio<@\unspecnc@, @\unspec{}@>; using duration = chrono::duration; using time_point = chrono::time_point; static constexpr bool is_steady = @\unspec;@ static time_point now() noexcept; // Map to C API static time_t to_time_t (const time_point& t) noexcept; static time_point from_time_t(time_t t) noexcept; }; \end{codeblock} \indexlibrarymember{rep}{system_clock}% \begin{itemdecl} using system_clock::rep = @\unspec@; \end{itemdecl} \begin{itemdescr} \pnum \requires \tcode{system_clock::duration::min() < system_clock::duration::zero()} shall be \tcode{true}.\\ \begin{note} This implies that \tcode{rep} is a signed type. \end{note} \end{itemdescr} \indexlibrarymember{to_time_t}{system_clock}% \begin{itemdecl} static time_t to_time_t(const time_point& t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{time_t} object that represents the same point in time as \tcode{t} when both values are restricted to the coarser of the precisions of \tcode{time_t} and \tcode{time_point}. It is \impldef{whether values are rounded or truncated to the required precision when converting between \tcode{time_t} values and \tcode{time_point} objects} whether values are rounded or truncated to the required precision. \end{itemdescr} \indexlibrarymember{from_time_t}{system_clock}% \begin{itemdecl} static time_point from_time_t(time_t t) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns A \tcode{time_point} object that represents the same point in time as \tcode{t} when both values are restricted to the coarser of the precisions of \tcode{time_t} and \tcode{time_point}. It is \impldef{whether values are rounded or truncated to the required precision when converting between \tcode{time_t} values and \tcode{time_point} objects} whether values are rounded or truncated to the required precision. \end{itemdescr} \rSec3[time.clock.steady]{Class \tcode{steady_clock}} \indexlibrary{\idxcode{steady_clock}}% \pnum Objects of class \tcode{steady_clock} represent clocks for which values of \tcode{time_point} never decrease as physical time advances and for which values of \tcode{time_point} advance at a steady rate relative to real time. That is, the clock may not be adjusted. \begin{codeblock} class steady_clock { public: using rep = @\unspec@; using period = ratio<@\unspecnc@, @\unspec{}@>; using duration = chrono::duration; using time_point = chrono::time_point<@\unspecnc@, duration>; static constexpr bool is_steady = true; static time_point now() noexcept; }; \end{codeblock} \rSec3[time.clock.hires]{Class \tcode{high_resolution_clock}} \indexlibrary{\idxcode{high_resolution_clock}}% \pnum Objects of class \tcode{high_resolution_clock} represent clocks with the shortest tick period. \tcode{high_resolution_clock} may be a synonym for \tcode{system_clock} or \tcode{steady_clock}. \begin{codeblock} class high_resolution_clock { public: using rep = @\unspec@; using period = ratio<@\unspecnc@, @\unspec{}@>; using duration = chrono::duration; using time_point = chrono::time_point<@\unspecnc@, duration>; static constexpr bool is_steady = @\unspec@; static time_point now() noexcept; }; \end{codeblock} \rSec2[ctime.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{ctime}}% \indexlibrary{\idxhdr{ctime}}% \indexlibrary{\idxcode{CLOCKS_PER_SEC}}% \indexlibrary{\idxcode{NULL}}% \indexlibrary{\idxcode{TIME_UTC}}% \indexlibrary{\idxcode{asctime}}% \indexlibrary{\idxcode{clock_t}}% \indexlibrary{\idxcode{clock}}% \indexlibrary{\idxcode{ctime}}% \indexlibrary{\idxcode{difftime}}% \indexlibrary{\idxcode{gmtime}}% \indexlibrary{\idxcode{localtime}}% \indexlibrary{\idxcode{mktime}}% \indexlibrary{\idxcode{size_t}}% \indexlibrary{\idxcode{strftime}}% \indexlibrary{\idxcode{time_t}}% \indexlibrary{\idxcode{timespec_get}}% \indexlibrary{\idxcode{timespec}}% \indexlibrary{\idxcode{time}}% \indexlibrary{\idxcode{tm}}% \begin{codeblock} #define NULL @\textit{see \ref{support.types.nullptr}}@ #define CLOCKS_PER_SEC @\seebelow@ #define TIME_UTC @\seebelow@ namespace std { using size_t = @\textit{see \ref{support.types.layout}}@; using clock_t = @\seebelow@; using time_t = @\seebelow@; struct timespec; struct tm; clock_t clock(); double difftime(time_t time1, time_t time0); time_t mktime(struct tm* timeptr); time_t time(time_t* timer); int timespec_get(timespec* ts, int base); char* asctime(const struct tm* timeptr); char* ctime(const time_t* timer); struct tm* gmtime(const time_t* timer); struct tm* localtime(const time_t* timer); size_t strftime(char* s, size_t maxsize, const char* format, const struct tm* timeptr); } \end{codeblock} \pnum \indextext{\idxhdr{time.h}}% \indexlibrary{\idxhdr{time.h}}% \indextext{\idxhdr{ctime}}% \indexlibrary{\idxhdr{ctime}}% The contents of the header \tcode{} are the same as the C standard library header \tcode{}.% \footnote{\tcode{strftime} supports the C conversion specifiers \tcode{C}, \tcode{D}, \tcode{e}, \tcode{F}, \tcode{g}, \tcode{G}, \tcode{h}, \tcode{r}, \tcode{R}, \tcode{t}, \tcode{T}, \tcode{u}, \tcode{V}, and \tcode{z}, and the modifiers \tcode{E} and \tcode{O}.} \pnum The functions \tcode{asctime}, \tcode{ctime}, \tcode{gmtime}, and \tcode{localtime} are not required to avoid data races~(\ref{res.on.data.races}). \xref ISO C 7.27 \rSec1[type.index]{Class \tcode{type_index}} \rSec2[type.index.synopsis]{Header \tcode{} synopsis} \indextext{\idxhdr{typeindex}}% \indexlibrary{\idxhdr{typeindex}}% \begin{codeblock} namespace std { class type_index; template struct hash; template<> struct hash; } \end{codeblock} \rSec2[type.index.overview]{\tcode{type_index} overview} \indexlibrary{\idxcode{type_index}}% \begin{codeblock} namespace std { class type_index { public: type_index(const type_info& rhs) noexcept; bool operator==(const type_index& rhs) const noexcept; bool operator!=(const type_index& rhs) const noexcept; bool operator< (const type_index& rhs) const noexcept; bool operator<= (const type_index& rhs) const noexcept; bool operator> (const type_index& rhs) const noexcept; bool operator>= (const type_index& rhs) const noexcept; size_t hash_code() const noexcept; const char* name() const noexcept; private: const type_info* target; // \expos // Note that the use of a pointer here, rather than a reference, // means that the default copy/move constructor and assignment // operators will be provided and work as expected. }; } \end{codeblock} \pnum The class \tcode{type_index} provides a simple wrapper for \tcode{type_info} which can be used as an index type in associative containers~(\ref{associative}) and in unordered associative containers~(\ref{unord}). \rSec2[type.index.members]{\tcode{type_index} members} \indexlibrary{\idxcode{type_index}!constructor}% \begin{itemdecl} type_index(const type_info& rhs) noexcept; \end{itemdecl} \begin{itemdescr} \pnum \effects Constructs a \tcode{type_index} object, the equivalent of \tcode{target = \&rhs}. \end{itemdescr} \indexlibrarymember{operator==}{type_index}% \begin{itemdecl} bool operator==(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{*target == *rhs.target}. \end{itemdescr} \indexlibrarymember{operator"!=}{type_index}% \begin{itemdecl} bool operator!=(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{*target != *rhs.target}. \end{itemdescr} \indexlibrarymember{operator<}{type_index}% \begin{itemdecl} bool operator<(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{target->before(*rhs.target)}. \end{itemdescr} \indexlibrarymember{operator<=}{type_index}% \begin{itemdecl} bool operator<=(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!rhs.target->before(*target)}. \end{itemdescr} \indexlibrarymember{operator>}{type_index}% \begin{itemdecl} bool operator>(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{rhs.target->before(*target)}. \end{itemdescr} \indexlibrarymember{operator>=}{type_index}% \begin{itemdecl} bool operator>=(const type_index& rhs) const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{!target->before(*rhs.target)}. \end{itemdescr} \indexlibrarymember{hash_code}{type_index}% \begin{itemdecl} size_t hash_code() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{target->hash_code()}. \end{itemdescr} \indexlibrarymember{name}{type_index}% \begin{itemdecl} const char* name() const noexcept; \end{itemdecl} \begin{itemdescr} \pnum \returns \tcode{target->name()}. \end{itemdescr} \rSec2[type.index.hash]{Hash support} \indexlibrary{\idxcode{hash}!\idxcode{type_index}}% \begin{itemdecl} template <> struct hash; \end{itemdecl} \begin{itemdescr} \pnum For an object \tcode{index} of type \tcode{type_index}, \tcode{hash()(index)} shall evaluate to the same result as \tcode{index.hash_code()}. \end{itemdescr} \rSec1[execpol]{Execution policies} \rSec2[execpol.general]{In general} \pnum This subclause describes classes that are \defn{execution policy} types. An object of an execution policy type indicates the kinds of parallelism allowed in the execution of an algorithm and expresses the consequent requirements on the element access functions. \begin{example} \begin{codeblock} using namespace std; vector v = @\commentellip@; // standard sequential sort sort(v.begin(), v.end()); // explicitly sequential sort sort(execution::seq, v.begin(), v.end()); // permitting parallel execution sort(execution::par, v.begin(), v.end()); // permitting vectorization as well sort(execution::par_unseq, v.begin(), v.end()); \end{codeblock} \end{example} \begin{note} Because different parallel architectures may require idiosyncratic parameters for efficient execution, implementations may provide additional execution policies to those described in this standard as extensions. \end{note} \rSec2[execution.syn]{Header \tcode{} synopsis} \indextext{\idxhdr{execution}}% \indexlibrary{\idxhdr{execution}}% \begin{codeblock} namespace std { // \ref{execpol.type}, execution policy type trait template struct is_execution_policy; template inline constexpr bool is_execution_policy_v = is_execution_policy::value; } namespace std::execution { // \ref{execpol.seq}, sequenced execution policy class sequenced_policy; // \ref{execpol.par}, parallel execution policy class parallel_policy; // \ref{execpol.parunseq}, parallel and unsequenced execution policy class parallel_unsequenced_policy; // \ref{execpol.objects}, execution policy objects inline constexpr sequenced_policy seq{ @\unspec@ }; inline constexpr parallel_policy par{ @\unspec@ }; inline constexpr parallel_unsequenced_policy par_unseq{ @\unspec@ }; } \end{codeblock} \rSec2[execpol.type]{Execution policy type trait} \indexlibrary{\idxcode{is_execution_policy}}% \begin{itemdecl} template struct is_execution_policy { @\seebelow@ }; \end{itemdecl} \begin{itemdescr} \pnum \tcode{is_execution_policy} can be used to detect execution policies for the purpose of excluding function signatures from otherwise ambiguous overload resolution participation. \pnum \tcode{is_execution_policy} shall be a \tcode{UnaryTypeTrait} with a base characteristic of \tcode{true_type} if \tcode{T} is the type of a standard or \impldef{additional execution policies supported by parallel algorithms} execution policy, otherwise \tcode{false_type}. \begin{note} This provision reserves the privilege of creating non-standard execution policies to the library implementation. \end{note} \pnum The behavior of a program that adds specializations for \tcode{is_execution_policy} is undefined. \end{itemdescr} \rSec2[execpol.seq]{Sequenced execution policy} \indexlibrary{\idxcode{execution::sequenced_policy}}% \begin{itemdecl} class execution::sequenced_policy { @\unspec@ }; \end{itemdecl} \begin{itemdescr} \pnum The class \tcode{execution::sequenced_policy} is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and require that a parallel algorithm's execution may not be parallelized. \pnum During the execution of a parallel algorithm with the \tcode{execution::sequenced_policy} policy, if the invocation of an element access function exits via an uncaught exception, \tcode{terminate()} shall be called. \end{itemdescr} \rSec2[execpol.par]{Parallel execution policy} \indexlibrary{\idxcode{execution::parallel_policy}}% \begin{itemdecl} class execution::parallel_policy { @\unspec@ }; \end{itemdecl} \begin{itemdescr} \pnum The class \tcode{execution::parallel_policy} is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm's execution may be parallelized. \pnum During the execution of a parallel algorithm with the \tcode{execution::parallel_policy} policy, if the invocation of an element access function exits via an uncaught exception, \tcode{terminate()} shall be called. \end{itemdescr} \rSec2[execpol.parunseq]{Parallel and unsequenced execution policy} \indexlibrary{\idxcode{execution::parallel_unsequenced_policy}}% \begin{itemdecl} class execution::parallel_unsequenced_policy { @\unspec@ }; \end{itemdecl} \begin{itemdescr} \pnum The class \tcode{execution::parallel_unsequenced_policy} is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm's execution may be parallelized and vectorized. \pnum During the execution of a parallel algorithm with the \tcode{execution::parallel_unsequenced_policy} policy, if the invocation of an element access function exits via an uncaught exception, \tcode{terminate()} shall be called. \end{itemdescr} \rSec2[execpol.objects]{Execution policy objects} \indexlibrary{\idxcode{seq}}% \indexlibrary{\idxcode{par}}% \indexlibrary{\idxcode{par_unseq}}% \indexlibrary{\idxcode{execution}!\idxcode{seq}}% \indexlibrary{\idxcode{execution}!\idxcode{par}}% \indexlibrary{\idxcode{execution}!\idxcode{par_unseq}}% \begin{itemdecl} inline constexpr execution::sequenced_policy execution::seq{ @\unspec@ }; inline constexpr execution::parallel_policy execution::par{ @\unspec@ }; inline constexpr execution::parallel_unsequenced_policy execution::par_unseq{ @\unspec@ }; \end{itemdecl} \begin{itemdescr} \pnum The header \tcode{} declares global objects associated with each type of execution policy. \end{itemdescr}