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

13 KiB
Raw Blame History

[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;}

1

#

reference_wrapper is a Cpp17CopyConstructible and Cpp17CopyAssignable wrapper around a reference to an object or function of type T.

2

#

reference_wrapper is a trivially copyable type ([basic.types.general]).

3

#

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);

1

#

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

2

#

Constraints: The expression FUN(declval()) is well-formed andis_same_v<remove_cvref_t, reference_wrapper> is false.

3

#

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.

4

#

Remarks: The exception specification is equivalent tonoexcept(FUN(declval())).

🔗

constexpr reference_wrapper(const reference_wrapper& x) noexcept;

5

#

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;

1

#

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

22.10.6.4 Access [refwrap.access]

🔗

constexpr operator T& () const noexcept;

1

#

Returns: The stored reference.

🔗

constexpr T& get() const noexcept;

2

#

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...>);

1

#

Mandates: T is a complete type.

2

#

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);

1

#

Constraints: The expression x.get() == y.get() is well-formed and its result is convertible to bool.

2

#

Returns: x.get() == y.get().

🔗

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

3

#

Constraints: The expression x.get() == y is well-formed and its result is convertible to bool.

4

#

Returns: x.get() == y.

🔗

friend constexpr bool operator==(reference_wrapper x, reference_wrapper<const T> y);

5

#

Constraints: is_const_v is false and the expression x.get() == y.get() is well-formed and its result is convertible to bool.

6

#

Returns: x.get() == y.get().

🔗

friend constexpr auto operator<=>(reference_wrapper x, reference_wrapper y);

7

#

Constraints: The expression synth-three-way(x.get(), y.get()) is well-formed.

8

#

Returns: synth-three-way(x.get(), y.get()).

🔗

friend constexpr auto operator<=>(reference_wrapper x, const T& y);

9

#

Constraints: The expression synth-three-way(x.get(), y) is well-formed.

10

#

Returns: synth-three-way(x.get(), y).

🔗

friend constexpr auto operator<=>(reference_wrapper x, reference_wrapper<const T> y);

11

#

Constraints: is_const_v is false.

The expression synth-three-way(x.get(), y.get()) is well-formed.

12

#

Returns: synth-three-way(x.get(), y.get()).

22.10.6.7 Helper functions [refwrap.helpers]

1

#

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;

2

#

Returns: reference_wrapper(t).

🔗

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

3

#

Returns: t.

🔗

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

4

#

Returns: reference_wrapper(t).

🔗

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

5

#

Returns: t.

🔗

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> &&ref-wrap-common-reference-exists-with<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> &&ref-wrap-common-reference-exists-with<T, R, TQual, RQual>)struct basic_common_reference<T, R, TQual, RQual> {using type = common_reference_t<typename R::type&, TQual>; };}