The ref Library provides the template factory functions boost: ref and boost: cref in boost/ref. hpp, which correspond to the packaging references and common references respectively. If you need to copy object parameters in some cases, you can select ref if the object cannot be copied or the copy cost is too high. [Cpp] # pragma once # include <boost/ref. hpp> # include <boost/assert. hpp> # include <string> # include <iostream> # include <conio. h ># include <vector> using namespace std; using namespace boost; // boost. the ref application proxy mode introduces the reference_wrapper concept of object reference to solve such problems (generally, it is feasible to pass the value semantics, but there are also many special situations, the copy cost of the function object as a parameter is too high, or you do not want to copy the object, or even copy is not feasible (single piece) void test1 () {int x = 10; reference_wrapper <int> rw (x); // wrap the reference assert (x = rw) of the int type; // stealth Conversion Convert to int type (int &) rw = 100; // display to int & type, used for the left value reference_wrapper <int> rw2 (rw ); // copy the constructor assert (rw2.get () = 100); string str; reference_wrapper <string> rws (str); // reference the wrapped string * rws. get_pointer () = "test reference_wrapper"; // cout <"str:" <str <"size:" <rws. get (). size () <endl; // str: test reference_wrapper size: 22 // reference_wrapper usage is similar to that of the reference type (T &) in C ++ &), it is like the alias of the encapsulated object. Therefore, the value assignment must be initialized, just like a variable of the reference type. It provides get () and get_pointer () to return the reference and pointer of the storage respectively.} // The reference_wrapper name is too long and it is inconvenient to declare reference to the packaging object. Therefore, the ref Library provides two convenient factory functions: ref () and cref () (const ref ()), you can easily construct a reference_wrapper object by using the parameter type. Void test2 () {// vector <int> v (10, 2); // BOOST_AUTO (rw, cref (v); // assert (is_reference_wrapper <BOOST_TYPEOF (rw)>:: value); // assert (! Is_reference_wrapper <BOOST_TYPEOF (v) >:: value); // string str; // BOOST_AUTO (rws, ref (str )); // cout <typeid (unwrap_reference <BOOST_TYPEOF <rws>: type ). name () <endl; // cout <typeid (unwrap_reference <BOOST_TYPEOF <str >:: type ). name () <endl ;}# include <iostream >#include <vector> # include <algorithm> # include <boost/bind. hpp> # include <boost/function. hpp> void print (std: ostream & OS, int I) {OS <I <std: Endl;} void test3 () {// std: cout is the standard output object and cannot be copied. Therefore, boost :: ref to bind its reference. Otherwise, the system prompts that the copy structure failed because the copy structure is private. Boost: function <void (int)> pt = boost: bind (print, boost: ref (std: cout), _ 1 ); vector <int> v (1, 1); v. push_back (2); v. push_back (3); v. push_back (4); std: for_each (v. begin (), v. end (), pt); // 1 2 3 4} void test (char t) {cout <"press key =" <t <endl; switch (t) {case '1': test1 (); break; case '2': test2 (); break; case '3': test3 (); break; // case '4': test4 (); break; case 27: www.2cto.com case 'q': exit (0); break; default: cout <"default" <t <endl; break ;}int main () {while (1) {test (getch () ;}return 0 ;}