# Header boost/utility.hpp

The entire contents of the header `<boost/utility.hpp>` are in `namespace boost`.

## Contents

- Class templates supporting the base-from-member idiom
- Function templates checked_delete() and checked_array_delete()
- Function templates next() and prior()
- Class noncopyable
- Function template addressof()
- Function template tie() and supporting class tied.

## Function templates checked_delete() and checked_array_delete()

See separate documentation.

## Function templates next() and prior()

Certain data types, such as the C++ Standard Library's forward and bidirectional iterators, do not provide addition and subtraction via operator+() or operator-(). This means that non-modifying computation of the next or prior value requires a temporary, even though operator++() or operator--() is provided. It also means that writing code like `itr+1` inside a template restricts the iterator category to random access iterators.

The next() and prior() functions provide a simple way around these problems:

>     template <class T>
>     T next(T x) { return ++x; }

>     template <class X>
>     T prior(T x) { return --x; }

Usage is simple:

>     const std::list<T>::iterator p = get_some_iterator();
>     const std::list<T>::iterator prev = boost::prior(p);

Contributed by Dave Abrahams.

## Class noncopyable

Class noncopyable is a base class. Derive your own class from noncopyable when you want to prohibit copy construction and copy assignment.

Some objects, particularly those which hold complex resources like files or network connections, have no sensible copy semantics. Sometimes there are possible copy semantics, but these would be of very limited usefulness and be very difficult to implement correctly. Sometimes you're implementing a class that doesn't need to be copied just yet and you don't want to take the time to write the appropriate functions. Deriving from noncopyable will prevent the otherwise implicitly-generated functions (which don't have the proper semantics) from becoming a trap for other programmers.

The traditional way to deal with these is to declare a private copy constructor and copy assignment, and then document why this is done. But deriving from noncopyable is simpler and clearer, and doesn't require additional documentation.

The program noncopyable_test.cpp can be used to verify class noncopyable works as expected. It has have been run successfully under GCC 2.95, Metrowerks CodeWarrior 5.0, and Microsoft Visual C++ 6.0 sp 3.

Contributed by Dave Abrahams.

### Example

>     // inside one of your own headers ...
>     #include <boost/utility.hpp>

>     class ResourceLadenFileSystem : boost::noncopyable {
>     ...

### Rationale

Class noncopyable has protected constructor and destructor members to emphasize that it is to be used only as a base class. Dave Abrahams notes concern about the effect on compiler optimization of adding (even trivial inline) destructor declarations. He says "Probably this concern is misplaced, because noncopyable will be used mostly for classes which own resources and thus have non-trivial destruction semantics."

## Function template addressof()

Function addressof() returns the address of an object.

>     template <typename T> inline T*                addressof(T& v);
>     template <typename T> inline const T*          addressof(const T& v);
>     template <typename T> inline volatile T*       addressof(volatile T& v);
>     template <typename T> inline const volatile T* addressof(const volatile T& v);

C++ allows programmers to replace the unary operator&() class member used to get the address of an object. Getting the real address of an object requires ugly casting tricks to avoid invoking the overloaded operator&(). Function addressof() provides a wrapper around the necessary code to make it easy to get an object's real address.

The program addressof_test.cpp can be used to verify that addressof() works as expected.

Contributed by Brad King based on ideas from discussion with Doug Gregor.

### Example

>     #include <boost/utility.hpp>

>     struct useless_type {};
>     class nonaddressable {
>       useless_type operator&() const;
>     };

>     void f() {
>       nonaddressable x;
>       nonaddressable* xp = boost::addressof(x);
>       // nonaddressable* xpe = &x; /* error */
>     }

## Class templates for the Base-from-Member Idiom

See separate documentation.

## Function template tie()

See separate documentation.

Revised 09 January, 2003

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