Chapter 7. Boost.Ref

| Home | Libraries | People | FAQ | More

## Chapter 7. Boost.Ref

### Jaakko Järvi

### Peter Dimov

### Douglas Gregor

### Dave Abrahams

Copyright © 1999, 2000 Jaakko Järvi

Copyright © 2001, 2002 Peter Dimov

Copyright © 2002 David Abrahams

Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.

### Introduction

The Ref library is a small library that is useful for passing references to function templates (algorithms) that would usually take copies of their arguments. It defines the class template `boost::reference_wrapper<T>`, the two functions `boost::ref` and `boost::cref` that return instances of `boost::reference_wrapper<T>`, and the two traits classes `boost::is_reference_wrapper<T>` and `boost::unwrap_reference<T>`.

The purpose of `boost::reference_wrapper<T>` is to contain a reference to an object of type T. It is primarily used to "feed" references to function templates (algorithms) that take their parameter by value.

To support this usage, `boost::reference_wrapper<T>` provides an 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 are not Assignable).

The expression `boost::ref(x)` returns a `boost::reference_wrapper<X>(x)` where X is the type of x. Similarly, `boost::cref(x)` returns a `boost::reference_wrapper<X const>(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` is T::type if T is a `reference_wrapper`, T otherwise.

Last revised: July 09, 2004 at 00:56:42 GMT |