[function.objects] (original) (raw)

20 General utilities library [utilities]

20.14.1 General [function.objects.general]

A 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, 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.

20.14.2 Header synopsis [functional.syn]

[Example 1:

If a C++ program wants to have a by-element addition of two vectors aand b containing double and put the result into a, it can do:transform(a.begin(), a.end(), b.begin(), a.begin(), plus<double>());

— _end example_]

[Example 2:

To negate every element of a:transform(a.begin(), a.end(), a.begin(), negate<double>());

— _end example_]

20.14.3 Definitions [func.def]

The following definitions apply to this Clause:

A call signature is the name of a return type followed by a parenthesized comma-separated list of zero or more argument types.

A callable type is a function object type or a pointer to member.

A callable object is an object of a callable type.

A call wrapper type is a type that holds a callable object and supports a call operation that forwards to that object.

A call wrapper is an object of a call wrapper type.

A target object is the callable object held by a call wrapper.

A call wrapper type may additionally hold a sequence of objects and references that may be passed as arguments to the target object.

These entities are collectively referred to as bound argument entities.

The target object and bound argument entities of the call wrapper are collectively referred to as state entities.

20.14.4 Requirements [func.require]

Define INVOKE(f, t, t, …, t) as follows:

Define INVOKE<R>(f, t, t, …, t) asstatic_­cast<void>(INVOKE(f, t, t, …, t))if R is cv void, otherwise_INVOKE_(f, t, t, …, t) implicitly converted to R.

Every call wrapper ([func.def]) meets the _Cpp17MoveConstructible_and Cpp17Destructible requirements.

An argument 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 delivers rvalue arguments as rvalue references and lvalue arguments as lvalue references.

[Note 1:

In a typical implementation, argument forwarding call wrappers have an overloaded function call operator of the formtemplate<class... UnBoundArgs> constexpr R operator()(UnBoundArgs&&... unbound_args) cv-qual;

— _end note_]

A perfect forwarding call wrapper is an argument forwarding call wrapper that forwards its state entities to the underlying call expression.

This forwarding step delivers a state entity of type Tas cv T&when the call is performed on an lvalue of the call wrapper type and as cv T&& otherwise, where cv represents the cv-qualifiers of the call wrapper and where cv shall be neither volatile nor const volatile.

A call pattern defines the semantics of invoking a perfect forwarding call wrapper.

A postfix call performed on a perfect forwarding call wrapper is expression-equivalent ([defns.expression-equivalent]) to an expression e determined from its call pattern cpby replacing all occurrences of the arguments of the call wrapper and its state entities with references as described in the corresponding forwarding steps.

A simple call wrapper is a perfect forwarding call wrapper that meets the Cpp17CopyConstructible and Cpp17CopyAssignable requirements and whose copy constructor, move constructor, and assignment operators are constexpr functions that do not throw exceptions.

The copy/move constructor of an argument forwarding call wrapper has the same apparent semantics as if memberwise copy/move of its state entities were performed ([class.copy.ctor]).

[Note 2:

This implies that each of the copy/move constructors has the same exception-specification as the corresponding implicit definition and is declared as constexprif the corresponding implicit definition would be considered to be constexpr.

— _end note_]

Argument forwarding call wrappers returned by a given standard library function template have the same type if the types of their corresponding state entities are the same.

20.14.5 Function template invoke [func.invoke]

template<class F, class... Args> constexpr invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) noexcept(is_nothrow_invocable_v<F, Args...>);

Returns: INVOKE(std​::​forward<F>(f), std​::​forward<Args>(args)...).

20.14.6 Class template reference_­wrapper [refwrap]

20.14.6.1 General [refwrap.general]

namespace std { template<class T> class reference_wrapper { public: using type = T;template<class U> constexpr reference_wrapper(U&&) noexcept(see below);constexpr reference_wrapper(const reference_wrapper& x) noexcept;constexpr reference_wrapper& operator=(const reference_wrapper& x) noexcept;constexpr operator T& () const noexcept;constexpr T& get() const noexcept;template<class... ArgTypes> constexpr invoke_result_t<T&, ArgTypes...> operator()(ArgTypes&&...) const;};template<class T> reference_wrapper(T&) -> reference_wrapper<T>;}

reference_­wrapper<T> is a Cpp17CopyConstructible and Cpp17CopyAssignable wrapper around a reference to an object or function of type T.

The template parameter T of reference_­wrappermay be an incomplete type.

20.14.6.2 Constructors and destructor [refwrap.const]

