Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
The Intel sign-in experience is changing in February to support enhanced security controls. If you sign in, click here for more information.
27558 Discussions

Problem with overloaded assignment when LHS is allocatable array

Vivek_R_
Novice
309 Views

For the code

 

module assignment_mod
implicit none
interface assignment(=)
   module procedure int_from_char
end interface
contains
elemental subroutine int_from_char(i,s)
integer, intent(out) :: i
character (len=*), intent(in) :: s
read (s,*) i
end subroutine int_from_char
end module assignment_mod
!
program test_assignment
use assignment_mod, only: assignment(=)
implicit none
integer :: j,k(2)
integer, allocatable :: ialloc(:)
j = "4"
print*,"j=",j
k = ["4","9"]
print*,"k=",k
ialloc = ["4","9"]
print*,"ialloc=",ialloc
end program test_assignment

compiling and running gives

c:\fortran\test>ifort assignment.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.5.0 Build 20211109_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:assignment.exe 
-subsystem:console 
assignment.obj 

c:\fortran\test>assignment
 j=           4
 k=           4           9
 ialloc=

which looks wrong for ialloc. Using the options -stand:f18 -standard-semantics does not change the result.

 

 

0 Kudos
1 Solution
FortranFan
Honored Contributor II
290 Views

@Vivek_R_ , the code as shown in the original post does not conform to the Fortran language standard.  In this case, the standard essentially places the onus on the program (and by extension its author) to conform, the processor (compiler being part of it) is not required to issue diagnostics.

And the issue is this: with the defined assignment as specified, the standard semantics requires the 'ialloc` to be allocated to the right shape prior to assignment on line 23: "ialloc = ["4","9"]".

You can retry with `allocate( ialloc(2) )` statement preceding the above assignment and check the program response.

By the way, if your interest is in Fortran generally and to consider aspects beyond Intel compiler implementation, take a look at the Fortran Discourse site: https://fortran-lang.discourse.group/  There have been recent threads there on defined assignment in standard Fortran and the intricacies with it.  There is a parallel to your situation.

View solution in original post

3 Replies
FortranFan
Honored Contributor II
291 Views

@Vivek_R_ , the code as shown in the original post does not conform to the Fortran language standard.  In this case, the standard essentially places the onus on the program (and by extension its author) to conform, the processor (compiler being part of it) is not required to issue diagnostics.

And the issue is this: with the defined assignment as specified, the standard semantics requires the 'ialloc` to be allocated to the right shape prior to assignment on line 23: "ialloc = ["4","9"]".

You can retry with `allocate( ialloc(2) )` statement preceding the above assignment and check the program response.

By the way, if your interest is in Fortran generally and to consider aspects beyond Intel compiler implementation, take a look at the Fortran Discourse site: https://fortran-lang.discourse.group/  There have been recent threads there on defined assignment in standard Fortran and the intricacies with it.  There is a parallel to your situation.

IanH
Black Belt
262 Views

For what it is subjectively worth, I would regard use of defined assignment for the example code as questionable style.  Be explicit about conversions and constructions.

 

 

 

...
elemental function int_from_char(s) result(i)
  character(*), intent(in) :: s
  integer :: i
  read (s, *) i
end function int_from_char
...

ialloc = int_from_char(['4','9'])

 

 

 

Defined assignment has its uses, but not this.

Vivek_R_
Novice
242 Views

@IanH Yes, thanks. I have not used defined assignment in serious codes but just want to see how it works. I think the good use for it is when you want control over how one derived type, possibly with pointer and allocatable components, is assigned to another.

Reply