通用及杂项内在函数#
associated#
名称#
associated(3) - [状态:查询] 指针或指针/目标对的关联状态
概要#
result = associated(pointer [,target])
logical function associated(pointer,target)
type(TYPE(kind=KIND),pointer :: pointer
type(TYPE(kind=KIND),pointer,optional :: target
特性#
pointer 应具有指针属性,并且可以是任何类型或过程指针。
target 应为指针或目标。它必须与pointer具有相同的类型、种类类型参数和数组秩。
pointer和target的关联状态都不应未定义。
结果为默认逻辑值
描述#
associated(3) 确定指针pointer的状态,或pointer是否与目标target关联。
选项#
- pointer
要测试关联的指针。其指针关联状态不应未定义。
- target
要测试是否占用与指针pointer相同的存储单元的目标。也就是说,测试它是否由pointer指向。
结果#
associated(3f) 返回类型为逻辑的标量值。有几种情况
当可选的target不存在时,如果pointer与目标关联,则associated(pointer)为.true.;否则,它返回.false.。
如果target存在且为标量目标,则如果target不是零大小的存储序列并且与pointer关联的目标占用相同的存储单元,则结果为.true.。如果pointer已断开关联,则结果为.false.。
如果target存在且为数组目标,则如果target和pointer具有相同的形状,不是零大小的数组,是元素不是零大小的存储序列的数组,并且target和pointer在数组元素顺序中占用相同的存储单元,则结果为.true.。
与情况 2 一样,如果pointer已断开关联,则结果为.false.。
如果target存在且为标量指针,则如果target与pointer关联,与target关联的目标不是零大小的存储序列并且占用相同的存储单元,则结果为.true.。
如果target或pointer已断开关联,则结果为.false.。
如果target存在且为数组指针,则如果与pointer关联的目标和与target关联的目标具有相同的形状,不是零大小的数组,是元素不是零大小的存储序列的数组,并且target和pointer在数组元素顺序中占用相同的存储单元,则结果为.true.。
如果target存在且为过程,则当且仅当pointer与target关联,并且如果target是内部过程,它们具有相同的宿主实例时,结果为真。
如果target存在且为过程指针,则当且仅当pointer和target与相同的过程关联,并且如果该过程是内部过程,它们具有相同的宿主实例时,结果为真。
示例#
示例程序
program demo_associated
implicit none
real, target :: tgt(2) = [1., 2.]
real, pointer :: ptr(:)
ptr => tgt
if (associated(ptr) .eqv. .false.) &
& stop 'POINTER NOT ASSOCIATED'
if (associated(ptr,tgt) .eqv. .false.) &
& stop 'POINTER NOT ASSOCIATED TO TARGET'
end program demo_associated
标准#
Fortran 95
另请参见#
fortran-lang 内在函数描述 (许可证:MIT) @urbanjost
extends_type_of#
名称#
extends_type_of(3) - [状态:查询] 确定a的动态类型是否是mold的动态类型的扩展。
概要#
result = extends_type_of(a, mold)
logical extends_type_of(a, mold)
type(TYPE(kind=KIND),intent(in) :: a
type(TYPE(kind=KIND),intent(in) :: mold
特性#
-a 应为可扩展声明类型的对象或指向可扩展声明类型的指针,或无限多态的。如果它是多态指针,则其关联状态不应未定义。-mole 应为可扩展声明类型的对象或指向可扩展声明类型的指针,或无限多态的。如果它是多态指针,则其关联状态不应未定义。
结果为标量默认逻辑类型。
描述#
extends_type_of(3) 为 .true. 当且仅当a的动态类型是或可能是(对于无限多态)mold的动态类型的扩展。
注释1#
已断开关联的指针或未分配的可分配变量的动态类型是其声明类型。
注释2#
extends_type_of执行的测试与类型保护class is执行的测试不同。extends_type_of执行的测试不考虑种类类型参数。
选项#
- a
为可扩展声明类型的对象或无限多态的。如果它是多态指针,则其关联状态不应未定义。
- mold
为可扩展声明类型的对象或无限多态的。如果它是多态指针,则其关联状态不应未定义。
结果#
如果mold是无限多态的,并且是已断开关联的指针或未分配的可分配变量,则结果为真。
否则,如果a是无限多态的,并且是已断开关联的指针或未分配的可分配变量,则结果为假。
否则,当且仅当a的动态类型
如果 A 或 MOLD 的动态类型是可扩展的,则当且仅当 A 的动态类型是 MOLD 的动态类型的扩展类型时,结果为真;否则结果为处理器相关的。
示例#
示例程序
! program demo_extends_type_of
module M_demo_extends_type_of
implicit none
private
type nothing
end type nothing
type, extends(nothing) :: dot
real :: x=0
real :: y=0
end type dot
type, extends(dot) :: point
real :: z=0
end type point
type something_else
end type something_else
public :: nothing
public :: dot
public :: point
public :: something_else
end module M_demo_extends_type_of
program demo_extends_type_of
use M_demo_extends_type_of, only : nothing, dot, point, something_else
implicit none
type(nothing) :: grandpa
type(dot) :: dad
type(point) :: me
type(something_else) :: alien
write(*,*)'these should all be true'
write(*,*)extends_type_of(me,grandpa),'I am descended from Grandpa'
write(*,*)extends_type_of(dad,grandpa),'Dad is descended from Grandpa'
write(*,*)extends_type_of(me,dad),'Dad is my ancestor'
write(*,*)'is an object an extension of itself?'
write(*,*)extends_type_of(grandpa,grandpa) ,'self-propagating!'
write(*,*)extends_type_of(dad,dad) ,'clone!'
write(*,*)' you did not father your grandfather'
write(*,*)extends_type_of(grandpa,dad),'no paradox here'
write(*,*)extends_type_of(dad,me),'no paradox here'
write(*,*)extends_type_of(grandpa,me),'no relation whatsoever'
write(*,*)extends_type_of(grandpa,alien),'no relation'
write(*,*)extends_type_of(me,alien),'not what everyone thinks'
call pointers()
contains
subroutine pointers()
! Given the declarations and assignments
type t1
real c
end type
type, extends(t1) :: t2
end type
class(t1), pointer :: p, q
allocate (p)
allocate (t2 :: q)
! the result of EXTENDS_TYPE_OF (P, Q) will be false, and the result
! of EXTENDS_TYPE_OF (Q, P) will be true.
write(*,*)'(P,Q)',extends_type_of(p,q),"mind your P's and Q's"
write(*,*)'(Q,P)',extends_type_of(q,p)
end subroutine pointers
end program demo_extends_type_of
结果
these should all be true
T I am descended from Grandpa
T Dad is descended from Grandpa
T Dad is my ancestor
is an object an extension of itself?
T self-propagating!
T clone!
you did not father your grandfather
F no paradox here
F no paradox here
F no relation whatsoever
F no relation
F not what everyone thinks
(P,Q) F mind your P's and Q's
(Q,P) T
标准#
Fortran 2003
另请参见#
fortran-lang 内在函数描述 (许可证:MIT) @urbanjost
is_iostat_end#
名称#
is_iostat_end(3) - [状态:查询] 测试文件结束值
概要#
result = is_iostat_end(i)
elemental logical function is_iostat_end(i)
integer,intent(in) :: i
特性#
i 为任何种类的整数
返回值为默认逻辑
描述#
is_iostat_end(3) 测试变量(假设从 I/O 语句返回为状态)是否具有“文件结束”I/O 状态值。
该函数等效于将变量与内在模块iso_fortran_env的iostat_end参数进行比较。
选项#
- i
要测试的整数状态值,以指示文件结束。
结果#
如果i具有指示iostat=说明符的文件结束条件的值,则返回.true.,否则返回.false.。
示例#
示例程序
program demo_iostat
implicit none
real :: value
integer :: ios
character(len=256) :: message
write(*,*)'Begin entering numeric values, one per line'
do
read(*,*,iostat=ios,iomsg=message)value
if(ios.eq.0)then
write(*,*)'VALUE=',value
elseif( is_iostat_end(ios) ) then
stop 'end of file. Goodbye!'
else
write(*,*)'ERROR:',ios,trim(message)
exit
endif
!
enddo
end program demo_iostat
标准#
Fortran 2003
另请参见#
fortran-lang 内在函数描述 (许可证:MIT) @urbanjost
is_iostat_eor#
名称#
is_iostat_eor(3) - [状态:查询] 测试记录结束值
概要#
result = is_iostat_eor(i)
elemental integer function is_iostat_eor(i)
integer(kind=KIND),intent(in) :: i
特征#
i 为任何种类的整数
返回值为默认逻辑
描述#
is_iostat_eor(3) 测试变量是否具有 I/O 状态“记录结束”的值。该函数等效于将变量与内在模块 iso_fortran_env 的 iostat_eor 参数进行比较。
选项#
- i
要测试的值,表示“记录结束”。
结果#
当且仅当 i 的值为 iostat= 说明符指示的记录结束条件时,返回 .true.,否则返回 .false.。
示例#
示例程序
program demo_is_iostat_eor
use iso_fortran_env, only : iostat_eor
implicit none
integer :: inums(5), lun, ios
! create a test file to read from
open(newunit=lun, form='formatted',status='scratch')
write(lun, '(a)') '10 20 30'
write(lun, '(a)') '40 50 60 70'
write(lun, '(a)') '80 90'
write(lun, '(a)') '100'
rewind(lun)
do
read(lun, *, iostat=ios) inums
write(*,*)'iostat=',ios
if(is_iostat_eor(ios)) then
stop 'end of record'
elseif(is_iostat_end(ios)) then
print *,'end of file'
exit
elseif(ios.ne.0)then
print *,'I/O error',ios
exit
endif
enddo
close(lun,iostat=ios,status='delete')
end program demo_is_iostat_eor
结果
> iostat= 0
> iostat= -1
> end of file
标准#
Fortran 2003
另请参见#
Fortran 语言内在描述
move_alloc#
名称#
move_alloc(3) - [内存] 将分配从一个对象移动到另一个对象
概要#
call move_alloc(from, to [,stat] [,errmsg] )
subroutine move_alloc(from, to)
type(TYPE(kind=**)),intent(inout),allocatable :: from(..)
type(TYPE(kind=**)),intent(out),allocatable :: to(..)
integer(kind=**),intent(out) :: stat
character(len=*),intent(inout) :: errmsg
特征#
from 可以是任何类型和种类。
to 的类型、种类和秩应与 from 相同。
描述#
move_alloc(3) 将分配从 from 移动到 to。在此过程中,from 将被释放分配。
这可能比其他将 from 中的值分配给 to 并显式释放 from 的方法更有效率,因为这些方法更有可能需要一个临时对象或数组元素的副本。
选项#
- from
要移动到 to 并释放分配的数据对象。
- to
目标数据对象,将分配的数据对象 from 移动到该对象。通常,它的形状与 from 不同。
- stat
如果 stat 存在且执行成功,则将其赋值为零。
如果发生错误条件,则
o 如果 stat 不存在,则启动错误终止;o 否则,如果 from 是一个共数组,并且当前团队包含一个已停止的映像,则 stat 被分配内在模块 ISO_FORTRAN_ENV 中的 STAT_STOPPED_IMAGE 值;o 否则,如果 from 是一个共数组,并且当前团队包含一个失败的映像,并且没有发生其他错误条件,则 stat 被分配内在模块 ISO_FORTRAN_ENV 中的 STAT_FAILED_IMAGE 值;o 否则,stat 被分配一个与 STAT_STOPPED_IMAGE 或 STAT_FAILED_IMAGE 不同的处理器相关的正值。
- errmsg
如果 errmsg 参数存在并且发生错误条件,则将其分配一个解释性消息。如果未发生错误条件,则 errmsg 的定义状态和值将保持不变。
示例#
分配更大网格的基本示例程序
program demo_move_alloc
implicit none
! Example to allocate a bigger GRID
real, allocatable :: grid(:), tempgrid(:)
integer :: n, i
! initialize small GRID
n = 3
allocate (grid(1:n))
grid = [ (real (i), i=1,n) ]
! initialize TEMPGRID which will be used to replace GRID
allocate (tempgrid(1:2*n)) ! Allocate bigger grid
tempgrid(::2) = grid ! Distribute values to new locations
tempgrid(2::2) = grid + 0.5 ! initialize other values
! move TEMPGRID to GRID
call MOVE_ALLOC (from=tempgrid, to=grid)
! TEMPGRID should no longer be allocated
! and GRID should be the size TEMPGRID was
if (size (grid) /= 2*n .or. allocated (tempgrid)) then
print *, "Failure in move_alloc!"
endif
print *, allocated(grid), allocated(tempgrid)
print '(99f8.3)', grid
end program demo_move_alloc
结果
T F
1.000 1.500 2.000 2.500 3.000 3.500
标准#
Fortran 2003,STAT 和 ERRMSG 选项于 2018 年添加
另请参见#
Fortran 语言内在描述
present#
名称#
present(3) - [状态:查询] 确定是否指定了可选的虚参
概要#
result = present(a)
logical function present (a)
type(TYPE(kind=KIND)) :: a(..)
特征#
a 可以是任何类型,可以是指针、标量或数组值,或虚过程。
描述#
present(3) 可用于过程以确定在对过程的当前调用中是否存在可选的虚参。
a 应是出现在 present(3) 函数引用所在的子程序中可访问的可选虚参的名称。对 a 没有其他要求。
注意,当在调用当前过程时不存在参数时,您只能将其作为可选参数传递给另一个过程,或将其作为参数传递给 present。
选项#
- a
当前子程序或函数中可访问的可选虚参的名称。
结果#
如果可选参数 a 存在(已在对过程的调用中传递),则返回 .true.,否则返回 .false.。
示例#
示例程序
program demo_present
implicit none
integer :: answer
! argument to func() is not present
answer=func()
write(*,*) answer
! argument to func() is present
answer=func(1492)
write(*,*) answer
contains
!
integer function func(x)
! the optional characteristic on this definition allows this variable
! to not be specified on a call; and also allows it to subsequently
! be passed to PRESENT(3):
integer, intent(in), optional :: x
integer :: x_local
!
! basic
if(present(x))then
! if present, you can use x like any other variable.
x_local=x
else
! if not, you cannot define or reference x except to
! pass it as an optional parameter to another procedure
! or in a call to present(3f)
x_local=0
endif
!
func=x_local**2
!
! passing the argument on to other procedures
! so something like this is a bad idea because x is used
! as the first argument to merge(3f) when it might not be
! present
! xlocal=merge(x,0,present(x)) ! NO!!
!
! We can pass it to another procedure if another
! procedure declares the argument as optional as well,
! or we have tested that X is present
call tattle('optional argument x',x)
if(present(x))call not_optional(x)
end function
!
subroutine tattle(label,arg)
character(len=*),intent(in) :: label
integer,intent(in),optional :: arg
if(present(arg))then
write(*,*)label,' is present'
else
write(*,*)label,' is not present'
endif
end subroutine tattle
!
subroutine not_optional(arg)
integer,intent(in) :: arg
write(*,*)'already tested X is defined',arg
end subroutine not_optional
!
end program demo_present
结果
optional argument x is not present
0
optional argument x is present
already tested X is defined 1492
2226064
标准#
Fortran 95
fortran-lang 内在函数描述 (许可证:MIT) @urbanjost
same_type_as#
名称#
same_type_as(3) - [状态:查询] 查询动态类型是否相等
概要#
result = same_type_as(a, b)
logical same_type_as(a, b)
type(TYPE(kind=KIND),intent(in) :: a
type(TYPE(kind=KIND),intent(in) :: b
特征#
a 应是可扩展声明类型或无限多态的对象。如果它是多态指针,则其关联状态不得未定义。
b 应是可扩展声明类型或无限多态的对象。如果它是多态指针,则其关联状态不得未定义。
描述#
same_type_as(3) 查询对象的动态类型是否相等。
选项#
- a
要与 b 比较以检查类型是否相等的对象
- b
要与其比较以检查类型是否相等的对象
结果#
如果 a 或 b 的动态类型是可扩展的,则当且仅当 a 的动态类型与 b 的动态类型相同时,结果为真。如果 a 和 b 都没有可扩展的动态类型,则结果取决于处理器。
NOTE1
与已断开关联的指针或未分配的可分配变量的动态类型是其声明类型。无限多态实体没有声明类型。
NOTE2
SAME_TYPE_AS 执行的测试与类型保护 TYPE IS 执行的测试不同。SAME_TYPE_AS 执行的测试不考虑种类类型参数。
示例程序
! program demo_same_type_as
module M_ether
implicit none
private
type :: dot
real :: x=0
real :: y=0
end type dot
type, extends(dot) :: point
real :: z=0
end type point
type something_else
end type something_else
public :: dot
public :: point
public :: something_else
end module M_ether
program demo_same_type_as
use M_ether, only : dot, point, something_else
implicit none
type(dot) :: dad, mom
type(point) :: me
type(something_else) :: alien
write(*,*)same_type_as(me,dad),'I am descended from Dad, but equal?'
write(*,*)same_type_as(me,me) ,'I am what I am'
write(*,*)same_type_as(dad,mom) ,'what a pair!'
write(*,*)same_type_as(dad,me),'no paradox here'
write(*,*)same_type_as(dad,alien),'no relation'
call pointers()
contains
subroutine pointers()
! Given the declarations and assignments
type t1
real c
end type
type, extends(t1) :: t2
end type
class(t1), pointer :: p, q, r
allocate (p, q)
allocate (t2 :: r)
! the result of SAME_TYPE_AS (P, Q) will be true, and the result
! of SAME_TYPE_AS (P, R) will be false.
write(*,*)'(P,Q)',same_type_as(p,q),"mind your P's and Q's"
write(*,*)'(P,R)',same_type_as(p,r)
end subroutine pointers
end program demo_same_type_as
结果
F I am descended from Dad, but equal?
T I am what I am
T what a pair!
F no paradox here
F no relation
(P,Q) T mind your P's and Q's
(P,R) F
标准#
Fortran 2003
另请参见#
Fortran 语言内在描述