template<class U> constexpr reference_wrapper(U&& u) noexcept(_see below_);

Let FUN denote the exposition-only functionsvoid FUN(T&) noexcept;void FUN(T&&) = delete;

Constraints: The expression FUN(declval<U>()) is well-formed andis_­same_­v<remove_­cvref_­t<U>, reference_­wrapper> is false.

Effects: Creates a variable ras if by T& r = std​::​forward<U>(u), then constructs a reference_­wrapper object that stores a reference to r.

Remarks: The expression inside noexceptis equivalent to noexcept(FUN(declval<U>())).

constexpr reference_wrapper(const reference_wrapper& x) noexcept;

Effects: Constructs a reference_­wrapper object that stores a reference to x.get().

20.14.6.3 Assignment [refwrap.assign]

constexpr reference_wrapper& operator=(const reference_wrapper& x) noexcept;

Postconditions: *this stores a reference to x.get().

20.14.6.4 Access [refwrap.access]

constexpr operator T& () const noexcept;

Returns: The stored reference.

constexpr T& get() const noexcept;

Returns: The stored reference.

20.14.6.5 Invocation [refwrap.invoke]

template<class... ArgTypes> constexpr invoke_result_t<T&, ArgTypes...> operator()(ArgTypes&&... args) const;

Mandates: T is a complete type.

Returns: INVOKE(get(), std​::​forward<ArgTypes>(args)...).

20.14.6.6 Helper functions [refwrap.helpers]

The template parameter T of the following ref and cref function templates may be an incomplete type.

template<class T> constexpr reference_wrapper<T> ref(T& t) noexcept;

Returns: reference_­wrapper<T>(t).

template<class T> constexpr reference_wrapper<T> ref(reference_wrapper<T> t) noexcept;

template<class T> constexpr reference_wrapper<const T> cref(const T& t) noexcept;

Returns: reference_­wrapper <const T>(t).

template<class T> constexpr reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;

20.14.7 Arithmetic operations [arithmetic.operations]

20.14.7.2 Class template plus [arithmetic.operations.plus]

template<class T = void> struct plus { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct plus<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) + std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) + std::forward<U>(u));

Returns: std​::​forward<T>(t) + std​::​forward<U>(u).

20.14.7.3 Class template minus [arithmetic.operations.minus]

template<class T = void> struct minus { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct minus<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) - std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) - std::forward<U>(u));

Returns: std​::​forward<T>(t) - std​::​forward<U>(u).

20.14.7.4 Class template multiplies [arithmetic.operations.multiplies]

template<class T = void> struct multiplies { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct multiplies<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) * std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) * std::forward<U>(u));

Returns: std​::​forward<T>(t) * std​::​forward<U>(u).

20.14.7.5 Class template divides [arithmetic.operations.divides]

template<class T = void> struct divides { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct divides<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) / std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) / std::forward<U>(u));

Returns: std​::​forward<T>(t) / std​::​forward<U>(u).

20.14.7.6 Class template modulus [arithmetic.operations.modulus]

template<class T = void> struct modulus { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct modulus<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) % std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) % std::forward<U>(u));

Returns: std​::​forward<T>(t) % std​::​forward<U>(u).

20.14.7.7 Class template negate [arithmetic.operations.negate]

template<class T = void> struct negate { constexpr T operator()(const T& x) const;};

constexpr T operator()(const T& x) const;

template<> struct negate<void> { template<class T> constexpr auto operator()(T&& t) const -> decltype(-std::forward<T>(t));using is_transparent = _unspecified_;};

template<class T> constexpr auto operator()(T&& t) const -> decltype(-std::forward<T>(t));

Returns: -std​::​forward<T>(t).

20.14.8 Comparisons [comparisons]

20.14.8.1 General [comparisons.general]

The library provides basic function object classes for all of the comparison operators in the language ([expr.rel], [expr.eq]).

For templates less, greater, less_­equal, andgreater_­equal, the specializations for any pointer type yield a result consistent with the implementation-defined strict total order over pointers ([defns.order.ptr]).

[Note 1:

If a < b is well-defined for pointers a and b of type P, then (a < b) == less<P>()(a, b),(a > b) == greater<P>()(a, b), and so forth.

— _end note_]

For template specializations less<void>, greater<void>,less_­equal<void>, and greater_­equal<void>, if the call operator calls a built-in operator comparing pointers, the call operator yields a result consistent with the implementation-defined strict total order over pointers.

20.14.8.2 Class template equal_­to [comparisons.equal.to]

