Automatically generate associated operators

xiaoxiao2021-03-06  117

Automatically generate associated operators

Custom operator is a feature of C . It can use a lot of operations to be intuitive, in line with the general thinking:

For example, in most languages, for the built-in type variables A, B, B, B, B, and one calculation process can be written to: A B / A - 300, which is exactly the same as the mathematical operation expression. But for non-built-in types, such as Complex is not necessarily. For example, in Java, it can only be written as a.add (B.DIV (a) .sub (300)), it is very ugly. The C can be implemented with intimate operational expression with INT by custom operators.

There is an important principle of the implementation of the operator: the original intention of the operator is not changed. Should not be overloaded into subtraction operations, and should not define "&&" as multipliers, although these redemption is legal in grammar. One of this principle of this principle is that the relationship between the associated operator should not change. " =" And " " are a pair of associated operation, "<", ">", "<=" and "> =" are also a group of associated operations. Its relationship is like this:

A = a b is equivalent to A = B

There is a a, b> = a, a <= b

The associated operator should be implemented together, in order to ensure that it is unchanged, only one basic operator is implemented in a set of associated operators, and other operators are implemented using this basic operator. For example, the addition calculation is based on = as basic operation, and call = operator is implemented.

Class A

{

A & Operator = (a const & a);

}

A Operator (a const & a const & b)

{

A result = a;

Result = B;

Return Result;

}

The relational operator has a set of modes of implementation. If A defines the "<" number, the following template is defined in STL.

Template

Inline Bool _stlp_call operator> (const _tp & __x, const _tp & __y) {

Return __y <__x;

}

Template

Inline Bool _stlp_call operator <= (const_tp & __x, const _tp & __y) {

Return! (__ y <__x);

}

Template

Inline Bool _stlp_call operator> = (const_tp & __x, const _tp & __y) {

Return! (__ x <__y);

}

They are defined in the std :: Rel_ops namespace, to use them, need to declare use namespace std :: rel_ops in the class.

This can be considered the most basic associated operation operator generator, but unfortunately it is too little, it is not flexible enough. However, this powerful function in the Boost library is completed, this is boost :: Operators.

Operator mainly provides operator definitions for the type by inheritance and friend.

Usage 1 is inherited:

Class A: Boost :: Less_th_comparable

{

Friend Bool Operator <(A Const & A, A Const & B);

}

Usage 2 passes the template:

Class A

{

Friend Bool Operator <(A Const & A, A Const & B);

}

Template boost :: less_than_comparable ; // Explicitly instantiates template.

Boost :: Less_than_Comparable Template class provides the following group of friends:

Friend Bool Operator> (Const T & X, Const T & Y) {RETURN Y

Friend Bool Operator <= (Const T & X, Const T & Y) {Return! (Y

Friend Bool Operator> = (Const T & X, Const T & Y) {Return! (x

They all rely on the Operator

Boost does not only provide operators for the current type, but also provide operators between types, as follows.

Usage 1 is inherited:

Class A: Boost :: Less_than_comparable

{

Friend Bool Operator <(A Const & A, B Const & B);

Friend Bool Operator> (A Const & A, B Const & B);

}

Usage 2 passes the template:

Class A

{

Friend Bool Operator <(A Const & A, B Const & B);

Friend Bool Operator> (A Const & A, B Const & B);

}

Template Boost :: Less_than_comparable ; // Explicit instantiation template.

Boost :: Less_than_comparable Template class provides the following group of friends:

BOOL Operator <= (Const T &, Const U &)

Bool Operator> = (Const T &, Const U &)

Bool Operator> (Const U &, Const T &)

Bool Operator <(Const U &, Const T)

BOOL Operator <= (Const U &, Const T)

Bool Operator> = (Const U &, Const T & S)

The following is a Template provided by Boost, a corresponding table of operators provided by templates and operators dependent on the template.

T: Primary Operand Type T, T1: VALUES OF TYPE T u: Value of Type U

template

Operator provided

Dependent operator

Less_than_comparable

Less_than_comparable1

Bool Operator> (Const T &, Const T &) BOOL Operator <= (Const T &, Const T &) Bool Operator> = (Const T &, Const T &)

T less_than_comparable2

Bool Operator <= (Const T &, Const U &) BOOL Operator> = (Const U &, Const U &) BOOL Operator> (Const U &, Const T &) BOOL Operator <= (Const U &, Const T & Bool Operator> = (Const U &, Const T &)

T u.

Equality_comparable equality_comparable1

Bool Operator! = (Const T &, Const T & S)

T == T1.

Equality_comparable equality_comparable2

Bool Operator == (Const U &, Const T &) Bool Operator! = (Const U &, Const T &) BOOL Operator! = (Const T &, Const U &)

T == u.

Addable Addable1

T Operator (Const T &, Const T & S)

T Temp (T); TEMP = T1. The return value type is T.

Addable Addable2

T Operator (Const T &, Const U &) T Operator (Const U &, Const T & S)

T Temp (T); Temp = u. Return value type T.

Subtractable Subtractable1

T Operator- (Const T &, Const T & S)

T TEMP (T); TEMP - = T1.

Subtractable subtractable2

T Operator- (Const T &, Const U &)

T Temp (T); TEMP - = U.

Subtractable2_left

T Operator - (Const U &, Const T &)

T temp (u); temp - = t.

Multipliable Multipliable1

T Operator * (Const T &, Const T & S)

T Temp (T); Temp * = T1.

Multipliable multipliable2

T Operator * (Const T &, Const U &) T Operator * (Const U &, Const T &)

T temp (t); temp * = u.

Dividable Dividable1

T Operator / (Const T &, Const T & S)

T Temp (T); TEMP / = T1.

Dividable Dividable2 T Operator / (Const T &, Const U &)

T Temp (T); TEMP / = U.

Dividable2_left

T Operator / (Const U &, Const T &)

T Temp (U); TEMP / = T.

Modable Modable1

T Operator% (Const T &, Const T &)

T TEMP (T); TEMP% = T1.

Modable MODABLE2

T Operator% (Const T &, Const U &)

T temp (t); temp% = u.

Modable2_left

T Operator% (Const U &, Const T &)

T Temp (U); TEMP% = T.

ORABLE ORABLE1

T Operator | (Const T &, Const T & S)

T TEMP (T); TEMP | = T1.

ORABLE ORABLE2

T Operator | (Const T &, Const U &) T Operator | (Const U &, Const T &)

T Temp (T); TEMP | = u.

ANDABLE Andable1

T Operator & (Const T &, Const T &)

T Temp (T); TEMP & = T1.

ANDABLE andable2

T Operator & (Const T &, Const U &) T Operator & (Const U &, Const T &)

T Temp (T); TEMP & = U.

XORABLE XORABLE1

T Operator ^ (Const T &, Const T & S & I)

T Temp (T); TEMP ^ = T1.

XORABLE xorable2

T Operator ^ (Const T &, Const U &) T Operator ^ (Const U &, Const T &)

T Temp (T); TEMP ^ = u.

Incrementable

T Operator (T &, INT)

T temp (t); T

Decrementable

T Operator - (T &, INT)

T temp (t); --T;

Left_shiftable left_shiftable1

T Operator << (Const T &, Const T & S)

T Temp (T); TEMP << = T1.

Left_shiftable left_shiftable2

T Operator << (Const T &, Const U &)

T Temp (T); TEMP << = u.

Right_shiftable Right_Shiftable1 T Operator >> (Const T &, Const T &)

T TEMP (T); TEMP >> = T1.

Right_shiftable right_shiftable2

T Operator >> (Const T &, Const U &)

T TEMP (T); TEMP >> = U.

Equivalent Equivalent1

BOOL Operator == (Const T &, Const T & S)

T

Equivalent Equivalent2

Bool Operator == (Const T &, Const U &)

T u.

Partially_Ordered Partially_Ordered1

Bool Operator> (Const T &, Const T &) BOOL Operator <= (Const T &, Const T &) Bool Operator> = (Const T &, Const T &)

T

Partially_Ordered partially_ordered2

Bool Operator <= (Const T &, Const U &) BOOL Operator> = (Const U &, Const U &) BOOL Operator> (Const U &, Const T &) BOOL Operator <= (Const U &, Const T & Bool Operator> = (Const U &, Const T &)

T u. t == u.

For the same class to implement multi-group operators, Operators uses chain inheritance, for example, if it is necessary to simultaneously define less than and equal to symbols. It can be defined like this:

Class A: Boost :: Less_than_comparable >>

{

Friend Bool Operator <(A Const & A, A Const & B);

Friend Bool Operator == (A Const & A, A Const & B);

}

This is the case if it is less than and equal to the defined type:

Class A: Boost :: Less_than_comparable >

{

Friend Bool Operator <(A Const & A, B Const & B);

Friend Bool Operator> (A Const & A, B Const & B);

Friend Bool Operator == (A Const & A, B Const & B);

}

Boost pre-defines some combination operation templates, or they can also be used directly, such as the above class can also be defined as: Class A: Totally_Ordered

{

Friend Bool Operator <(A Const & A, A Const & B);

Friend Bool Operator == (A Const & A, A Const & B);

}

The following is a constituent table called a combination of combined operations:

Template

Component Operator Templates

Totally_Ordered Totally_Ordered1

· Less_than_comparable

· Equality_comparable

Totally_Ordered Totally_Ordered2

· Less_than_comparable

· Equality_comparable

Additive Additive1

Addable

· Subtractable

Additive Additive2

Addable

Subtractable

Multiplicative Multiplicative1

· Multipliable

· Dividable

Multiplicative Multiplicative2

· Multipliable

· Dividable

Integer_multiplicative Integer_Multiplicative1

· Multiplicative

· MODABLE

Integer_Multiplicative integer_multiplicative2

· Multiplicative

· MODABLE

Arithmetic Arithmetic1

· Additive

· Multiplicative

Arithmetic arithmetic2

· Additive

· Multiplicative

Integer_arithmetic INTEGER_ARITHMETIC1

· Additive

· Integer_multiplicative

Integer_arithmetic integer_arithmetic2

· Additive

· Integer_multiplicative

Bitwise Bitwise1

· XORABLE

· ANDABLE

· ORABLE

Bitwise bitwise2

· XORABLE

· ANDABLE · ORABLE

Unit_steppable

· Incrementable

· Decrementable

Shiftable Shiftable1

· LEFT_SHIFTABLE

· Right_Shiftable

Shiftable Shiftable2

· LEFT_SHIFTABLE

· Right_Shiftable

Ring_operators Ring_operators1

· Additive

· Multipliable

Ring_operators Ring_operators2

· Additive

· Subtractable2_left

· Multipliable

Ordered_ring_operators Ordered_ring_operators1

· Ring_operators

Totally_Ordered

Ordered_ring_operators Ordered_ring_operators2

· Ring_Operators

· Totally_Ordered

Field_operators field_operators1

· Ring_operators

· Dividable

Field_operators field_operators2

· Ring_Operators

· Dividable

· DividAble2_left

Ordered_field_operators Ordered_Field_operators1

· Field_operators

Totally_Ordered

Ordered_field_operators Ordered_field_operators2

· Field_operators

· Totally_Ordered

Euclidian_ring_operators Euclidian_ring_operators1

· Ring_operators

· Dividable

· MODABLE

Euclidian_ring_operators euclidian_ring_operators2

· Ring_Operators

· Dividable

· DividAble2_left

· MODABLE

· MODABLE2_LEFT

Ordered_euclidian_ring_operators Orderate_Euclidian_ring_operators1

· Euclidian_ring_operators · Totally_Ordered

Ordered_euclidian_ring_operators Orderate_Euclidian_ring_operators2

· Euclidian_ring_operators

· Totally_Ordered

Like other Boost libraries, Operators requires support for the C 98 standard compiler, and now fully supported compilers have VC7.1, GCC3.3, EGD 2.5 or more. GCC2.95 and VC6 SP3 or more are supported by a certain manner.

For a fully supported compiler, it is recommended to use OperatorAble and OperatRable template. For compilers that are not fully supported, you can use OperatorAble1 and OperatorAble2 two interfaces.


New Post(0)