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

发布于 2025-01-20 17:03:11 字数 602 浏览 2 评论 0原文

对于代码

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 中是否可以重载内在类型上的内在运算符?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2025-01-27 17:03:11

不允许有这样的定义操作。从 Fortran 2018 (15.4.3.4.2) 开始:

如果运算符是内在运算符(R608),则伪参数的数量应与该运算符的固有用途一致,并且伪参数的类型、类型参数或等级应与所需的不同对于内在操作(10.1.5)。

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).

放赐 2025-01-27 17:03:11

这是Fortran 2018中的这种限制所禁止的

15.4.3.4.2
定义的操作1如果在通用规范中指定了操作员,则在通用元中指定的所有过程
接口应是可以称为定义的函数
操作(10.1.6,15.5)。对于两个参数的功能,
infix二进制操作员表示法。在功能的情况下
一个参数,暗示前缀操作员符号。操作员不得
为没有参数或功能的功能指定
超过两个论点。虚拟论点应无与伦比
虚拟数据对象,并应具有(in)或值属性的意图。
功能结果不得假定字符长度。 如果是
操作员是一个固有的操作员(R608),假人的数量
论点应与该论点的内在用途一致
操作员以及虚拟的类型,类型参数或等级
论点应与内在操作所需的论点不同
(10.1.5)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文