template<class T = void> struct equal_to { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct equal_to<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) == std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) == std::forward<U>(u));

Returns: std​::​forward<T>(t) == std​::​forward<U>(u).

20.14.8.3 Class template not_­equal_­to [comparisons.not.equal.to]

template<class T = void> struct not_equal_to { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct not_equal_to<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) != std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) != std::forward<U>(u));

Returns: std​::​forward<T>(t) != std​::​forward<U>(u).

20.14.8.4 Class template greater [comparisons.greater]

template<class T = void> struct greater { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct greater<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) > std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) > std::forward<U>(u));

Returns: std​::​forward<T>(t) > std​::​forward<U>(u).

20.14.8.5 Class template less [comparisons.less]

template<class T = void> struct less { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct less<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) < std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) < std::forward<U>(u));

Returns: std​::​forward<T>(t) < std​::​forward<U>(u).

20.14.8.6 Class template greater_­equal [comparisons.greater.equal]

template<class T = void> struct greater_equal { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct greater_equal<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) >= std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) >= std::forward<U>(u));

Returns: std​::​forward<T>(t) >= std​::​forward<U>(u).

20.14.8.7 Class template less_­equal [comparisons.less.equal]

template<class T = void> struct less_equal { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct less_equal<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) <= std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) <= std::forward<U>(u));

Returns: std​::​forward<T>(t) <= std​::​forward<U>(u).

20.14.8.8 Class compare_­three_­way [comparisons.three.way]

In this subclause, BUILTIN-PTR-THREE-WAY(T, U)for types T and U is a boolean constant expression.

BUILTIN-PTR-THREE-WAY(T, U) is trueif and only if <=> in the expressiondeclval<T>() <=> declval<U>() resolves to a built-in operator comparing pointers.

struct compare_three_way { template<class T, class U> requires three_way_comparable_with<T, U> || BUILTIN-PTR-THREE-WAY(T, U) constexpr auto operator()(T&& t, U&& u) const;using is_transparent = unspecified;};

template<class T, class U> requires three_way_comparable_with<T, U> || _BUILTIN-PTR-THREE-WAY_(T, U) constexpr auto operator()(T&& t, U&& u) const;

Preconditions: If the expression std​::​forward<T>(t) <=> std​::​forward<U>(u) results in a call to a built-in operator <=> comparing pointers of type P, the conversion sequences from both T and U to Pare equality-preserving ([concepts.equality]).

Effects:

20.14.9 Concept-constrained comparisons [range.cmp]

In this subclause, BUILTIN-PTR-CMP(T, op, U) for types Tand U and where op is an equality ([expr.eq]) or relational operator ([expr.rel]) is a boolean constant expression.

BUILTIN-PTR-CMP(T, op, U) is true if and only if opin the expression declval<T>() op declval<U>() resolves to a built-in operator comparing pointers.

