call aux(1, aux_output)
is_f2003 = aux_output > 1
end function is_f2003
recursive subroutine aux(depth, output)
integer depth
integer output
integer test(int(1.0))
save
test = 0
if(depth > 0) then
call aux(depth-1, output)
end if
test = test+1
output = test(1)
end subroutine aux
end module test_version
program test
use test_version
implicit none
write(*,'(a)') trim(merge('Version is f2003', &
'Version is f95 ', &
is_f2003()))
end program test
C:\gcc_mingw64a\clf\test_version>x86_64-pc-mingw32-gfortran -std=f95
test_versio
n.f90 -otest_version
C:\gcc_mingw64a\clf\test_version>test_version
Version is f2003
C:\gcc_mingw64a\clf\test_version>x86_64-pc-mingw32-gfortran -std=f2003
test_vers
ion.f90 -otest_version
C:\gcc_mingw64a\clf\test_version>test_version
Version is f2003
C:\gcc_mingw64a\clf\test_version>ifort /stand:f95 test_version.f90
Intel(R) Fortran Compiler for Intel(R) EM64T-based applications, Version 9.1
Build 20061104
Copyright (C) 1985-2006 Intel Corporation. All rights reserved.
Microsoft (R) Incremental Linker Version 8.00.40310.39
Copyright (C) Microsoft Corporation. All rights reserved.
-out:test_version.exe
-subsystem:console
test_version.obj
C:\gcc_mingw64a\clf\test_version>test_version
Version is f2003
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
Why do you expect this program to work differently for Fortran 95
and Fortran 2003?
You might be able to write a program that works differently for
Fortran 95 and Fortran 2003 by taking advantage of the difference
in the way floating-point zeros are formatted in list-directed
output. I wouldn't put much stock in such a program, because I
know of implementations going back to FORTRAN 77 that follow the
Fortran 2003 convention.
Bob Corbett
> Why do you expect this program to work differently for Fortran 95
> and Fortran 2003?
The point of the program is that the array test in subroutine aux
should not have the SAVE attribute in f95. If you comment out the
SAVE statement in subroutine aux, the program reports 'Version is f95'.
Some passages from ISO/IEC 1539-1:1997(E):
"An initialization expression is...
(4) An elemental intrinsic function reference of type integer or
character where each argument is an initialization expression of type
integer or character." (Section 7.1.6.1)
This is the key difference between f95 and f03: f03 allows elemental
intrinsics to have any type of argument so int(1.0) is an
initialization expression in f03 but not f95.
In section 5.1 (with corrigendum 1):
"The specification expression (7.1.6.2) of a char-len-param-value
(5.1.1.5) or an array-spec (5.1.2.4) shall be an initialization
expression unless it is in an interface body (12.3.2.1), the
specification part of a subprogram, or the type-spec of a FUNCTION
statement (12.5.2.2). If the data object being declared depends
on the value of a specification-expr that is not an initialization
expression and is not a dummy argument, such on object is called
an automatic data object."
I conclude the array test in subroutine aux is an automatic data
object.
The effect of this can be seen in section 5.1:
"Constraint: The SAVE attribute shall not be specified for an
object that is in a common block, a dummy argument, a procedure,
a function result, an automatic data object, or an object with
the PARAMETER attribute."
And then in section 5.2.4:
"A SAVE statement without a saved entity list is treated as though
it contained the names of all allowed items in the same scoping
unit."
Since automatic data object test in subroutine aux is constrained
not to be given the SAVE attribute, it is exempted from the set of
all allowed items, so it should not be affected by the SAVE
statement. Thus the SAVE statement should have no effect in f95
and so commenting it out should have no effect on a compiler which
is supposed to conform to the f95 standard. But it does have an
effect, so I think the compilers are in error. What do you think?
> <robert....@sun.com> wrote in message
> news:11774761-fbc3-44ff...@a3g2000prm.googlegroups.com...
> > Why do you expect this program to work differently for Fortran 95
> > and Fortran 2003?
>
> The point of the program is that the array test in subroutine aux
> should not have the SAVE attribute in f95.
Yep. It took me a while to figure that one out. I was tempted to post a
question much like Bob's, but I knew enough about James to figure there
was probably a subtle point I was missing. It eventually came to me.
Well, actually, I didn't go as far as checking against the definiton of
initialization expression, but I guessed that the difference might lie
there.
Congratulations on illustrating the first example I think I have *EVER*
seen of code that is valid both with and without the SAVE attribute, but
should give different results in the two cases. Other examples I have
seen that allege to be such things instead always turn out to be
examples of code that is invalid without the SAVE attribute. Thus it is
usually at least "safe" for the compiler to implement things by acting
as though everything in sight were SAVEd.
And, of course, it is combined with your typically careful diesecting of
the exact definitions of expression categories.
Subtle. It doesn't surprize me that some compilers get it wrong. I don't
know how hard it would be to get right. If it's very hard, I'd wonder
about the cost/benefit. I might even wonder whether it was worth an
interp to ask whether it was actually intended for there to be such a
distinction. After all, it isn't listed as an incompatibility (and I'd
wager quite large sums of money that this is because it didn't occur to
anyone involved). I'd have a hard time comming up with an answer I was
happy with to such an interp. I don't think an incompatibility was
intended, but it seems like it would be complicated to resolve the
incompatibility issue.
I'd sure not recommend that users write code that depended on that in
general... though I suppose I can see the desire to be able to
automatically detect the distinction between f95 and f2003 as a special
case. Still, it seems awfully fragile at best, as you managed to
demonstrate.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain