## 8.9.4 Defining Generic Operators

An interface block can be used to define a generic operator. The only procedures allowed in the interface block are functions that can be referenced as defined operations.

The initial line for such an interface block takes the following form:

  INTERFACE OPERATOR (op)
  op
  Is one of the following:
  - A defined unary operator (one argument)
  - A defined binary operator (two arguments)
  - An extended intrinsic operator (number of arguments must be consistent with the intrinsic uses of that operator)

The functions within the interface block must have one or two nonoptional arguments with intent IN, and the function result must not be of type character with assumed length. A defined operation is treated as a reference to the function.

The following shows the form (and an example) of a defined unary and defined binary operation:

Operation | Form | Example
--- | --- | ---
Defined Unary | .defined-operator. operand 1 | .MINUS. C
Defined Binary | operand 2 .defined-operator. operand 3 | B .MINUS. C
1 The operand corresponds to the function's dummy argument. 2 The left operand corresponds to the first dummy argument of the function. 3 The right operand corresponds to the second argument.

For intrinsic operator symbols, the generic properties include the intrinsic operations they represent. Both forms of each relational operator have the same interpretation, so extending one form (such as >=) defines both forms (>= and .GE.).

The following is an example of a procedure interface block defining a new operator:

    INTERFACE OPERATOR(.BAR.)
      FUNCTION BAR(A_1)
        INTEGER, INTENT(IN) :: A_1
        INTEGER :: BAR
      END FUNCTION BAR
    END INTERFACE

The following example shows a way to reference function BAR by using the new operator:

    INTEGER B
    I = 4 + (.BAR. B)

The following is an example of a procedure interface block with a defined operator extending an existing operator:

    INTERFACE OPERATOR(+)
      FUNCTION LGFUNC (A, B)
        LOGICAL, INTENT(IN) :: A(:), B(SIZE(A))
        LOGICAL :: LGFUNC(SIZE(A))
      END FUNCTION LGFUNC
    END INTERFACE

The following example shows two equivalent ways to reference function LGFUNC:

    LOGICAL, DIMENSION(1:10) :: C, D, E
    N = 10
    E = LGFUNC(C(1:N), D(1:N))
    E = C(1:N) + D(1:N)

For More Information:

- On interface blocks, see Section 8.9.2.
- On intrinsic operators, see Section 4.1.
- On defined operators and operations, see Section 4.1.5.
- On intent, see Section 5.10.

Previous Page Next Page Table of Contents