I’m writing a procedure that has as an input a function with a defined interface. Is it possible to declare a variable in this procedure to be the same type as an argument to the function, an equivalent of decltype in C++?
Example
module numerical
implicit none
abstract interface
function value_fun(v, err)
real(8), intent(in) :: v
integer, optional, intent(out) :: err ! 0 if no error
end function value_fun
end interface
contains
subroutine my_proc(f)
procedure(value_fun) :: f
! Declare x to be the same type as the first argument of f
! typeof<f,1> :: x
end subroutine my_proc
end module numerical
Not answering your post, but it made me wonder if the opposite holds when combined with typeof. Say:
module numerical
implicit none
contains
subroutine my_proc(f)
real(8) :: x
interface
function f(v, err)
import
typeof(x), intent(in) :: v
integer, optional, intent(out) :: err ! 0 if no error
end function
end interface
...
end subroutine
end module numerical
Would that be valid code (once we find a compiler with typeof support)?
I wanted to do something similar when I wrote benchmark.f, and I managed to achieve something very close with a combination of unlimited polymorphism (class(*)) and associate. Unfortunately the code does not seem to be portable and only works as expected with Intel compilers and not with gfortran. See the section Associate and unlimited polymorphism.
To know more about the experimentations and the work around you can have a look at the Method derived type.