【发布时间】:2015-03-30 10:50:08
【问题描述】:
有没有一种方法可以定义通用过程来动态处理int、character 和real 中的每个kind,而不必为每个过程指定一个过程?我想这也是在询问 kind 多态是否存在于 Fortran 中。
我正在考虑类似这样的接口:
module generics_test
interface read_generic
module procedure read_int, &
read_real, &
read_char
end interface read_generic
contains
subroutine read_int(value)
implicit none
! Arguments
<what sort of type spec could go here?> :: value
! Implementation
<would there need to be some kind handling here?>
end subroutine read_int
<other read subroutines here>
end module generics_test
Fortran 2003 中的多态性似乎侧重于派生类型,例如 Portland Group 的 examples。我只对内在类型的泛型处理感兴趣。
这主要是出于好奇,因为我们几乎只使用 integer 和 character 和 real(8) 的默认值。 (注意,我知道我们应该使用iso_fortran_env,但并非我们使用的所有编译器都支持它。)
【问题讨论】:
-
对,我的回答中提到的问题不是直接重复的,但这可能是。虽然,搜索这些技术的人不必了解 C++ 和模板。我会在关闭前等待其他投票。
-
注:您不必使用
iso_fortran_env。您真正应该 使用的是种类常量,而不是仅将 (8) 放在那里。您在哪里获得恒定值完全取决于您,iso_fortran_env。只是众多可能性之一。 -
所以我使用我自己的精度模块,其变量定义为
dp = selected_real_kind(x, y),但重构整个代码以使用这种东西有点困难。我这样做是因为我的新东西被很好地划分,但不是所有的东西。 -
我的意思是 kind,我想。我特别考虑了不同类型的
real,因为默认值可能因编译器而异(以及编译器标志),而且我们的大部分代码只有默认的kind。我的问题更多的是关于我是否需要实现read_real4、read_real8、read_real16等。 -
我编辑了代码以使其更清晰。