struct ranges::equal_to { template<class T, class U> requires equality_comparable_with<T, U> || _BUILTIN-PTR-CMP_(T, ==, U) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

Preconditions: If the expression std​::​forward<T>(t) == std​::​forward<U>(u)results in a call to a built-in operator == comparing pointers of typeP, the conversion sequences from both T and U to Pare equality-preserving ([concepts.equality]).

Effects:

struct ranges::not_equal_to { template<class T, class U> requires equality_comparable_with<T, U> || _BUILTIN-PTR-CMP_(T, ==, U) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

operator() has effects equivalent to:return !ranges::equal_to{}(std::forward<T>(t), std::forward<U>(u));

struct ranges::greater { template<class T, class U> requires [totally_­ordered_­with](concept.totallyordered#concept:totally%5Fordered%5Fwith "18.5.4 Concept totally_­ordered [concept.totallyordered]")<T, U> || _BUILTIN-PTR-CMP_(U, <, T) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

operator() has effects equivalent to:return ranges::less{}(std::forward<U>(u), std::forward<T>(t));

struct ranges::less { template<class T, class U> requires [totally_­ordered_­with](concept.totallyordered#concept:totally%5Fordered%5Fwith "18.5.4 Concept totally_­ordered [concept.totallyordered]")<T, U> || _BUILTIN-PTR-CMP_(T, <, U) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

Preconditions: If the expression std​::​forward<T>(t) < std​::​forward<U>(u) results in a call to a built-in operator < comparing pointers of type P, the conversion sequences from both T and U to P are equality-preserving ([concepts.equality]).

For any expressionsET and EU such that decltype((ET)) is T anddecltype((EU)) is U, exactly one ofranges​::​less{}(ET, EU),ranges​::​less{}(EU, ET), orranges​::​equal_­to{}(ET, EU)is true.

Effects:

struct ranges::greater_equal { template<class T, class U> requires [totally_­ordered_­with](concept.totallyordered#concept:totally%5Fordered%5Fwith "18.5.4 Concept totally_­ordered [concept.totallyordered]")<T, U> || _BUILTIN-PTR-CMP_(T, <, U) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

operator() has effects equivalent to:return !ranges::less{}(std::forward<T>(t), std::forward<U>(u));

struct ranges::less_equal { template<class T, class U> requires [totally_­ordered_­with](concept.totallyordered#concept:totally%5Fordered%5Fwith "18.5.4 Concept totally_­ordered [concept.totallyordered]")<T, U> || _BUILTIN-PTR-CMP_(U, <, T) constexpr bool operator()(T&& t, U&& u) const;using is_transparent = _unspecified_;};

operator() has effects equivalent to:return !ranges::less{}(std::forward<U>(u), std::forward<T>(t));

20.14.10 Logical operations [logical.operations]

20.14.10.2 Class template logical_­and [logical.operations.and]

template<class T = void> struct logical_and { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct logical_and<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) && std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) && std::forward<U>(u));

Returns: std​::​forward<T>(t) && std​::​forward<U>(u).

20.14.10.3 Class template logical_­or [logical.operations.or]

template<class T = void> struct logical_or { constexpr bool operator()(const T& x, const T& y) const;};

constexpr bool operator()(const T& x, const T& y) const;

template<> struct logical_or<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) || std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) || std::forward<U>(u));

Returns: std​::​forward<T>(t) || std​::​forward<U>(u).

20.14.10.4 Class template logical_­not [logical.operations.not]

template<class T = void> struct logical_not { constexpr bool operator()(const T& x) const;};

constexpr bool operator()(const T& x) const;

template<> struct logical_not<void> { template<class T> constexpr auto operator()(T&& t) const -> decltype(!std::forward<T>(t));using is_transparent = _unspecified_;};

template<class T> constexpr auto operator()(T&& t) const -> decltype(!std::forward<T>(t));

Returns: !std​::​forward<T>(t).

20.14.11 Bitwise operations [bitwise.operations]

20.14.11.2 Class template bit_­and [bitwise.operations.and]

template<class T = void> struct bit_and { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct bit_and<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) & std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) & std::forward<U>(u));

Returns: std​::​forward<T>(t) & std​::​forward<U>(u).

20.14.11.3 Class template bit_­or [bitwise.operations.or]

template<class T = void> struct bit_or { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct bit_or<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) | std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) | std::forward<U>(u));

Returns: std​::​forward<T>(t) | std​::​forward<U>(u).

20.14.11.4 Class template bit_­xor [bitwise.operations.xor]

template<class T = void> struct bit_xor { constexpr T operator()(const T& x, const T& y) const;};

constexpr T operator()(const T& x, const T& y) const;

template<> struct bit_xor<void> { template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) ^ std::forward<U>(u));using is_transparent = _unspecified_;};

template<class T, class U> constexpr auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) ^ std::forward<U>(u));

Returns: std​::​forward<T>(t) ^ std​::​forward<U>(u).

20.14.11.5 Class template bit_­not [bitwise.operations.not]

template<class T = void> struct bit_not { constexpr T operator()(const T& x) const;};

constexpr T operator()(const T& x) const;

template<> struct bit_not<void> { template<class T> constexpr auto operator()(T&& t) const -> decltype(~std::forward<T>(t));using is_transparent = _unspecified_;};

template<class T> constexpr auto operator()(T&&) const -> decltype(~std::forward<T>(t));

Returns: ~std​::​forward<T>(t).

20.14.12 Class identity [func.identity]

struct identity { template<class T> constexpr T&& operator()(T&& t) const noexcept;using is_transparent = _unspecified_;};template<class T> constexpr T&& operator()(T&& t) const noexcept;

Effects: Equivalent to: return std​::​forward<T>(t);

20.14.13 Function template not_­fn [func.not.fn]

template<class F> constexpr _unspecified_ not_fn(F&& f);

In the text that follows:

Mandates: is_­constructible_­v<FD, F> && is_­move_­constructible_­v<FD>is true.

Preconditions: FD meets the Cpp17MoveConstructible requirements.

Returns: A perfect forwarding call wrapper gwith call pattern !invoke(fd, call_­args...).

