I ran into a somewhat curious syntactical problem (or it is a problem
with the compiler I am using):
program test_print
call print( (/ /) )
contains
subroutine print( array )
real, dimension(:) :: array
write(*,*) array
end subroutine print
end program test_print
In this program I construct an empty array (in the actual program I
simply do not need it).
The Intel Fortran compiler, version 11.1, does not accept this (the
error message is not
related to array constructors though - it seems to expect a keyword
instead of /), which
makes me suspicious).
The gfortran compiler claims an empty array constructor is not
allowed.
My question: is this indeed the case? I can imagine there is a problem
in determining the
type of such a constructed array - is the only solution to define a
variable of type "real, dimension(0)" then?
Regards,
Arjen
! Requires /assume:realloc_lhs with ifort 11.1
PROGRAM IFeelReallyEmpty
IMPLICIT NONE
REAL, ALLOCATABLE :: a(:)
!****
a = [ REAL:: ]
PRINT *, SIZE(a)
END PROGRAM IFeelReallyEmpty
> The gfortran compiler claims an empty array constructor is not
> allowed.
>
> My question: is this indeed the case? I can imagine there is a problem
> in determining the
> type of such a constructed array - is the only solution to define a
> variable of type "real, dimension(0)" then?
gfortran is correct: An empty array constructor without a type-spec is
invalid as there the type of the array is not known. That causes
problems, e.g., when the compiler has to decide which of the generic
procedures should be called.
The solution is (cf. Ian's answer - though it is a bit hidden there)
to use a type-spec, e.g.
call print( (/ real :: /) )
which is a Fortran 2003 feature. In the standard it is encoded in the
following lines:
R468 array-constructor is (/ ac-spec /)
or lbracket ac-spec rbracket
R469 ac-spec is type-spec ::
or [type-spec ::] ac-value-list
Tobias
PS: I have filled a gfortran problem report (PR 47040) to print a more
helpful error message by mentioning "type-spec".
Ah, so the message from the Intel Fortran compiler was actually quite
correct: it was looking for an indication of the type.
Thank you both for the clarification.
Regards,
Arjen
Or if you want to stick with f95
Wot now ? cat z.f90
Program z
Implicit None
Integer :: i
Write( *, * ) Size( (/ ( i, i = 1, 0 ) /) )
End Program z
Wot now ? nagfor z.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now ? ./a.out
0
Wot now ? gfortran -W -Wall -std=f95 -fbounds-check z.f90
Wot now ? ./a.out
0
Wot now ? ifort -stand f95 z.f90
Wot now ? ./a.out
0
Ian
(talking to myself as usual)
Or better -
Program z
Implicit None
Write( *, * ) Size( Pack( (/ 1 /), (/ 2 /) == 3 ) )
End Program z
Wot now ? nagfor z.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now ? ./a.out
0
Wot now ? gfortran -W -Wall -std=f95 -fbounds-check z.f90
Wot now ? ./a.out
0
Wot now ? ifort -stand f95 z.f90
Wot now ? ./a.out
0
1) No var required
2) It uses pack, therefore is clf approved
Combine with reshape as required.
All it needs now is transfer,
Ian