目标是创建一个可以处理任何类型的一级分配的分配例程。我们的代码库可以通过标准化错误捕获进行单一调用。Fortran中的多态阵列分配例程
编译器错误如下:
generic_allocation.f08:32:27:
call myAllocator (array_int, source_int, lambda)
1
Error: Actual argument to ‘myarray’ at (1) must be polymorphic
generic_allocation.f08:33:27:
call myAllocator (array_real, source_real, lambda)
1
Error: Actual argument to ‘myarray’ at (1) must be polymorphic
可以将此代码来解决?
测试代码试图分配一个整数数组,然后一个真正的数组:
module mAllocator
implicit none
contains
subroutine myAllocator (myArray, source_type, lambda)
class (*), allocatable, intent (inout) :: myArray (:)
class (*), intent (in) :: source_type
integer, intent (in) :: lambda
integer :: alloc_status = 0
character (len = 512) :: alloc_message = ''
allocate (myArray (1 : lambda), source = source_type, stat = alloc_status, errmsg = alloc_message)
if (alloc_status /= 0) then
write (*, "(' allocation errmsg = ', g0, '.')") trim (alloc_message)
stop 'Fatal error in subroutine myAllocator'
end if
end subroutine myAllocator
end module mAllocator
program generic_allocation
use mAllocator, only : myAllocator
implicit none
integer, parameter :: lambda = 10
integer, parameter :: source_int = 1
real, parameter :: source_real = 1.0
integer, allocatable :: array_int (:)
real, allocatable :: array_real (:)
call myAllocator (array_int, source_int, lambda)
call myAllocator (array_real, source_real, lambda)
end program generic_allocation
代码的第一个版本,如图FORTRAN: polymorphism allocation在一个select type
构建依赖。使用的另一个参考是Fortran polymorphism, functions and allocation。
的gfortran版本是6.0
$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/opt/gnu/6.0/libexec/gcc/x86_64-pc-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/opt/gnu/6.0 --enable-languages=c,c++,fortran,lto --disable-multilib --disable-werror
Thread model: posix
gcc version 6.0.0 20160227 (experimental) (GCC)
*目标是创建一个可以处理任何类型的一级分配的分配例程。*这不是“allocate”提供的吗? –