Procedure for any type of array in Fortran

前端 未结 2 1987
孤街浪徒
孤街浪徒 2021-01-22 05:59

Basis: I want to write a procedure which make some operation for any input array type: integer, real(4), real(8), etc. The only idea i read on Stac

2条回答
  •  青春惊慌失措
    2021-01-22 06:15

    This is the same situation as with the MPI procedures. The key is (at least in Fortran 2008 and older) to use implicit interface to the routine. In that case no argument checking takes place and you can pass whatever you want. You have the implicit interface if the procedure is external.This is the same situation as with the MPI procedures. The key is (at least in Fortran 2008 and older) to use implicit interface to the routine. In that case no argument checking takes place and you can pass whatever you want. You have the implicit interface if the procedure is external.

    Example:

         subroutine store(A,count,sizeof)
             use iso_fortran_env, only: int8
             integer, intent(in) :: count, sizeof
             integer(int8) :: A(count*sizeof)
             integer :: u
    
             !here you can treat A just as an array of count*sizeof bytes
           open(newunit=u,file=out.bin,access=stream)
            write(u)  A
            close(u)
    
         end subroutine
    
         program p
           real :: A(5,6)
           call store(A, size(A), storage_size(A(1,1))/8)
    
         end program
    

    When you need the explicit interface, you could (untested) do something like (not supported by many compilers, beyond Fortran 2008):

        subroutine typebound(self, A, count, sizeof)
          class(my_class) :: self
          TYPE(*),DIMENSION(..) :: A
          integer, intent(in) :: count, sizeof
    
          !subroutine
          call store(A, count, sizeof)
    

    or use the directives which a few compilers support:

        !GCC$ attributes no_arg_check::A
        !DEC$ attributes no_arg_check::A
    

    this disables the check of the arguments.

提交回复
热议问题