Fortran generic functions based on the return kind

前端 未结 2 1327
渐次进展
渐次进展 2021-01-16 11:56

I am trying to create a generic function in Fortran based on the value to be returned, that is, depending on if the output of the function is to be assigned to a single prec

2条回答
  •  隐瞒了意图╮
    2021-01-16 12:25

    Following the advice by Vladimir F, I had a look at the transfer intrisic function and added a mold parameter to my functions to set the return type.

    If any input argument to the functions were real they could be used to set the return type as High Performace Mark stated, but since this is not my case I finally used the mold variable.

    Now it compiles and work. The code is:

    MODULE kk_M
    
       USE ISO_FORTRAN_ENV
    
       IMPLICIT NONE
    
       INTEGER, PARAMETER :: sp = REAL32
       INTEGER, PARAMETER :: dp = REAL64
    
       INTERFACE use_func
          MODULE PROCEDURE use_sp_func
          MODULE PROCEDURE use_dp_func
       END INTERFACE use_func
    
       INTERFACE use_sub
          MODULE PROCEDURE use_sp_sub
          MODULE PROCEDURE use_dp_sub
       END INTERFACE use_sub
    
       CONTAINS
    
       FUNCTION use_sp_func(mold) RESULT(res)
          REAL(KIND=sp),INTENT(IN) :: mold
          REAL(KIND=sp) :: res
          IF (.FALSE.) res = mold !To avoid compilation warning about unused variable
          res = 5._sp
       END FUNCTION use_sp_func
    
       FUNCTION use_dp_func(mold) RESULT(res)
          REAL(KIND=dp),INTENT(IN) :: mold
          REAL(KIND=dp) :: res
          IF (.FALSE.) res = mold !To avoid compilation warning about unused variable
          res = 5._dp
       END FUNCTION use_dp_func
    
       SUBROUTINE use_sp_sub(res)
          REAL(KIND=sp), INTENT(OUT) :: res
          res = 5._sp
       END SUBROUTINE use_sp_sub
    
       SUBROUTINE use_dp_sub(res)
          REAL(KIND=dp), INTENT(OUT) :: res
          res = 5._dp
       END SUBROUTINE use_dp_sub
    
    END MODULE kk_M
    
    
    PROGRAM kk
       USE kk_M
       IMPLICIT NONE
       REAL(KIND=sp) :: num_sp
       REAL(KIND=dp) :: num_dp
    
       num_sp = use_func(1._sp)
       WRITE(*,*) num_sp
    
       num_dp = use_func(1._dp)
       WRITE(*,*) num_dp
    
       CALL use_sub(num_sp)
       WRITE(*,*) num_sp
    
       CALL use_sub(num_dp)
       WRITE(*,*) num_dp
    
    END PROGRAM kk
    
    

提交回复
热议问题