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

function return value is CLASS(*)

Julian_H_
Beginner
599 Views

Hello,

i have a function which returns CLASS(*). Overall it is working fine except if the returned value is from type CHARACTER(*). Here is a minimal example:

MODULE test_mod
  
CONTAINS
  FUNCTION test() RESULT(result)
  IMPLICIT NONE
  
  CLASS(*), POINTER :: result

  ALLOCATE(result, source='test')
  END FUNCTION Test
END MODULE test_mod

!PROGRAM class_void_bug_not_working
!  USE test_mod
!  IMPLICIT NONE
!  
!  ! no need to declare pointer!
!  SELECT TYPE (pointer => test())
!  TYPE IS (CHARACTER(*))
!    WRITE(*,*) pointer
!  END SELECT
!END PROGRAM

PROGRAM class_void_bug_working
  IMPLICIT NONE
  
  CLASS(*), ALLOCATABLE :: result
  ALLOCATE(result, source='test')
  
  ! still no need for pointer to be declared
  SELECT TYPE (pointer => result)
  TYPE IS (CHARACTER(*))
    WRITE(*,*) pointer
  END SELECT
END PROGRAM

!PROGRAM class_void_bug_working_with_function
!  USE test_mod
!  IMPLICIT NONE
!  
!  ! now pointer has to be declared
!  CLASS(*), POINTER :: pointer
!  
!  pointer => test()
!  
!  SELECT TYPE (pointer)
!  TYPE IS (CHARACTER(*))
!    WRITE(*,*) pointer
!  END SELECT
!END PROGRAM

I was able to compile a working version, but there are still a few things I do not understand (see source code)

0 Kudos
5 Replies
Steve_Lionel
Honored Contributor III
599 Views

In the first case, "pointer" is the associate-name, which becomes declared with the type and (many of the) attributes of the selector ("result"). In the second case, "pointer" is both the associate-name and the selector - this requires the selector to be a named variable and hence declared (since you have IMPLICIT NONE).

0 Kudos
Julian_H_
Beginner
599 Views

thank you so far. 

What about the compiling but not working Program:

PROGRAM class_void_bug_not_working
  USE test_mod
  IMPLICIT NONE
  
  SELECT TYPE (pointer => test())
  TYPE IS (CHARACTER(*))
    WRITE(*,*) pointer
  END SELECT
END PROGRAM

If the function test() is giving a real or an integer back the example prints correct to cmd, only if it is a character it is not working-

0 Kudos
FortranFan
Honored Contributor II
599 Views

Julian H. wrote:

thank you so far. 

What about the compiling but not working Program .. only if it is a character it is not working-

I think it's compiler bug in Intel Fortran compiler 18.0 Update 3 and perhaps earlier versions also.

However the issue appears to have been fixed in 19.0 Beta Update 1.

0 Kudos
Julian_H_
Beginner
599 Views

So I guess there is no need to file a bug report.

0 Kudos
Steve_Lionel
Honored Contributor III
599 Views

My advice is to always file a bug report, if for no other reason to add your test case to the regression test set.

0 Kudos
Reply