Proposal: Allow std::ref, std::cref and std::reference_wrapper

55 views
Skip to first unread message

jdoe...@chromium.org

unread,
Feb 22, 2019, 5:57:21 AM2/22/19
to cxx
Hi all,

I would like to propose moving std::reference_wrapper [1], std::ref and std::cref [2] in the allowed library features section. Quoting from the initial standardisation proposal [3], "reference_wrapper<T> is a CopyConstructible and Assignable wrapper around a reference to an object of type T. It provides an implicit conversion to T&, often allowing the function templates to work on references unmodified." For example, this allows usage of reference like objects in standard library containers such as std::vector or std::optional, or allows std::make_tuple to create a tuple holding a reference. Without these libraries features this would not be possible [4].

std::ref and std::cref are convenience functions that create std::reference_wrappers of type T or const T, respectively. While their usage should be rare, allowing them would make it possible to get rid of base::ConstRef [5] and base::internal::ConstRefWrapper [6], which are effectively our handrolled implementations of std::cref and std::reference_wrapper<const T>.

Let me know what you think!

Cheers,
Jan

Peter Kasting

unread,
Feb 22, 2019, 6:04:02 AM2/22/19
to Jan Wilken, cxx
On Fri, Feb 22, 2019 at 2:57 AM <jdoe...@chromium.org> wrote:
std::ref and std::cref are convenience functions that create std::reference_wrappers of type T or const T, respectively. While their usage should be rare, allowing them would make it possible to get rid of base::ConstRef [5] and base::internal::ConstRefWrapper [6], which are effectively our handrolled implementations of std::cref and std::reference_wrapper<const T>.

I'm supportive of any proposal that replaces a Chromium-specific object with a standardized one of equivalent functionality.

I don't think we need to qualify in the features table as "usage should be rare"; while that's true in practice as you note, I think of that note as implying "this probably isn't what you want and you should think twice about using it", and I doubt we need to discourage usage of this (since few people will try anyway).

PK

Nico Weber

unread,
Feb 22, 2019, 8:17:56 AM2/22/19
to Peter Kasting, Jan Wilken, cxx
+1 to everything pkasting said :)
 

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAE_u8nm3S4RThRzTZWUh0vn7biyd0EOsduCsT%3DAs2VNQ%40mail.gmail.com.

Jan Wilken Dörrie

unread,
Feb 22, 2019, 8:37:08 AM2/22/19
to Nico Weber, Peter Kasting, cxx
Thanks both. I went ahead and created a CL doing the style guide change and modifying base::ConstRef to make use of std::reference_wrapper. Longer term we should replace all usages of base::ConstRef with std::cref and eventually delete base::ConstRef completely. However, that is definitely out of scope for the initial CL.

On Fri, Feb 22, 2019 at 2:18 PM Nico Weber <tha...@chromium.org> wrote:
On Fri, Feb 22, 2019 at 6:04 AM 'Peter Kasting' via cxx <c...@chromium.org> wrote:
On Fri, Feb 22, 2019 at 2:57 AM <jdoe...@chromium.org> wrote:
std::ref and std::cref are convenience functions that create std::reference_wrappers of type T or const T, respectively. While their usage should be rare, allowing them would make it possible to get rid of base::ConstRef [5] and base::internal::ConstRefWrapper [6], which are effectively our handrolled implementations of std::cref and std::reference_wrapper<const T>.

I'm supportive of any proposal that replaces a Chromium-specific object with a standardized one of equivalent functionality.

I don't think we need to qualify in the features table as "usage should be rare"; while that's true in practice as you note, I think of that note as implying "this probably isn't what you want and you should think twice about using it", and I doubt we need to discourage usage of this (since few people will try anyway).

Right, I should have made this more clear. I didn't intend to discourage using these features, but just expecting that their usage will be low, given that they address rather niche usecases.
 

+1 to everything pkasting said :)
 

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
--

Jan Wilken Dörrie

Software Engineer

jdoe...@google.com
 +49 89 839300973


Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


Diese E-Mail ist vertraulich. Falls sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde.

    

This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.

Jan Wilken Dörrie

unread,
Mar 12, 2019, 8:28:09 AM3/12/19
to Nico Weber, Peter Kasting, cxx
Closing the loop on this: r635481 moved std::reference_wrapper, std::ref() and std::cref() in the allowed section of the style guide, while r637663 deleted all occurrences of base::ConstRef in favor of std::cref.

Cheers,
Jan
Reply all
Reply to author
Forward
0 new messages