Boost::ref

Source: Internet
Author: User

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::refandboost::crefThat return instances ofboost::reference_wrapper<T>, a functionboost::unwrap_refThat 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)whereXis 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.