(j3.2006) Unlimited poly and pointer component
Malcolm Cohen
malcolm
Thu Dec 21 03:11:58 EST 2017
Case 1:
program main
Type dt
integer, pointer :: p => NULL()
end type
Type(dt) :: d1
end program
The standard says
[527:20]: "A derived-type scalar object is defined if and only if all of its nonpointer components are defined."
[529:28]: "(22) When a pointer becomes associated with a target that is defined, the pointer becomes defined."
[528:2]: "Variables that are not initially defined are initially undefined."
Question: Is d1 initially undefined?
Answer: I don?t care.
Extended answer: Just look at 19.6.1 and forget about the convoluted (and incomplete) stuff about ?initially defined/undefined?. According to your first quote, whether d1 is initially defined or not, it *clearly* is defined at the beginning of program execution.
module m
type t1
integer, pointer :: p => NULL()
contains
procedure, private :: t1Assign
generic, public :: assignment(=) => t1Assign
end type t1
type t2
type(t1) :: base
end type t2
contains
subroutine t1Assign(this, src)
implicit none
class(t1), intent(inout) :: this !!! what if intent(out)
class(t1), intent(in) :: src
print *, associated(this%p) !!! AAA
end subroutine
subroutine copy(dest, src)
class(*), pointer :: dest
class(*), intent(in) :: src
allocate(dest, source=src)
end subroutine copy
end module
program main
use m
class(*), pointer :: aa
type(t2) :: bb
allocate(bb%base%p)
call copy(aa, bb)
end
Question 1: Is "this" at line AAA defined?
Question 2: if the answer of 1 is YES, then what should it print?
It seems this%p is not nullified because the default initialization doesn't apply to unlimited polymorphic object (dest) nor to the intent(inout) (this). Doesn't it mean the call to associated is invalid?
Question 3: if the answer to 1 is YES, if I change "this" in "t1Assign" to "INTENT(OUT)"(so the default initialization is applied to "this"), is the call to associated then legal, and it should print false?
Answer 1: Question is meaningless since there is no execution path that reaches that line. Perhaps you need a quote from the standard here:
?On successful allocation, if allocate-object and source-expr have the same rank the value of allocate-object becomes the value provided?
That is, ?dest? gets the value of ?src?, without the involvement of intrinsic or defined assignment.
Answer 2: Question is meaningless because whether ?this? is ?defined? or not has literally *NO BEARING WHATSOEVER UNDER ANY CIRCUMSTANCES* on whether it is valid to execute ?ASSOCIATED(this%p)?, because being a pointer component, the association status and definition status of ?this%p? do not affect the definition status of ?this?. Your very first quote from the standard tells you this.
Answer 2: Of course your problem is that apparently some processor is invoking defined assignment when it is not allowed to.
Default initialisation would be invoked by
ALLOCATE(dest,MOLD=src)
but then you would need a SELECT TYPE to assign a value to dest, which is a bit inconvenient.
Cheers,
--
..............Malcolm Cohen, NAG Oxford/Tokyo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.j3-fortran.org/pipermail/j3/attachments/20171221/1bfef268/attachment-0001.html>
More information about the J3
mailing list