Fortran:在通用过程中处理不同类型的类型 [英] Fortran: Handling types with different kind in generic procedures
问题描述
有没有办法定义通用程序来动态处理 int
中的每个类型
, character
和 real
,而不必为每一个指定一个过程?我想这也是问在Fortran中是否存在 kind
多态性。
Is there a way to define generic procedures to dynamically handle each kind
of int
, character
and real
without having to specify a procedure for each one? I guess this is also asking whether kind
polymorphism exists in Fortran.
我想到的是接口是这样的:
I'm thinking of something that would be interfaced something like this:
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中的多态性似乎集中于派生类型,如波特兰集团的示例。我只对内在类型的通用处理感兴趣。
It seems that polymorphism in Fortran 2003 focuses on derived types, such as the examples from the Portland Group. I'm only interested in generic handling of intrinsic types.
这主要是为了好奇,因为我们几乎完全使用整数
和
字符
和 real(8)
。 (注意:我知道我们应该使用 iso_fortran_env
,但并不是我们所用的所有编译器都支持它。)
This is mainly for curiosity, since we work almost exclusively with defaults for integer
and character
, and real(8)
. (N.B. I know we should use iso_fortran_env
, but not all of the compilers we use have support for it.)
推荐答案
不,这是不可能的。甚至没有参数化派生类型。
No, it is not possible. Not even with parametrized derived types.
您必须手动创建每个特定的过程。预处理器有很多技巧可以帮助你做一些穷人的C ++ - 就像诱惑一样。甚至可以在StackOverflow上找到示例。请参阅 Fortran中的STL模拟。
You must manually create each specific procedure. There are many tricks with preprocessors which can assist you to do some poor-man's C++-like tempting. Examples can be found even on StackOverflow. See, among others, STL analogue in Fortran .
这篇关于Fortran:在通用过程中处理不同类型的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!