Chapter 8. The reference_wrapper Class Template

These are only aliases. Their real names areStuhldreher, Miller, Crowley, and Layden.

— Story on Notre Dame football victory over ArmyGRANTLAND RICE

The class template reference_wrapper creates objects that act like references but can be copied. Ordinary references can’t be copied.

Example 8.1. Copying References (funobjref/refcopy.cpp)

#include <functional>using std::tr1::reference_wrapper;class ref  { // simple class containing referencepublic :  ref(int& i) : member(i) {}private :  int& member;  };class refwrap  { // simple class containing reference_wrapperpublic :  refwrap(int& i) : member(i) {}private :  reference_wrapper<int> member;  };void f()  { // demonstrate copying  int i, j;  ref r0(i); ...

Get The C++ Standard Library Extensions A Tutorial and Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.