In the case of C + + algorithms and functions, it is very common to use class objects as parameters, in general, the value of the transfer semantics is feasible, but there are many special cases, the copy of the function object as a parameter is too expensive (with complex internal state ), or you do not want to copy the object ( The internal state should not be changed), or even a copy of the shell is not feasible (noncopyable, single piece ).
Boost.ref applies the proxy mode and introduces the wrapper concept of object reference to solve this problem. It is in the namespace boost, in order to use the REF component
Http://www.boost.org/doc/libs/1_57_0/libs/core/doc/html/core/ref.html
The REF library is a small library that's useful for passing references to function templates (algorithms) that would USU Ally take copies of their arguments. It defines the class templateboost::reference_wrapper<T>
, functionsboost::ref
andboost::cref
That return instances ofboost::reference_wrapper<T>
, a functionboost::unwrap_ref
That unwraps aboost::reference_wrapper<T>
or returns a reference to any other type of object, and the traits classesboost::is_reference_wrapper<T>
andboost::unwrap_reference<T>
.
The purpose of boost::reference_wrapper<t
is to contain a reference to an object of Type t
. It is the primarily used to "feeds" references to function templates (algorithms), which take their parameter by value.
To boost::reference_wrapper<T>
provides a implicit conversion to T&
. This usually allows the function templates to work on references unmodified.
boost::reference_wrapper<T>
is both copyconstructible and assignable (ordinary references was not assignable).
Theexpression boost::ref(x)
Returns aboost::reference_wrapper<X>(x)
whereX
is the type ofx
. Similarly,boost::cref(x)
Returns aboost::reference_wrapper<X const>(x)
.
The Expression boost::unwrap_ref (x)
returns a boost::unwrap_reference <x>::type< Span class= "Special" >&
where x
is the type Of x
.
The Expression boost::is_reference_wrapper<t >::value
is true
if t
is a reference_wrapper
, And false
otherwise.
The Type-expression boost::unwrap_reference<T>::type
T::type
is an if T
reference_wrapper
is a, T
otherwise.
Referenceheader <boost/core/ref.hpp>
NamespaceBoost{Template<TypeName T>Class Reference_wrapper;Template<TypeName T>struct Is_reference_wrapper;Template<TypeName T>struct unwrap_reference;Template<TypeName T> Reference_wrapper<T>Const REF(T&);Template<TypeName T> Reference_wrapper<TConst>Const CREF(TConst&);Template<TypeName T>void ref(TConst&&);Template<TypeName T> void cref(T const &&); Template<typename T> Unwrap_reference< t >::type & Unwrap_ref(T &);}
http://blog.csdn.net/zengraoli/article/details/9663057
Http://www.cnblogs.com/rocketfan/archive/2010/12/28/1918529.html
Boost::ref