Throws: Any exception thrown by the initialization of fd.

20.14.14 Function template bind_­front [func.bind.front]

template<class F, class... Args> constexpr _unspecified_ bind_front(F&& f, Args&&... args);

In the text that follows:

Mandates: is_constructible_v<FD, F> &&is_move_constructible_v<FD> && (is_constructible_v<BoundArgs, Args> && ...) && (is_move_constructible_v<BoundArgs> && ...) is true.

Preconditions: FD meets the Cpp17MoveConstructible requirements.

For each in BoundArgs, if is an object type, meets the Cpp17MoveConstructible requirements.

Returns: A perfect forwarding call wrapper gwith call pattern invoke(fd, bound_­args..., call_­args...).

Throws: Any exception thrown by the initialization of the state entities of g ([func.def]).

20.14.15 Function object binders [func.bind]

20.14.15.1 General [func.bind.general]

Subclause [func.bind] describes a uniform mechanism for binding arguments of callable objects.

20.14.15.2 Class template is_­bind_­expression [func.bind.isbind]

namespace std { template<class T> struct is_bind_expression; }

The class template is_­bind_­expression can be used to detect function objects generated by bind.

The function template binduses is_­bind_­expression to detect subexpressions.

Specializations of the is_­bind_­expression template shall meet the Cpp17UnaryTypeTrait requirements ([meta.rqmts]).

The implementation provides a definition that has a base characteristic oftrue_­type if T is a type returned from bind, otherwise it has a base characteristic of false_­type.

A program may specialize this template for a program-defined type Tto have a base characteristic of true_­type to indicate thatT should be treated as a subexpression in a bind call.

20.14.15.3 Class template is_­placeholder [func.bind.isplace]

namespace std { template<class T> struct is_placeholder; }

The class template is_­placeholder can be used to detect the standard placeholders_­1, _­2, and so on.

The function template bind usesis_­placeholder to detect placeholders.

Specializations of the is_­placeholder template shall meet the Cpp17UnaryTypeTrait requirements ([meta.rqmts]).

The implementation provides a definition that has the base characteristic ofintegral_­constant<int, J_> if T is the type ofstd​::​placeholders​::​_­_J, otherwise it has a base characteristic of integral_­constant<int, 0>.

A program may specialize this template for a program-defined type T to have a base characteristic of integral_­constant<int, N>with N > 0 to indicate that T should be treated as a placeholder type.

20.14.15.4 Function template bind [func.bind.bind]

In the text that follows:

template<class F, class... BoundArgs> constexpr _unspecified_ bind(F&& f, BoundArgs&&... bound_args);template<class R, class F, class... BoundArgs> constexpr _unspecified_ bind(F&& f, BoundArgs&&... bound_args);

Mandates: is_­constructible_­v<FD, F> is true.

For each in BoundArgs, is_­constructible_­v<, > is true.

Preconditions: FD and each meet the Cpp17MoveConstructible and Cpp17Destructible requirements.

INVOKE(fd, , , …,) ([func.require]) is a valid expression for some values , , …, , whereN has the value sizeof...(bound_­args).

Returns: An argument forwarding call wrapper g ([func.require]).

A program that attempts to invoke a volatile-qualified gis ill-formed.

When g is not volatile-qualified, invocation ofg(, , …, )is expression-equivalent ([defns.expression-equivalent]) to_INVOKE_(static_cast<>(),static_cast<>(), static_cast<>(), …, static_cast<>()) for the first overload, and_INVOKE_<R>(static_cast<>(),static_cast<>(), static_cast<>(), …, static_cast<>()) for the second overload, where the values and types of the target argument and of the bound arguments, , …, are determined as specified below.

Throws: Any exception thrown by the initialization of the state entities of g.

[Note 1:

If all of FD and meet the requirements of Cpp17CopyConstructible, then the return type meets the requirements of Cpp17CopyConstructible.

— _end note_]

The values of the bound arguments , , …, and their corresponding types , , …, depend on the types derived from the call to bind and the cv-qualifiers cv of the call wrapper g as follows:

The value of the target argument is fd and its corresponding type is cv FD&.

20.14.15.5 Placeholders [func.bind.place]

namespace std::placeholders { see below _1;see below _2;. . . see below _M;}

All placeholder types meet the Cpp17DefaultConstructible and_Cpp17CopyConstructible_ requirements, and their default constructors and copy/move constructors are constexpr functions that do not throw exceptions.

