...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::reference_wrapper — Contains a reference to an object of type T
.
// In header: <boost/core/ref.hpp> template<typename T> class reference_wrapper { public: // types typedef T type; // public member functions explicit reference_wrapper(T &) noexcept; reference_wrapper(T &&) = delete; template<typename Y, typename = unspecified> reference_wrapper(reference_wrapper< Y >) noexcept; operator T&() const noexcept; T & get() const noexcept; T * get_pointer() const noexcept; };
reference_wrapper
is primarily used to "feed" references to function templates (algorithms) that take their parameter by value. It provides an implicit conversion to T&
, which usually allows the function templates to work on references unmodified.
reference_wrapper
public member functionsexplicit reference_wrapper(T & t) noexcept;
Constructs a reference_wrapper
object that stores a reference to t
.
Note | |
---|---|
Does not throw. |
reference_wrapper(T && t) = delete;
Note | |
---|---|
Construction from a temporary object is disabled. |
template<typename Y, typename = unspecified> reference_wrapper(reference_wrapper< Y > r) noexcept;
Constructs a reference_wrapper
object that stores the reference stored in the compatible reference_wrapper
r
.
Note | |
---|---|
Only enabled when |
Note | |
---|---|
Does not throw. |
operator T&() const noexcept;
Note | |
---|---|
Does not throw. |
Returns: |
The stored reference. |
T & get() const noexcept;
Note | |
---|---|
Does not throw. |
Returns: |
The stored reference. |
T * get_pointer() const noexcept;
Note | |
---|---|
Does not throw. |
Returns: |
A pointer to the object referenced by the stored reference. |