目標是創建一個可以處理任何類型的一級分配的分配例程。我們的代碼庫可以通過標準化錯誤捕獲進行單一調用。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」提供的嗎? –