It is implementation-defined whether placeholder types meet the Cpp17CopyAssignable requirements, but if so, their copy assignment operators are constexpr functions that do not throw exceptions.

Placeholders should be defined as:inline constexpr unspecified _1{};

If they are not, they are declared as:extern unspecified _1;

20.14.16 Function template mem_­fn [func.memfn]

template<class R, class T> constexpr _unspecified_ mem_fn(R T::* pm) noexcept;

Returns: A simple call wrapper ([func.def]) fnwith call pattern invoke(pmd, call_­args...), wherepmd is the target object of fn of type R T​::​*direct-non-list-initialized with pm, andcall_­args is an argument pack used in a function call expression ([expr.call]) of pm.

20.14.17 Polymorphic function wrappers [func.wrap]

20.14.17.1 General [func.wrap.general]

Subclause [func.wrap] describes a polymorphic wrapper class that encapsulates arbitrary callable objects.

20.14.17.2 Class bad_­function_­call [func.wrap.badcall]

An exception of type bad_­function_­call is thrown byfunction​::​operator() ([func.wrap.func.inv]) when the function wrapper object has no target.

namespace std { class bad_function_call : public exception { public: const char* what() const noexcept override;};}

const char* what() const noexcept override;

Returns: Animplementation-defined ntbs.

20.14.17.3 Class template function [func.wrap.func]

20.14.17.3.1 General [func.wrap.func.general]

namespace std { template<class> class function; template<class R, class... ArgTypes> class function<R(ArgTypes...)> { public: using result_type = R; function() noexcept; function(nullptr_t) noexcept; function(const function&); function(function&&) noexcept;template<class F> function(F); function& operator=(const function&); function& operator=(function&&); function& operator=(nullptr_t) noexcept;template<class F> function& operator=(F&&);template<class F> function& operator=(reference_wrapper<F>) noexcept;~function();void swap(function&) noexcept;explicit operator bool() const noexcept; R operator()(ArgTypes...) const;const type_info& target_type() const noexcept;template<class T> T* target() noexcept;template<class T> const T* target() const noexcept;};template<class R, class... ArgTypes> function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;template<class F> function(F) -> function<_see below_>;template<class R, class... ArgTypes> bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;template<class R, class... ArgTypes> void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;}

The function class template provides polymorphic wrappers that generalize the notion of a function pointer.

Wrappers can store, copy, and call arbitrary callable objects, given acall signature, allowing functions to be first-class objects.

A callable type Fis Lvalue-Callable for argument types ArgTypesand return type Rif the expression_INVOKE_<R>(declval<F&>(), declval<ArgTypes>()...), considered as an unevaluated operand, is well-formed ([func.require]).

The function class template is acall wrapper whose call signatureis R(ArgTypes...).

[Note 1:

The types deduced by the deduction guides for functionmight change in future revisions of C++.

— _end note_]

20.14.17.3.2 Constructors and destructor [func.wrap.func.con]

function(nullptr_t) noexcept;

function(const function& f);

Postconditions: !*this if !f; otherwise,*this targets a copy of f.target().

Throws: Nothing if f's target is a specialization of reference_­wrapper or a function pointer.

Otherwise, may throw bad_­allocor any exception thrown by the copy constructor of the stored callable object.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, wheref's target is an object holding only a pointer or reference to an object and a member function pointer.

function(function&& f) noexcept;

Postconditions: If !f, *this has no target; otherwise, the target of *this is equivalent to the target of f before the construction, andf is in a valid state with an unspecified value.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f's target is an object holding only a pointer or reference to an object and a member function pointer.

template<class F> function(F f);

Constraints: F is Lvalue-Callable ([func.wrap.func]) for argument typesArgTypes... and return type R.

Preconditions: F meets the Cpp17CopyConstructible requirements.

Postconditions: !*this if any of the following hold:

Otherwise, *this targets a copy of finitialized with std​::​move(f).

Throws: Nothing if f is a specialization of reference_­wrapper or a function pointer.

Otherwise, may throw bad_­allocor any exception thrown by F's copy or move constructor.

Recommended practice: Implementations should avoid the use of dynamically allocated memory for small callable objects, for example, where f is an object holding only a pointer or reference to an object and a member function pointer.

template<class F> function(F) -> function<_see below_>;

Constraints: &F​::​operator() is well-formed when treated as an unevaluated operand anddecltype(​&F​::​operator()) is of the formR(G​::​*)(A...) cv & noexceptfor a class type G.

Remarks: The deduced type is function<R(A...)>.

