fortran - 在内部类型上重载 Fortran 内部运算符?

标签 fortran operator-overloading

代码

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 说

pow_fast.f90:5:25:

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

Intel Fortran 也有类似的说法。 Fortran 中是否可以在内部类型上重载内部运算符?

最佳答案

这样定义的操作是不允许的。来自 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).

关于fortran - 在内部类型上重载 Fortran 内部运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71820344/

相关文章:

Fortran 调用 sleep 不会阻塞程序

fortran - 是否有必要在现代 Fortran 中将 _kind 附加到数字文字?

python - 使用 Python 将数组写入二进制文件,然后使用 Fortran 读取

design-patterns - 如何避免 Fortran 90 或更高版本中的全局变量?

compiler-errors - Fortran问题: Argument of “xx” is one type at (2) but is some other type at (1)

c++ - 在 C++ 中,如何在指向对象的指针上调用偏移运算符

c++ - 使用模板模板参数对 'operator<<' 进行模糊重载

c++ - boost foreach 和运算符重载

c++ - 为具有大量元素的类实现赋值运算符

c++ - Operator << 重载继承,为什么我从基类而不是子类获取输入?