Fortran:在通用过程中处理不同种类的类型
Fortran: Handling types with different kind in generic procedures
有没有一种方法可以定义通用过程来动态处理 int
、character
和 real
中的每个 kind
,而不必为每个过程指定一个过程?我想这也是在询问 Fortran 中是否存在 kind
多态性。
我正在考虑可以像这样连接的东西:
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)
的默认值。 (N.B。我知道我们应该使用 iso_fortran_env
,但并非我们使用的所有编译器都支持它。)
不,这是不可能的。甚至没有参数化的派生类型。
您必须手动创建每个特定过程。预处理器有很多技巧可以帮助您完成一些类似 C++ 的简单操作。示例甚至可以在 Whosebug 上找到。参见 STL analogue in Fortran .
有没有一种方法可以定义通用过程来动态处理 int
、character
和 real
中的每个 kind
,而不必为每个过程指定一个过程?我想这也是在询问 Fortran 中是否存在 kind
多态性。
我正在考虑可以像这样连接的东西:
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)
的默认值。 (N.B。我知道我们应该使用 iso_fortran_env
,但并非我们使用的所有编译器都支持它。)
不,这是不可能的。甚至没有参数化的派生类型。
您必须手动创建每个特定过程。预处理器有很多技巧可以帮助您完成一些类似 C++ 的简单操作。示例甚至可以在 Whosebug 上找到。参见 STL analogue in Fortran .