Fortran 2003中的类型绑定函数重载 [英] Type-bound function overloading in Fortran 2003

查看:181
本文介绍了Fortran 2003中的类型绑定函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Fortran派生类型T,它包含(很多)不同级别和类型的数据数组。这些数组隐藏在一个复杂的数据结构中,我想要有一个getter函数来完成以下工作:

I have a Fortran derived type T that contains data arrays of (many) different ranks and types. These arrays are hidden inside a complicated data structure and I would like to have a getter function of that does the following:

a => T%get(data_id)

其中a是给定类型的数组指针,data_id是一个用于查找数据结构内数据的整数。我通过在通用名称下重载了许多get_thistype()函数来实现这一点。

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. I do that by overloading many "get_thistype()" functions under a generic name.

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

如果get_thistype()例程是子例程,如果它们被写为功能,则不是。这意味着我的代码如下所示:

This works if the get_thistype() routines are subroutines, but not if they are written as functions. 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?

推荐答案

当在Fortran中执行(指针)赋值语句时,右手在分配发生之前,总是得到充分的评估。这种情况独立于左侧,所以LHS绝对不可能影响RHS评估的结果。这只是语言设计的方式。

When a (pointer) assignment statement gets executed in fortran, the right hand side always gets evaluated fully before the assignment takes place. 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. It's just the way the language is designed.

这篇关于Fortran 2003中的类型绑定函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