13 KiB
[refwrap]
22 General utilities library [utilities]
22.10 Function objects [function.objects]
22.10.6 Class template reference_wrapper [refwrap]
22.10.6.1 General [refwrap.general]
namespace std {template class reference_wrapper {public:// typesusing type = T; // [refwrap.const], constructorstemplateconstexpr reference_wrapper(U&&) noexcept(see below); constexpr reference_wrapper(const reference_wrapper& x) noexcept; // [refwrap.assign], assignmentconstexpr reference_wrapper& operator=(const reference_wrapper& x) noexcept; // [refwrap.access], accessconstexpr operator T& () const noexcept; constexpr T& get() const noexcept; // [refwrap.invoke], invocationtemplate<class... ArgTypes>constexpr invoke_result_t<T&, ArgTypes...> operator()(ArgTypes&&...) constnoexcept(is_nothrow_invocable_v<T&, ArgTypes...>); // [refwrap.comparisons], comparisonsfriend constexpr bool operator==(reference_wrapper, reference_wrapper); friend constexpr bool operator==(reference_wrapper, const T&); friend constexpr bool operator==(reference_wrapper, reference_wrapper); friend constexpr auto operator<=>(reference_wrapper, reference_wrapper); friend constexpr auto operator<=>(reference_wrapper, const T&); friend constexpr auto operator<=>(reference_wrapper, reference_wrapper); }; template reference_wrapper(T&) -> reference_wrapper;}
reference_wrapper is a Cpp17CopyConstructible and Cpp17CopyAssignable wrapper around a reference to an object or function of type T.
reference_wrapper is a trivially copyable type ([basic.types.general]).
The template parameter T of reference_wrapper may be an incomplete type.
[Note 1:
Using the comparison operators described in [refwrap.comparisons] with T being an incomplete type can lead to an ill-formed program with no diagnostic required ([temp.point], [temp.constr.atomic]).
â end note]
22.10.6.2 Constructors [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()) is well-formed andis_same_v<remove_cvref_t, reference_wrapper> is false.
Effects: Creates a variable r as if by T& r = std::forward(u), then constructs a reference_wrapper object that stores a reference to r.
Remarks: The exception specification is equivalent tonoexcept(FUN(declval())).
constexpr reference_wrapper(const reference_wrapper& x) noexcept;
Effects: Constructs a reference_wrapper object that stores a reference to x.get().
22.10.6.3 Assignment [refwrap.assign]
constexpr reference_wrapper& operator=(const reference_wrapper& x) noexcept;
Postconditions: *this stores a reference to x.get().
22.10.6.4 Access [refwrap.access]
constexpr operator T& () const noexcept;
Returns: The stored reference.
constexpr T& get() const noexcept;
Returns: The stored reference.
22.10.6.5 Invocation [refwrap.invoke]
template<class... ArgTypes> constexpr invoke_result_t<T&, ArgTypes...> operator()(ArgTypes&&... args) const noexcept(is_nothrow_invocable_v<T&, ArgTypes...>);
Mandates: T is a complete type.
Returns: INVOKE(get(), std::forward(args)...) ([func.require]).
22.10.6.6 Comparisons [refwrap.comparisons]
friend constexpr bool operator==(reference_wrapper x, reference_wrapper y);
Constraints: The expression x.get() == y.get() is well-formed and its result is convertible to bool.
Returns: x.get() == y.get().
friend constexpr bool operator==(reference_wrapper x, const T& y);
Constraints: The expression x.get() == y is well-formed and its result is convertible to bool.
Returns: x.get() == y.
friend constexpr bool operator==(reference_wrapper x, reference_wrapper<const T> y);
Constraints: is_const_v is false and the expression x.get() == y.get() is well-formed and its result is convertible to bool.
Returns: x.get() == y.get().
friend constexpr auto operator<=>(reference_wrapper x, reference_wrapper y);
Constraints: The expression synth-three-way(x.get(), y.get()) is well-formed.
Returns: synth-three-way(x.get(), y.get()).
friend constexpr auto operator<=>(reference_wrapper x, const T& y);
Constraints: The expression synth-three-way(x.get(), y) is well-formed.
Returns: synth-three-way(x.get(), y).
friend constexpr auto operator<=>(reference_wrapper x, reference_wrapper<const T> y);
Constraints: is_const_v is false.
The expression synth-three-way(x.get(), y.get()) is well-formed.
Returns: synth-three-way(x.get(), y.get()).
22.10.6.7 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).
template<class T> constexpr reference_wrapper<T> ref(reference_wrapper<T> t) noexcept;
Returns: t.
template<class T> constexpr reference_wrapper<const T> cref(const T& t) noexcept;
Returns: reference_wrapper(t).
template<class T> constexpr reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Returns: t.
22.10.6.8 common_reference related specializations [refwrap.common.ref]
namespace std {templateconstexpr bool is-ref-wrapper = false; // exposition onlytemplateconstexpr bool is-ref-wrapper<reference_wrapper> = true; template<class R, class T, class RQ, class TQ>concept ref-wrap-common-reference-exists-with = // exposition only**is-ref-wrapper &&requires { typename common_reference_t<typename R::type&, TQ>; } &&convertible_to<RQ, common_reference_t<typename R::type&, TQ>>; template<class R, class T, template class RQual, template class TQual>requires (ref-wrap-common-reference-exists-with<R, T, RQual, TQual> &&<T, R, TQual, RQual>)struct basic_common_reference<R, T, RQual, TQual> {using type = common_reference_t<typename R::type&, TQual>; }; template<class T, class R, template class TQual, template class RQual>requires (ref-wrap-common-reference-exists-with<R, T, RQual, TQual> &&
<T, R, TQual, RQual>)struct basic_common_reference<T, R, TQual, RQual> {using type = common_reference_t<typename R::type&, TQual>; };}