3

Since C++11, std::reference_wrapper is a small "shim" template that is a class type that is constructible from and convertible to a reference type. It can be used in generic containers which might not otherwise support references.

https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper

std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references.

This standard library feature was deprecated in C++17 and is removed in the current draft of C++20. Why?

Is std::reference_wrapper unsafe to use or defective in some way?

In http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0619r3.html#2.0 it appears that this is part considered part of "D.8 Old adaptable function bindings" and the text in the standard describing std::reference_wrapper is crossed off in section "D.9.2 Typedefs to Support Function Binders [depr.func.adaptor.typedefs]"

It appears that we are removing it because of a role it played in an old function binder API, but it actually has additional uses in containers, as described on the reference page. Is there something I'm missing that replaces that use-case, or something else that I missed about this situation?

If this useful feature is being removed, should we implement it when we need it, or is there some reason that this whole pattern is unsafe?

Chris Beck
  • 15,614
  • 4
  • 51
  • 87
  • 11
    The only thing deprecated and removed are the member typedefs. – T.C. Aug 08 '18 at 03:09
  • thanks, since this question is a simple misunderstanding, do you think i should close it, delete it, or leave it? – Chris Beck Aug 08 '18 at 03:14
  • @ChrisBeck If you had this misunderstanding, then it's not impossible that others will also have this misunderstanding. It may be appropriate to keep it. – NetherGranite Sep 04 '18 at 11:29

1 Answers1

15

This standard library feature was deprecated in C++17 and is removed in the current draft of C++20.

It wasn't deprecated and it's not removed. In fact, you can find it under [refwrap] in the latest draft.

Ah, you're misunderstand the wording that was removed. The "old adaptable function bindings" were under [refwrap] (and others), but they were deprecated, so they were moved to Appendix D. This means that the specification of std::reference_wrapper was split!

You can see this in action from the D9.2p2 of your linked paper. It says:

The following member names are defined in addition to names specified in Clause 23.14:

Those binders were additional members. Their removal has no impact on the other part of the specification of reference_wrapper.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162