Operators


Header: "boost/operators.hpp"

There are a number of base classes that comprise the Operators library. Each class contributes operators according to the concept it names. You use them by inheriting from themmultiply inheriting if you need the services of more than one. Fortunately, there are some composite concepts defined in Operators obviating the need to multiply inherit for many common cases. The following synopses describe some of the most commonly used Operator classes, the concepts they represent, and the demands they place on classes derived from them. In some cases, the requirements for the actual concepts are not the same as the requirements for the concept base classes when using Operators. For example, the concept addable requires that there be an operator T operator+(const T& lhs,const T& rhs) defined, but the Operators base class addable instead requires a member function, T operator+=(const T& other). Using this member function, the base class addable augments the derived class with operator+. THRoughout the synopses, the concepts are always stated first, followed by the type requirements for classes deriving from them. Rather than repeating all of the concepts in this library, I have selected a few important ones; you'll find the full reference at www.boost.org, of course.

less_than_comparable

The less_than_comparable concept requires the following semantics for a type T.

 bool operator<(const T&,const T&);  bool operator>(const T&,const T&);  bool operator<=(const T&,const T&); bool operator>=(const T&,const T&); 

When deriving from boost::less_than_comparable, the derived class (T) must provide the equivalent of

 bool operator<(const T&, const T&);  

Note that the return type need not be exactly bool, but it must be implicitly convertible to bool. For the concept LessThanComparable found in the C++ Standard, operator< is required, so classes derived from less_than_comparable need to comply with that requirement. In return, less_than_comparable implements the three remaining operators in terms of operator<.

equality_comparable

The equality_comparable concept requires the following semantics for a type T.

 bool operator==(const T&,const T&); bool operator!=(const T&,const T&); 

When deriving from boost::equality_comparable, the derived class (T) must provide the equivalent of

 bool operator==(const T&,const T&); 

Again, the return type needn't be bool, but it must be a type implicitly convertible to bool. For the concept EqualityComparable in the C++ Standard, operator== is required, so derived classes from equality_comparable need to comply with that requirement. The class equality_comparable equips T with bool operator!=(const T&,const T&).

addable

The addable concept requires the following semantics for a type T.

 T operator+(const T&,const T&); T operator+=(const T&); 

When deriving from boost::addable, the derived class (T) must provide the equivalent of

 T operator+=(const T&); 

The return type must be implicitly convertible to T. The class addable equips T with T operator+(const T&,const T&).

subtractable

The subtractable concept requires the following semantics for a type T.

 T operator-(const T&,const T&); T operator+=(const T&); 

When deriving from boost::subtractable, the derived class (T) must provide the equivalent of

 T operator-=(const T&,const T&); 

The return type must be implicitly convertible to T. The class subtractable equips T with T operator-(const T&,const T&).

orable

The orable concept requires the following semantics for a type T.

 T operator|(const T&,const T&); T operator|=(const T&,const T&); 

When deriving from boost::orable, the derived class (T) must provide the equivalent of

 T operator|=(const T&,const T&); 

The return type must be implicitly convertible to T. The class orable equips T with T operator|(const T&,const T&).

andable

The andable concept requires the following semantics for a type T.

 T operator&(const T&,const T&); T operator&=(const T&,const T&); 

When deriving from boost::andable, the derived class (T) must provide the equivalent of

 T operator&=(const T&,const T&); 

The return type must be implicitly convertible to T. The class andable equips T with T operator&(const T&,const T&).

incrementable

The incrementable concept requires the following semantics for a type T.

 T& operator++(T&); T operator++(T&,int); 

When deriving from boost::incrementable, the derived class (T) must provide the equivalent of

 T& operator++(T&); 

The return type must be implicitly convertible to T. The class incrementable equips T with T operator++(T&,int).

decrementable

The decrementable concept requires the following semantics for a type T.

 T& operator--(T&); T operator--(T&,int); 

