| Home | Libraries | People | FAQ | More

## Class template old_ptr_if_copyable

boost::contract::old_ptr_if_copyable — Old value pointer that does not require the pointed old value type to be copyable.

## Synopsis

    // In header: <boost/contract/old.hpp>

    template<typename T>
    class old_ptr_if_copyable {
    public:
      // types
      typedef T element_type;  // Pointed old value type.

      // public member functions
      old_ptr_if_copyable();
      old_ptr_if_copyable(old_ptr< T > const &);
      T const & operator*() const;
      T const * operator->() const;
      explicit operator bool() const;
    };

## Description

This pointer can be set to point to an actual old value copy using either `BOOST_CONTRACT_OLDOF` or `boost::contract::make_old`:

    template<typename T> // Type `T` might or not be copyable.
    class u {
    public:
        virtual void f(..., boost::contract::virtual_* v = 0) {
            boost::contract::old_ptr_if_copyable<T> old_var =
                    BOOST_CONTRACT_OLDOF(v, old_expr);
            ...
                if(old_var) ... // Always null for non-copyable types.
            ...
        }

        ...
    };

See Also: Old Value Requirements

### Template Parameters

1.     typename T

   Type of the pointed old value. If this type is not copyable (i.e., `boost::contract::is_old_value_copyable<T>::value` is `false`), this pointer will always be null (but this library will not generate a compile-time error when this pointer is dereferenced).

### `old_ptr_if_copyable` public member functions

1.     old_ptr_if_copyable();

   Construct this old value pointer as null.

2.     old_ptr_if_copyable(old_ptr< T > const & other);

   Construct this old value pointer from an old value pointer that requires the old value type to be copyable.

   Ownership of the pointed value object is transferred to this pointer. This constructor is implicitly called by this library when assigning an object of this type using ``BOOST_CONTRACT_OLDOF`` (this constructor is usually not explicitly called by user code).

   Parameters:

   `other` | Old value pointer that requires the old value type to be copyable.

3.     T const & operator*() const;

   Dereference this old value pointer.

   This will generate a run-time error if this pointer is null, but no compile-time error is generated if the pointed type `T` is not copyable (i.e., if `boost::contract::is_old_value_copyable<T>::value` is `false`).

   Returns: | The pointed old value. Contract assertions should not change the state of the program so this member function is `const` and it returns the old value as a reference to a constant object (see Constant Correctness).

4.     T const * operator->() const;

   Structure-dereference this old value pointer.

   This will return null but will not generate a compile-time error if the pointed type `T` is not copyable (i.e., if `boost::contract::is_old_value_copyable<T>::value` is `false`).

   Returns: | A pointer to the old value (null if this old value pointer is null). Contract assertions should not change the state of the program so this member function is `const` and it returns the old value as a pointer to a constant object (see Constant Correctness).

5.     explicit operator bool() const;

   Query if this old value pointer is null or not (safe-bool operator).

   (This is implemented using safe-bool emulation on compilers that do not support C++11 explicit type conversion operators.)

   Returns: | True if this pointer is not null, false otherwise.