...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
T | is a type that is a model of OptionalPointee |
t | is an object of type T or possibly const T |
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. |
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.
Based on the original concept developed by Augustus Saunders.
Copyright © 2003 | Fernando Cacciola |
Distributed under the Boost Software License, Version 1.0. See www.boost.org/LICENSE_1_0.txt