When deriving from boost::decrementable, the derived class (T) must provide the equivalent of

 T& operator--(T&); 

The return type must be implicitly convertible to T. The class decrementable equips T with T operator--(T&,int).

equivalent

The equivalent concept requires the following semantics for a type T.

 bool operator<(const T&,const T&); bool operator==(const T&,const T&); 

When deriving from boost::equivalent, the derived class (T) must provide the equivalent of

 bool operator<(const T&,const T&); 

The return type must be implicitly convertible to bool. The class equivalent equips T with T operator==(const T&,const T&). Note that equivalence and equality are, by definition, different beasts; two objects that are equivalent aren't necessarily equal. However, for the purposes of the equivalent concept, they are the same.

Dereferencing Operators

Especially useful for iterators, these two concepts, dereferenceable and indexable, cover two cases of dereferencing: *t, where t is an iterator that supports dereferencing (and all iterators obviously do), and indexing, t[x], where t is a type that supports indexing through the subscript operator, and x is of an integral type. These two are used together with a higher-level abstraction, grouped iterator operators, which builds on both these dereferencing operators and the simple arithmetic operators.

dereferenceable

The dereferenceable concept requires the following semantics for a type T, assuming that T is the operand, R is the reference type, and P is a pointer type (for example, T is an iterator type, R is a reference to the iterator's value_type, and P is a pointer to the iterator's value_type).

 P operator->() const; R operator*() const; 

When deriving from boost::dereferenceable, the derived class (T) must provide the equivalent of

 R operator*() const; 

Additionally, the unary operator& for R must be implicitly convertible to P. This means that R doesn't actually need to be the reference typeit can just as well be a proxy class. The class dereferenceable equips T with P operator->() const.

indexable

The indexable concept requires the following semantics for a type T, assuming that T is the operand, R is the reference type, P is a pointer type, and D is the difference_type (for example, T is an iterator type, R is a reference to the iterator's value_type, P is a pointer to the iterator's value_type, and D is the difference_type).

 R operator[](D) const; R operator+(const T&,D); 

When deriving from boost::indexable, the derived class (T) must provide the equivalent of

 R operator+(const T&,D); 

The class indexable equips T with R operator[](D) const.

Composite Arithmetic Operators

The concepts we've seen thus far represent primitive functionality. However, there are higher level, or composite, concepts that combine several primitive concepts or even add a primitive concept to another composite concept. For example, a class is totally_ordered if it is both less_than_comparable and equality_comparable. These groups are useful both because they reduce the amount of code that needs to be written and that they explicitly name important, commonly used concepts. Because they merely represent the combination of concepts already covered, these composite concepts are most easily represented in a table showing the primitive concepts on which they are built. For example, if a class inherits from totally_ordered, it must implement the operators required for less_than_comparable (bool operator<(const T&,const T&)) and for equality_comparable (bool operator==(const T&,const T&)).

Composite Concept

Constituent Concepts

totally_ordered

less_than_comparable

equality_comparable

additive

addable

subtractable

multiplicative

multipliable

dividable

integer_multiplicative

multiplicative

modable

arithmetic

additive

multiplicative

integer_arithmetic

additive

integer_multiplicative

bitwise

andable

orable

xorable

unit_steppable

incrementable

decrementable

shiftable

left_shiftable

right_shiftable

ring_operators

additive

multipliable

ordered_ring_operators

ring_operators

totally_ordered

field_operators

ring_operators

dividable

ordered_field_operators

field_operators

totally_ordered

euclidian_ring_operators

ring_operators

dividable

modable

ordered_ euclidian_ring_operators

euclidean_ring_operators

totally_ordered




    Beyond the C++ Standard Library(c) An Introduction to Boost
    Beyond the C++ Standard Library: An Introduction to Boost
    ISBN: 0321133544
    EAN: 2147483647
    Year: 2006
    Pages: 125

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net