Beyond the C ++ standard library: An Introduction to boost-library 4.2 Operators

Source: Internet
Author: User
Operators

Header file: "Boost/operators. HPP"

The operators library consists of multiple base classes. Each class generates Operators related to its name concept. You can use them in inheritance mode. If you need more than one function, you need to use multiple inheritance. Fortunately, operators defines some composite concepts. In most cases, multi-inheritance is not required. The following describes some of the most common operator classes, including the concepts they represent and their requirements for derived classes. In some cases, operators may have different requirements on the Concept Base class. For example, the concept of addable requires an operator.T operator + (const T & LHS, const T & RHs)While operators's base classAddableHowever, a member function is required,T operator ++ = (const T & other). Use this member function, the base classAddableAutomatically added for the derived classOperator +In the following sections, the concepts are first given, and then the requirements for Classes derived from the concepts are given. I did not repeat all the concepts in this library, but just selected the most important ones. You can find the complete reference documents on www.boost.org.

Less_than_comparable

Less_than_comparableRequired typeTIt has the following semantics.

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

To be derived fromBoost: less_than_comparable, Derived class (T) Must provide:

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

Note that the type of the returned value does not have to be true.BoolBut must be implicitly convertedBoolThe. c ++ standard concept lessthancomparable requiresOperator <, So fromLess_than_comparableThe derived class must meet this requirement. In return,Less_than_comparableAccordingOperator <Implement the remaining three operators.

Equality_comparable

Equality_comparableRequired typeTIt has the following semantics.

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

To be derived fromBoost: equality_comparable, Derived class (T) Must provide:

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

Similarly, the type of the returned value does not have to beBoolBut must be implicitly convertedBoolIn the. c ++ standard, the concept of equalitycomparable must be provided.Operator =ThereforeEquality_comparableThe derived class must meet this requirement.Equality_comparableClass isTProvideBool Operator! = (Const T &, const T &).

Addable

AddableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: addable, Derived class (T) Must provide:

T operator+=(const T&);

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator + (const T &, const T &).

Subtractable

SubtractableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: subtractable, Derived class (T) Must provide:

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

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator-(const T &, const T &).

Orable

OrableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: orable, Derived class (T) Must provide:

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

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator | (const T &, const T &).

Andable

AndableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: andable, Derived class (T) Must provide:

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

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator & (const T &, const T &).

Incrementable

IncrementableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: incrementable, Derived class (T) Must provide:

T& operator++(T&);

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator ++ (T &, INT).

Decrementable

DecrementableConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: decrementable, Derived class (T) Must provide:

T& operator--(T&);

The type of the returned value must be implicitly convertedT. ClassAddableIsTImplementationT operator -- (T &, INT).

Equivalent

EquivalentConcept requirement typeTIt has the following semantics.

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

To be derived fromBoost: equivalent, Derived class (T) Must provide:

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

The type of the returned value must be implicitly convertedBool. ClassEquivalentIsTImplementationT operator = (const T &, const T &). Note: equivalence and equality are not necessarily the same. Equivalent objects are not necessarily equal ). ButEquivalentIn terms of concept, they are the same.

Unreference Operator

Two concepts are particularly useful for iterators,DereferenceableAndIndexableTo understand the two situations of reference:* T,TIt is an iterator that supports resolution reference (obviously all iterators support it), and the other is indexing,T [x],TIs a type that supports subscript operator addressing, whileXIt is usually an integer. At a higher abstraction level, they are usually used together and collectively called iterator operators, including the two unreferenced operators and some simple arithmetic operators.

Dereferenceable

DereferenceableConcept requirement typeTIt has the following semantics:TIs the operand,RIs a reference type, whilePIs a pointer type (for example,TIs an iterator type,RIs the iterator'sValue_typeAndPIt is the iterator'sValue_typePointer ).

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

To be derived fromBoost: dereferenceable, Derived class (T) Must provide:

R operator*() const;

In addition,RRMBOperator &Must be implicitly convertedP. This meansRIt does not have to be a reference type. It can be a proxy class ). ClassDereferenceableIsTImplementationP operator-> () const.

Indexable

IndexableConcept requirement typeTIt has the following semantics:TIs the operand,RYes reference type,PIs a pointer type, whileDYesDifference_type(For example,TIs an iterator type,RIs the iterator'sValue_type,PIs the iterator'sValue_typePointer, andDIsDifference_type).

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

To be derived fromBoost: Indexable, Derived class (T) Must provide:

R operator+(const T&,D);

ClassIndexableIsTImplementationR operator [] (d) const.

Compound Arithmetic Operators

The concepts we have seen so far only represent the simplest functions. However, there are also some advanced or composite concepts, which are combined by a few simple concepts, or simply added to the composite concepts. For example, a class isTotally_orderedIf it isLess_than_comparableAndEquality_comparable. These combinations are useful because they reduce the amount of code and indicate important and common concepts. Because they only represent a combination of existing concepts, these concepts can easily be expressed in a table. For example, if a class is derived fromTotally_ordered, Which must be implementedLess_than_comparableRequired operator (Bool operator <(const T &, const T &)) AndEquality_comparableRequired operator (Bool operator = (const T &, const T &)).

Combination Concept

Consists of the following 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.