Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29279 Discussions

procedure pointer in derived type to function/subroutine

zhouxian
Beginner
537 Views
I use ifort 11.1
type has component which is a procedure pointer to function. The function will return an array.But by calling the pointer , I got trash value. When I tried to find the size of returned array from the call from the procedure pointer,the compiler showed error " An array-valued argument is required in this context."
If I change function to the form of subroutine, there is no such a problem.
If I use g95 , everything is OK.
------------source code--------------------
module type_module
implicit none

type test_type
procedure(fun_interface), nopass, pointer :: fun_ptr
procedure(sub_interface), nopass, pointer :: sub_ptr
end type test_type

abstract interface
function fun_interface(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end function fun_interface
subroutinesub_interface(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end subroutine sub_interface
end interface

contains

subroutinetest_type_constructor(test, fun,sub)
interface
function fun(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end function fun
subroutinesub(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end subroutine sub
end interface

type(test_type) :: test
test%fun_ptr => fun;
test%sub_ptr => sub;
end subroutine test_type_constructor
end module type_module

module funcs
implicit none

contains

function fun1 (n,x ) result (f )
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
! try this
f = 2.0*x ;
! or this , both give WRONG results
callsub1(n,x,f)
end function fun1

subroutine sub1(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
f = 2.0*x ;
end subroutine sub1
end module funcs

program main
use type_module
use funcs
type(test_type) :: test
integer :: n =2 ;
double precision :: x(2), f(2)
call test_type_constructor (test, fun1, sub1)
x = (/-1.d0, 1.d0/);
f = 0.d0;
calltest%sub_ptr(n,x,f)
print *, " f from call fun ", f;

f = test%fun_ptr(n,x);
print *, " f from call fun ", f;
! This does not work in ifort.
print *, " size of returned value from fun_ptr ", size(test%fun_ptr(n,x));
end program
0 Kudos
3 Replies
Ron_Green
Moderator
537 Views

this has been a problem area. Let me investigate.

ron

0 Kudos
Ron_Green
Moderator
537 Views

issue number is DPD200151942

simplified case to:

module type_module
implicit none

abstract interface
function fun_interface(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end function fun_interface

subroutine sub_interface(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x(n)
double precision :: f(n)
end subroutine sub_interface
end interface

type test_type
procedure(fun_interface), nopass, pointer :: fun_ptr
procedure(sub_interface), nopass, pointer :: sub_ptr
end type test_type

end module type_module


program main
use type_module
type(test_type) :: test
integer :: n = 2 ;
double precision :: x(2), f(2)

! This does not work in ifort.
print *, " size of returned value from fun_ptr ", size(test%fun_ptr(n,x));

end program

0 Kudos
Ron_Green
Moderator
537 Views
I noticed I did not close out this issue.

This bug was fixed in the 12.0 compiler. It was also fixed in the 11.1 Update 7, 11.1.073 compiler

closing this now.

ron
0 Kudos
Reply