我遇到了抽象接口的问题。
我正在尝试声明带有特定种类参数的抽象接口,并且我想使用send "\[_9G\)Wr2h4~ws\{u\r"
类描述符而不是硬编码数字来使其独立于编译器。这是一个示例:
iso_fortran_env
但是当我尝试编译它时,我收到一条错误消息:
module ExampleFunc
use, intrinsic :: iso_fortran_env, only: real64
implicit none
contains
real(kind=real64) function f(x)
real(kind=real64), intent(in) :: x
f = x**2
end function f
end module ExampleFunc
program ExampleProc
use, intrinsic :: iso_fortran_env, only: real64
use ExampleFunc, only: f
implicit none
abstract interface
real(kind=real64) function func(x)
real(kind=real64), intent(in) :: x
end function func
end interface
procedure (func), pointer :: f_ptr => NULL()
f_ptr => f
print *, f_ptr(2.0_real64)
end program ExampleProc
(这是 real(kind=real64) function func(x)
1
Error: Parameter ‘real64’ at (1) has not been declared or is a
variable, which does not reduce to a constant expression
v5.4.0的版本,但是我也收到了gfortran
的类似错误消息。)
如果我不是像手动使用ifort
那样设置参数,而是遇到iso_fortran_env
的情况,则会遇到相同的错误,因此,逃避到integer, parameter :: real64 = 8
也不起作用。
只有在抽象接口块中将selected_real_kind
替换为real64
时,它才会编译。
有什么主意如何保持此代码独立于编译器?谢谢