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 (it exist) the reference refers to
the pointee value. If the pointee is invalid (it does not exist), the semantics is undefined. |
|
Validity Test | t t != 0 !!t /TD> | bool | If the pointee is valid, true. If the pointee is invalid, false. |
|
Invalidity Test | t == 0 !t |
bool | If the pointee is valid, false. If the pointee is invalid, 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 a deep equality-test that can
be used in generic code with any type which models OptionalPointee:
template<class OptionalPointee> inline bool equal_pointees ( OptionalPointee const& x, OptionalPointee const& y ) { return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ; }
The preceding generic function has the following deep semantics:
If both x and y have valid pointees, it compares pointee's values via (*x == *y).
If only one have valid pointees, returns false.
If both have invalid pointees, returns true.
equal_pointees()
is implemented in optional.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 © 2002 | Fernando Cacciola, based on the original concept developed by Augustus Saunders. |