Fortranner
Fortranner

Reputation: 2605

Overload Fortran intrinsic operator on intrinsic types?

For the code

module pow_mod
implicit none
integer, parameter :: dp = kind(1.0d0)
interface operator(**)
   module procedure mypow
end interface
contains
!
function mypow(x,y) result(x_to_y)
real(kind=dp), intent(in) :: x,y
real(kind=dp)             :: x_to_y
x_to_y = exp(y*log(x))
end function mypow
end module pow_mod

gfortran says

pow_fast.f90:5:25:

    5 |    module procedure mypow
      |                         1
Error: Operator interface at (1) conflicts with intrinsic interface

and Intel Fortran says something similar. Is it possible in Fortran to overload intrinsic operators on intrinsic types?

Upvotes: 3

Views: 277

Answers (2)

This is forbidden by this restriction in Fortran 2018

15.4.3.4.2
Defined operations 1 If OPERATOR is specified in a generic specification, all of the procedures specified in the generic interface shall be functions that may be referenced as defined operations (10.1.6, 15.5). In the case of functions of two arguments, infix binary operator notation is implied. In the case of functions of one argument, prefix operator notation is implied. OPERATOR shall not be specified for functions with no arguments or for functions with more than two arguments. The dummy arguments shall be nonoptional dummy data objects and shall have the INTENT (IN) or VALUE attribute. The function result shall not have assumed character length. If the operator is an intrinsic-operator (R608), the number of dummy arguments shall be consistent with the intrinsic uses of that operator, and the types, kind type parameters, or ranks of the dummy arguments shall differ from those required for the intrinsic operation (10.1.5).

Upvotes: 3

francescalus
francescalus

Reputation: 32406

It is not allowed to have a defined operation like this. From Fortran 2018 (15.4.3.4.2):

If the operator is an intrinsic-operator (R608), the number of dummy arguments shall be consistent with the intrinsic uses of that operator, and the types, kind type parameters, or ranks of the dummy arguments shall differ from those required for the intrinsic operation (10.1.5).

Upvotes: 4

Related Questions