[Example 1: void f() { int i{5}; function g = [&](double) { return i; }; } — _end example_]

function& operator=(const function& f);

Effects: As if by function(f).swap(*this);

function& operator=(function&& f);

Effects: Replaces the target of *thiswith the target of f.

function& operator=(nullptr_t) noexcept;

Effects: If *this != nullptr, destroys the target of this.

Postconditions: !(*this).

template<class F> function& operator=(F&& f);

Constraints: decay_­t<F> is Lvalue-Callable ([func.wrap.func]) for argument types ArgTypes... and return type R.

Effects: As if by: function(std​::​forward<F>(f)).swap(*this);

template<class F> function& operator=(reference_wrapper<F> f) noexcept;

Effects: As if by: function(f).swap(*this);

Effects: If *this != nullptr, destroys the target of this.

20.14.17.3.3 Modifiers [func.wrap.func.mod]

void swap(function& other) noexcept;

Effects: Interchanges the targets of *this and other.

20.14.17.3.4 Capacity [func.wrap.func.cap]

explicit operator bool() const noexcept;

Returns: true if *this has a target, otherwise false.

20.14.17.3.5 Invocation [func.wrap.func.inv]

R operator()(ArgTypes... args) const;

Returns: INVOKE<R>(f, std​::​forward<ArgTypes>(args)...) ([func.require]), where f is the target object of *this.

Throws: bad_­function_­call if !*this; otherwise, any exception thrown by the wrapped callable object.

20.14.17.3.6 Target access [func.wrap.func.targ]

const type_info& target_type() const noexcept;

Returns: If *this has a target of type T,typeid(T); otherwise, typeid(void).

template<class T> T* target() noexcept;template<class T> const T* target() const noexcept;

Returns: If target_­type() == typeid(T)a pointer to the stored function target; otherwise a null pointer.

20.14.17.3.7 Null pointer comparison operator functions [func.wrap.func.nullptr]

template<class R, class... ArgTypes> bool operator==(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;

20.14.17.3.8 Specialized algorithms [func.wrap.func.alg]

template<class R, class... ArgTypes> void swap(function<R(ArgTypes...)>& f1, function<R(ArgTypes...)>& f2) noexcept;

Effects: As if by: f1.swap(f2);

20.14.18 Searchers [func.search]

20.14.18.1 General [func.search.general]

Subclause [func.search] provides function object types ([function.objects]) for operations that search for a sequence [pat_­first, pat_­last) in another sequence [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.

Each specialization of a class template specified in [func.search]shall meet the Cpp17CopyConstructible and Cpp17CopyAssignable requirements.

Template parameters named

of templates specified in[func.search] shall meet the same requirements and semantics as specified in [algorithms.general].

Template parameters named Hash shall meet the _Cpp17Hash_requirements (Table 34).

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.

20.14.18.2 Class template default_­searcher [func.search.default]

template<class ForwardIterator1, class BinaryPredicate = equal_to<>> class default_searcher { public: constexpr default_searcher(ForwardIterator1 pat_first, ForwardIterator1 pat_last, BinaryPredicate pred = BinaryPredicate());template<class ForwardIterator2> constexpr pair<ForwardIterator2, ForwardIterator2> operator()(ForwardIterator2 first, ForwardIterator2 last) const;private: ForwardIterator1 pat_first_; ForwardIterator1 pat_last_; BinaryPredicate pred_; };

constexpr default_searcher(ForwardIterator pat_first, ForwardIterator pat_last, BinaryPredicate pred = BinaryPredicate());

Effects: Constructs a default_­searcher object, initializing pat_­first_­with pat_­first, pat_­last_­ with pat_­last, andpred_­ with pred.

Throws: Any exception thrown by the copy constructor of BinaryPredicate orForwardIterator1.

template<class ForwardIterator2> constexpr pair<ForwardIterator2, ForwardIterator2> operator()(ForwardIterator2 first, ForwardIterator2 last) const;

Effects: Returns a pair of iterators i and j such that

20.14.18.3 Class template boyer_­moore_­searcher [func.search.bm]

template<class RandomAccessIterator1,class Hash = hash<typename iterator_traits<RandomAccessIterator1>::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<class RandomAccessIterator2> pair<RandomAccessIterator2, RandomAccessIterator2> operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;private: RandomAccessIterator1 pat_first_; RandomAccessIterator1 pat_last_; Hash hash_; BinaryPredicate pred_; };

boyer_moore_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());

