简体   繁体   English

Fortran 2003中的类型绑定函数重载

[英]Type-bound function overloading in Fortran 2003

I have a Fortran derived type T that contains data arrays of (many) different ranks and types. 我有一个Fortran派生类型T,它包含(许多)不同等级和类型的数据数组。 These arrays are hidden inside a complicated data structure and I would like to have a getter function of that does the following: 这些数组隐藏在一个复杂的数据结构中,我想有一个getter函数,它执行以下操作:

a => T%get(data_id)

where "a" is an array pointer of given type, and data_id is an integer that is used to find the data inside the data structure. 其中“a”是给定类型的数组指针,data_id是一个整数,用于查找数据结构内的数据。 I do that by overloading many "get_thistype()" functions under a generic name. 我通过在通用名称下重载许多“get_thistype()”函数来做到这一点。

TYPE T
   PROCEDURE :: get_real
   PROCEDURE :: get_integer
   GENERIC   :: get => get_real,get_integer
END TYPE

This works if the get_thistype() routines are subroutines, but not if they are written as functions. 如果get_thistype()例程是子例程,则此方法有效,但如果它们被写为函数则不行。 This means my code looks like: 这意味着我的代码如下:

CALL T%get(a,data_id)

which I find much less readable. 我发现它的可读性要低得多。 Is there a way to overload functions that have the same argument list but different return types? 有没有办法重载具有相同参数列表但返回类型不同的函数? or do I have to use subroutines for that? 或者我必须使用子程序吗?

When a (pointer) assignment statement gets executed in fortran, the right hand side always gets evaluated fully before the assignment takes place. 当一个(指针)赋值语句在fortran中执行时,右侧总是在赋值发生之前被完全评估。 This happens independently of the left hand side, so there is absolutely no way that the LHS can influence the outcome of the evaluation of the RHS. 这种情况独立于左侧发生,因此LHS绝对无法影响RHS评估的结果。 It's just the way the language is designed. 这就是语言设计的方式。

I just came across this post, so for the benefit of anyone see this in the future: 我刚刚看到这篇文章,所以为了任何人的利益,将来会看到这个:

If I understand the question correctly, you can accomplish this by overloading the assignment operator. 如果我正确理解了这个问题,你可以通过重载赋值运算符来实现这个目的。 Example: 例:

file X.f90: 文件X.f90:

MODULE XModule

TYPE :: X
   INTEGER, DIMENSION(:), POINTER :: IntArray
   REAL,    DIMENSION(:), POINTER :: RealArray
END TYPE

INTERFACE ASSIGNMENT (=)
   MODULE PROCEDURE PointToInt
   MODULE PROCEDURE PointToReal
END INTERFACE

CONTAINS

SUBROUTINE PointToInt(Ip, V)
   INTEGER, POINTER, DIMENSION(:), INTENT(OUT) :: Ip
   TYPE(X), INTENT(IN) :: V
   Ip => V%IntArray
END SUBROUTINE PointToInt

SUBROUTINE PointToReal(Rp, V)
   REAL, POINTER, DIMENSION(:), INTENT(OUT) :: Rp
   TYPE(X), INTENT(IN) :: V
   Rp => V%RealArray
END SUBROUTINE PointToReal

END MODULE

test driver file Driver.f90: 测试驱动程序文件Driver.f90:

PROGRAM Driver
USE XModule
TYPE(X) :: Var
INTEGER, DIMENSION(:), POINTER :: I
REAL, DIMENSION(:), POINTER :: R

ALLOCATE(Var%IntArray(2))
ALLOCATE(Var%RealArray(3))

Var%IntArray = [1, 2]
Var%RealArray = [1., 2., 3.]

I = Var
PRINT*, I

R = Var
PRINT*, R

END PROGRAM

Output: 输出:

           1           2
   1.000000       2.000000       3.000000    

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM