I am trying to write a generic type-bound procedure that takes different callback functions as parameters. When compiling following code (with ifort 12.1.3) I get the warning below:
module test
type :: a_type
contains
procedure :: t_s => at_s
procedure :: t_d => at_d
generic :: t => t_s,t_d
end type a_type
abstract interface
integer function cb_s(arg)
real(4) :: arg
end function cb_s
integer function cb_d(arg)
real(8) :: arg
end function cb_d
end interface
contains
subroutine at_s(this,cb)
class(a_type) :: this
procedure(cb_s) :: cb
end subroutine
subroutine at_d(this,cb)
class(a_type) :: this
procedure(cb_d) :: cb
end subroutine
end module test
The warning:
compileme.f(27): warning #8449: The type/rank/keyword signature for this specific
procedure matches another specific procedure that shares the same generic
binding name. [AT_D]
It seems like the compiler is not differentiating between different function interfaces when used as procedure arguments...
My question is: Why aren't those types checked and what is the correct, clean way to write generic type-bound procedures with procedures or procedure pointers as arguments?
Possible solution
As Vladimir F pointed out, only the callback function's return arguments are type checked. In my case it is OK to then just slightly change the functions' interfaces:
abstract interface
real(4) function cb_s(arg)
real(4) :: arg
end function cb_s
real(8) function cb_d(arg)
real(8) :: arg
end function cb_d
end interface
The compiler is right, because Fortran 2008's 12.4.3.4.5 Restrictions on generic declarations has
Two dummy arguments are distinguishable if - one is a procedure and the other is a data object, - they are both data objects or known to be functions, and neither is TKR compatible with the other, - one has the ALLOCATABLE attribute and the other has the POINTER attribute, or - one is a function with nonzero rank and the other is not known to be a function.
It means that both your functions are integer functions so they are not distinguishable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With