Preconditions: The value type of RandomAccessIterator1 meets the Cpp17DefaultConstructible requirements, the Cpp17CopyConstructible requirements, and the Cpp17CopyAssignable requirements.

Let V be iterator_­traits<RandomAccessIterator1>​::​value_­type.

For any two values A and B of type V, if pred(A, B) == true, then hf(A) == hf(B) is true.

Effects: Initializespat_­first_­ with pat_­first,pat_­last_­ with pat_­last,hash_­ with hf, andpred_­ with pred.

Throws: Any exception thrown by the copy constructor of RandomAccessIterator1, or by the default constructor, copy constructor, or the copy assignment operator of the value type of RandomAccessIterator1, or the copy constructor or operator() of BinaryPredicate or Hash.

May throw bad_­alloc if additional memory needed for internal data structures cannot be allocated.

template<class RandomAccessIterator2> pair<RandomAccessIterator2, RandomAccessIterator2> operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;

Mandates: RandomAccessIterator1 and RandomAccessIterator2have the same value type.

Effects: Finds a subsequence of equal values in a sequence.

Returns: A pair of iterators i and j such that

Returns make_­pair(first, first) if [pat_­first_­, pat_­last_­) is empty, otherwise returns make_­pair(last, last) if no such iterator is found.

Complexity: At most (last - first) * (pat_­last_­ - pat_­first_­) applications of the predicate.

20.14.18.4 Class template boyer_­moore_­horspool_­searcher [func.search.bmh]

template<class RandomAccessIterator1,class Hash = hash<typename iterator_traits<RandomAccessIterator1>::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<class RandomAccessIterator2> pair<RandomAccessIterator2, RandomAccessIterator2> operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;private: RandomAccessIterator1 pat_first_; RandomAccessIterator1 pat_last_; Hash hash_; BinaryPredicate pred_; };

boyer_moore_horspool_searcher(RandomAccessIterator1 pat_first, RandomAccessIterator1 pat_last, Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());

Preconditions: The value type of RandomAccessIterator1 meets the Cpp17DefaultConstructible,Cpp17CopyConstructible, and Cpp17CopyAssignable requirements.

Let V be iterator_­traits<RandomAccessIterator1>​::​value_­type.

For any two values A and B of type V, if pred(A, B) == true, then hf(A) == hf(B) is true.

Effects: Initializespat_­first_­ with pat_­first,pat_­last_­ with pat_­last,hash_­ with hf, andpred_­ with pred.

Throws: Any exception thrown by the copy constructor of RandomAccessIterator1, or by the default constructor, copy constructor, or the copy assignment operator of the value type of RandomAccessIterator1or the copy constructor or operator() of BinaryPredicate or Hash.

May throw bad_­alloc if additional memory needed for internal data structures cannot be allocated.

template<class RandomAccessIterator2> pair<RandomAccessIterator2, RandomAccessIterator2> operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;

Mandates: RandomAccessIterator1 and RandomAccessIterator2have the same value type.

Effects: Finds a subsequence of equal values in a sequence.

Returns: A pair of iterators i and j such that

Returns make_­pair(first, first) if [pat_­first_­, pat_­last_­) is empty, otherwise returns make_­pair(last, last) if no such iterator is found.

Complexity: At most (last - first) * (pat_­last_­ - pat_­first_­) applications of the predicate.

20.14.19 Class template hash [unord.hash]

The unordered associative containers defined in [unord] use specializations of the class template hash ([functional.syn]) as the default hash function.

Each specialization of hash is either enabled or disabled, as described below.

[Note 1:

Enabled specializations meet the Cpp17Hash requirements, and disabled specializations do not.

— _end note_]

Each header that declares the template hashprovides enabled specializations of hash for nullptr_­t and all cv-unqualified arithmetic, enumeration, and pointer types.

For any type Key for which neither the library nor the user provides an explicit or partial specialization of the class template hash,hash<Key> is disabled.

If the library provides an explicit or partial specialization of hash<Key>, that specialization is enabled except as noted otherwise, and its member functions are noexcept except as noted otherwise.

If H is a disabled specialization of hash, these values are false:is_­default_­constructible_­v<H>,is_­copy_­constructible_­v<H>,is_­move_­constructible_­v<H>,is_­copy_­assignable_­v<H>, andis_­move_­assignable_­v<H>.

Disabled specializations of hashare not function object types.

[Note 2:

This means that the specialization of hash exists, but any attempts to use it as a Cpp17Hash will be ill-formed.

— _end note_]

An enabled specialization hash<Key> will: