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 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 Equality_comparable Bool Operator! = (Const T &, Const T & S) T == T1. Equality_comparable Bool Operator == (Const U &, Const T &) Bool Operator! = (Const U &, Const T &) BOOL Operator! = (Const T &, Const U &) T == u. Addable T Operator (Const T &, Const T & S) T Temp (T); TEMP = T1. The return value type is T. Addable T Operator (Const T &, Const U &) T Operator (Const U &, Const T & S) T Temp (T); Temp = u. Return value type T. Subtractable T Operator- (Const T &, Const T & S) T TEMP (T); TEMP - = T1. Subtractable 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 T Operator * (Const T &, Const T & S) T Temp (T); Temp * = T1. Multipliable T Operator * (Const T &, Const U &) T Operator * (Const U &, Const T &) T temp (t); temp * = u. Dividable T Operator / (Const T &, Const T & S) T Temp (T); TEMP / = T1. Dividable T Temp (T); TEMP / = U. Dividable2_left T Operator / (Const U &, Const T &) T Temp (U); TEMP / = T. Modable T Operator% (Const T &, Const T &) T TEMP (T); TEMP% = T1. Modable 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 T Operator | (Const T &, Const T & S) T TEMP (T); TEMP | = T1. ORABLE T Operator | (Const T &, Const U &) T Operator | (Const U &, Const T &) T Temp (T); TEMP | = u. ANDABLE T Operator & (Const T &, Const T &) T Temp (T); TEMP & = T1. ANDABLE T Operator & (Const T &, Const U &) T Operator & (Const U &, Const T &) T Temp (T); TEMP & = U. XORABLE T Operator ^ (Const T &, Const T & S & I) T Temp (T); TEMP ^ = T1. XORABLE 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 T Operator << (Const T &, Const T & S) T Temp (T); TEMP << = T1. Left_shiftable T Operator << (Const T &, Const U &) T Temp (T); TEMP << = u. Right_shiftable T TEMP (T); TEMP >> = T1. Right_shiftable T Operator >> (Const T &, Const U &) T TEMP (T); TEMP >> = U. Equivalent BOOL Operator == (Const T &, Const T & S) T Equivalent Bool Operator == (Const T &, Const U &) T Partially_Ordered Bool Operator> (Const T &, Const T &) BOOL Operator <= (Const T &, Const T &) Bool Operator> = (Const T &, Const T &) T Partially_Ordered 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 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 · Less_than_comparable · Equality_comparable Totally_Ordered · Less_than_comparable · Equality_comparable Additive Addable · Subtractable Additive Addable Subtractable Multiplicative · Multipliable · Dividable Multiplicative · Multipliable · Dividable Integer_multiplicative · Multiplicative · MODABLE Integer_Multiplicative · Multiplicative · MODABLE Arithmetic · Additive · Multiplicative Arithmetic · Additive · Multiplicative Integer_arithmetic · Additive · Integer_multiplicative Integer_arithmetic · Additive · Integer_multiplicative Bitwise · XORABLE · ANDABLE · ORABLE Bitwise · XORABLE · ANDABLE Unit_steppable · Incrementable · Decrementable Shiftable · LEFT_SHIFTABLE · Right_Shiftable Shiftable · LEFT_SHIFTABLE · Right_Shiftable Ring_operators · Additive · Multipliable Ring_operators · Additive · Subtractable2_left · Multipliable Ordered_ring_operators · Ring_operators Totally_Ordered Ordered_ring_operators · Ring_Operators · Totally_Ordered Field_operators · Ring_operators · Dividable Field_operators · Ring_Operators · Dividable · DividAble2_left Ordered_field_operators · Field_operators Totally_Ordered Ordered_field_operators · Field_operators · Totally_Ordered Euclidian_ring_operators · Ring_operators · Dividable · MODABLE Euclidian_ring_operators · Ring_Operators · Dividable · DividAble2_left · MODABLE · MODABLE2_LEFT Ordered_euclidian_ring_operators · Euclidian_ring_operators Ordered_euclidian_ring_operators · 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