C++ Boost

Concept: OptionalPointee

Description

A type is a model of OptionalPointee if it points to (or refers to) a value that may not exist. That is, if it has a pointee which might be valid (existent) or invalid (inexistent); and it is possible to test whether the pointee is valid or not. This model does not imply pointer semantics: i.e., it does not imply shallow copy nor aliasing.

Notation

T is a type that is a model of OptionalPointee
t is an object of type T or possibly const T

Definitions

Valid expressions

Name Expression Return type Semantics
Value Access  *t  T& If the pointee is valid returns a reference to the pointee.
If the pointee is invalid the result is undefined.
Value Access  t->xyz  T* If the pointee is valid returns a builtin pointer to the pointee.
If the pointee is invalid the result is undefined (It might not even return NULL).
Validity Test  t
 t != 0
 !!t
 bool If the pointee is valid returns true.
If the pointee is invalid returns false.
Invalidity Test  t == 0
 !t
 bool If the pointee is valid returns false.
If the pointee is invalid returns true.

Models


OptionalPointee and relational operations

This concept does not define any particular semantic for relational operations, therefore, a type which models this concept might have either shallow or deep relational semantics.
For instance, pointers, which are models of OptionalPointee, have shallow relational operators: comparisons of pointers do not involve comparisons of pointees. This makes sense for pointers because they have shallow copy semantics.
But boost::optional<T>, on the other hand, which is also a model of OptionalPointee, has deep-copy and deep-relational semantics.
If generic code is written for this concept, it is important not to use relational operators directly because the semantics might be different depending on the actual type.
Still, the concept itsef can be used to define deep relational tests that can be used in generic code with any type which models OptionalPointee:

Equivalence relation:

template<class OptionalPointee>
inline
bool equal_pointees ( OptionalPointee const& x, OptionalPointee const& y )
{
  return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ;
}
template<class OptionalPointee>
struct equal_pointees_t : std::binary_function<OptionalPointee,OptionalPointee,bool>
{
  bool operator() ( OptionalPointee const& x, OptionalPointee const& y ) const
    { return equal_pointees(x,y) ; }
} ;

The preceding generic function and function object have the following semantics:
If both x and y have valid pointees, it compares values via (*x == *y).
If only one has a valid pointee, returns false.
If both have invalid pointees, returns true.

Less-than relation:

template<class OptionalPointee>
inline
bool less_pointees ( OptionalPointee const& x, OptionalPointee const& y )
{
  return !y ? false : ( !x ? true : (*x) < (*y) ) ;
}
template<class OptionalPointee>
struct less_pointees_t : std::binary_function<OptionalPointee,OptionalPointee,bool>
{
  bool operator() ( OptionalPointee const& x, OptionalPointee const& y ) const
    { return less_pointees(x,y) ; }
} ;

The preceding generic function and function object have the following semantics:
If y has an invalid pointee, returns false.
Else, if x has an invalid pointee, returns true.
Else, ( x and y have valid pointees), compares values via (*x < *y).


All these functions and function objects are is implemented in compare_pointees.hpp

Notice that OptionalPointee does not imply aliasing (and optional<> for instance does not alias); so direct usage of relational operators with the implied aliasing of shallow semantics -as with pointers- should not be used with generic code written for this concept.



Copyright © 2003 Fernando Cacciola, based on the original concept developed by Augustus Saunders.