Commit c79c2fa2 authored by jdoerrie's avatar jdoerrie Committed by Commit Bot

[styleguide] Allow std::reference_wrapper, std::ref and std::cref

This change moves the library features std::reference_wrapper, std::ref
and std::cref from the to be discussed section to the allowed section,
adds further documentation and links to the cxx@ discussion thread.

Furthermore, it removes base::internal::ConstRefWrapper and modifies
base::ConstRef to construct std::reference_wrappers instead.

As a follow up to this change all usages of base::ConstRef should be
replaced by std::cref, and base::ConstRef should be removed as well.

Bug: 934808
Change-Id: Ie09884c38049a0f69bb3cb6442907c699e29da2e
Reviewed-on: https://chromium-review.googlesource.com/c/1482958
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635481}
parent a5c326e4
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef BASE_BIND_H_ #ifndef BASE_BIND_H_
#define BASE_BIND_H_ #define BASE_BIND_H_
#include <functional>
#include <utility> #include <utility>
#include "base/bind_internal.h" #include "base/bind_internal.h"
...@@ -356,8 +357,8 @@ static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) { ...@@ -356,8 +357,8 @@ static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) {
// Note that because ConstRef() takes a reference on |n|, |n| must outlive all // Note that because ConstRef() takes a reference on |n|, |n| must outlive all
// its bound callbacks. // its bound callbacks.
template <typename T> template <typename T>
static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { inline std::reference_wrapper<const T> ConstRef(const T& o) {
return internal::ConstRefWrapper<T>(o); return std::cref(o);
} }
// Owned() transfers ownership of an object to the Callback resulting from // Owned() transfers ownership of an object to the Callback resulting from
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stddef.h> #include <stddef.h>
#include <functional>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
...@@ -83,16 +84,6 @@ class UnretainedWrapper { ...@@ -83,16 +84,6 @@ class UnretainedWrapper {
T* ptr_; T* ptr_;
}; };
template <typename T>
class ConstRefWrapper {
public:
explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
const T& get() const { return *ptr_; }
private:
const T* ptr_;
};
template <typename T> template <typename T>
class RetainedRefWrapper { class RetainedRefWrapper {
public: public:
...@@ -941,7 +932,7 @@ template <typename T> ...@@ -941,7 +932,7 @@ template <typename T>
struct IsWeakReceiver : std::false_type {}; struct IsWeakReceiver : std::false_type {};
template <typename T> template <typename T>
struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; struct IsWeakReceiver<std::reference_wrapper<T>> : IsWeakReceiver<T> {};
template <typename T> template <typename T>
struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; struct IsWeakReceiver<WeakPtr<T>> : std::true_type {};
...@@ -963,10 +954,8 @@ struct BindUnwrapTraits<internal::UnretainedWrapper<T>> { ...@@ -963,10 +954,8 @@ struct BindUnwrapTraits<internal::UnretainedWrapper<T>> {
}; };
template <typename T> template <typename T>
struct BindUnwrapTraits<internal::ConstRefWrapper<T>> { struct BindUnwrapTraits<std::reference_wrapper<T>> {
static const T& Unwrap(const internal::ConstRefWrapper<T>& o) { static T& Unwrap(std::reference_wrapper<T> o) { return o.get(); }
return o.get();
}
}; };
template <typename T> template <typename T>
......
...@@ -597,6 +597,14 @@ template &lt;typename T&gt;<br/>void Function(T&amp;&amp; t) { ... }</code></td> ...@@ -597,6 +597,14 @@ template &lt;typename T&gt;<br/>void Function(T&amp;&amp; t) { ... }</code></td>
<td>Because the standard does not define precisely how the <code><i>xxx</i>_distribution</code> objects generate output, the same object may produce different output for the same seed across platforms, or even across different versions of the STL. Do not use these objects in any scenario where behavioral consistency is required. <a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/MLgK9vCE4BA">Discussion thread</a></td> <td>Because the standard does not define precisely how the <code><i>xxx</i>_distribution</code> objects generate output, the same object may produce different output for the same seed across platforms, or even across different versions of the STL. Do not use these objects in any scenario where behavioral consistency is required. <a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/MLgK9vCE4BA">Discussion thread</a></td>
</tr> </tr>
<tr>
<td>Reference Wrapper Classes</td>
<td><code>std::reference_wrapper</code> and <code>std::ref()</code>, <code>std::cref()</code></td>
<td>Allows you to wrap a reference within a standard object (and use those within containers)</td>
<td><a href="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper">std::reference_wrapper</a>, <a href="http://en.cppreference.com/w/cpp/utility/functional/ref">std::ref, std::cref</a></td>
<td><a href="https://groups.google.com/a/chromium.org/d/msg/cxx/oVdPIhPji3E/sr4Dv0geAgAJ">Discussion thread</a></td>
</tr>
<tr> <tr>
<td>String Direct Reference Functions</td> <td>String Direct Reference Functions</td>
<td><code>std::string::front()</code>, <code>std::string::back()</code></td> <td><code>std::string::front()</code>, <code>std::string::back()</code></td>
...@@ -1131,14 +1139,6 @@ something, remove all callers and remove the function instead.</td> ...@@ -1131,14 +1139,6 @@ something, remove all callers and remove the function instead.</td>
<td>Useful for determining the element type pointed at by a (possibly smart) pointer.</td> <td>Useful for determining the element type pointed at by a (possibly smart) pointer.</td>
</tr> </tr>
<tr>
<td>Reference Wrapper Classes</td>
<td><code>std::reference_wrapper</code> and <code>std::ref()</code>, <code>std::cref()</code></td>
<td>Allows you to wrap a reference within a standard object (and use those within containers)</td>
<td><a href="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper">std::reference_wrapper</a></td>
<td></td>
</tr>
<tr> <tr>
<td>Soft Program Exits</td> <td>Soft Program Exits</td>
<td><code>std::at_quick_exit()</code>, <code>std::quick_exit()</code></td> <td><code>std::at_quick_exit()</code>, <code>std::quick_exit()</code></td>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment