Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scalar integer constant expression

36 views
Skip to first unread message

Erik Toussaint

unread,
Apr 29, 2011, 9:06:57 PM4/29/11
to
Is the following program standard conforming?

program p1

implicit none

integer, parameter :: i1 = kind(0)
integer, parameter :: i2(*) = [i1]

integer(kind=i2(1)) :: i3

i3 = int(0, i1)
print *, i3

i3 = int(0, i2(1)) ! This line gives an error when compiling.
print *, i3

end program p1


When I compile it with gfortran (4.7-20110423) I get the following error
message:

$ gfortran -c p1.f90
p1.f90:13.15:

i3 = int(0, i2(1)) ! This line gives an error when compiling.
1
Error: 'kind' argument of 'int' intrinsic at (1) must be a constant


Looking at the standard (N1830.pdf), the second argument to the 'int'
intrinsic function should be a 'scalar integer constant expression'.
Is an array element the same as a scalar? It is my understanding that it
is, and this is reinforced by the fact that in the declaration of i3
i2(1) is accepted as kind-selector, for which the standard requires a
'scalar-int-constant-expr'.

Erik.

Richard Maine

unread,
Apr 29, 2011, 10:42:46 PM4/29/11
to
Erik Toussaint <us...@example.net.invalid> wrote:

> Is the following program standard conforming?

Well, not until f2008 because of the * in

> integer, parameter :: i2(*) = [i1]

I think f2008 might allow that; nothing before does. Since I don't yet
have any f2003 compilers, I don't yet get too excited about f2008.

[most of the rest of the code elided]
...


> i3 = int(0, i2(1)) ! This line gives an error when compiling.

...


> Error: 'kind' argument of 'int' intrinsic at (1) must be a constant
>
>
> Looking at the standard (N1830.pdf), the second argument to the 'int'
> intrinsic function should be a 'scalar integer constant expression'.
> Is an array element the same as a scalar?

Yes (at least as of f90; it wasn't in f77, but this code is quite far
from f77).

The code looks fine to me other than the f2008 * feature. If you change
that to a 1, I think it is valid f2003 (the use of [] for an array
constructor makes in invalid f90/f95).

Note that the compiler's error message didn't say anything about scalar
anyway. It complained about needing a constant (which isn't actually
true as a constant expression is allowed - or in f2003 I think it is
probably an initialization expression).

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

glen herrmannsfeldt

unread,
Apr 29, 2011, 11:51:11 PM4/29/11
to
Richard Maine <nos...@see.signature> wrote:

> Erik Toussaint <us...@example.net.invalid> wrote:

> Well, not until f2008 because of the * in

>> integer, parameter :: i2(*) = [i1]

> I think f2008 might allow that; nothing before does. Since I don't yet
> have any f2003 compilers, I don't yet get too excited about f2008.

I didn't know about that one. One C feature that I have missed
for a long time* in Fortran is the ability to dimension an array
based on its initialization. In C it looks like:

double x[]={1., 2., 3.};

which dimensions the array 3, to match the initializers. Does

integer :: i2(*) = [i1]

work in Fortran 2008, too?

Or:

integer i2(*)
data i2/i1/

(since about 1972)

-- glen

Erik Toussaint

unread,
Apr 29, 2011, 11:51:58 PM4/29/11
to
On 30-4-2011 4:42, Richard Maine wrote:
> Erik Toussaint<us...@example.net.invalid> wrote:
>
>> Is the following program standard conforming?
>
> Well, not until f2008 because of the * in
>
>> integer, parameter :: i2(*) = [i1]
>
> I think f2008 might allow that; nothing before does. Since I don't yet
> have any f2003 compilers, I don't yet get too excited about f2008.

I think so too. The array-spec in the dimension attribute can now be, as
a fifth allowed alternative, an implied-shape-spec-list, where an
implied-shape-spec is [ lower-bound : ] *.

Thinking about your comment on f2003 compilers, couldn't one defend the
position that in the view of the standardization organization there is
simply _the_ Fortran standard, which, after its official publication
last year, is defined by the document specifying f2008. The foreword of
N1830 has the following text:
"This third edition cancels and replaces the second edition (ISO/IEC
1539-1:2004)"
So the document defining f2003 is no longer in effect, and when one is
working to create a 'Fortran compiler', one should work on implementing
the current form of the standard. The particular order in which the
different features will be implemented is up to the developers. They can
choose to focus first on implementing all features of f2003 (which is as
we all know practically entirely contained within f2008), but they may
just as well choose to work first on features that are new in f2008.

(I hope, and expect, that everyone here realizes that I mainly came up
with this viewpoint to create an a posteriori rationalization for my
failing to mention which standard I actually was talking about and my
careless cherry picking of features from different ones.)

I do agree though, that it is regrettable that a fully
standard-compliant fortran compiler has never been available during the
lifetime of f2003 (at least not to the 'general' public), and noone
knows if we will ever see one before the publication of the next edition
of the standard, but I guess that depends just as much on the schedule
of the standardization organization, as it does on the effort of
compiler developers.

>
> [most of the rest of the code elided]
> ...
>> i3 = int(0, i2(1)) ! This line gives an error when compiling.
> ...
>> Error: 'kind' argument of 'int' intrinsic at (1) must be a constant
>>
>>
>> Looking at the standard (N1830.pdf), the second argument to the 'int'
>> intrinsic function should be a 'scalar integer constant expression'.
>> Is an array element the same as a scalar?
>
> Yes (at least as of f90; it wasn't in f77, but this code is quite far
> from f77).
>
> The code looks fine to me other than the f2008 * feature. If you change
> that to a 1, I think it is valid f2003 (the use of [] for an array
> constructor makes in invalid f90/f95).
>
> Note that the compiler's error message didn't say anything about scalar
> anyway. It complained about needing a constant (which isn't actually
> true as a constant expression is allowed - or in f2003 I think it is
> probably an initialization expression).
>

You're right that the error message actually complains about needing a
constant; that's why I included the other assignment statement: the
difference between the two is that one uses a direct reference to a
scalar constant, and the other a reference to a constant array element
(which you confirmed to be a scalar).
So, in short, I think the compiler misdiagnoses this case, and should in
fact accept this form as valid. Correct? (Oh, wait, you already answered
that question.)

Thanks for your reply,
Erik.

Richard Maine

unread,
Apr 30, 2011, 1:35:58 AM4/30/11
to
Erik Toussaint <us...@example.net.invalid> wrote:

> On 30-4-2011 4:42, Richard Maine wrote:

...


> > I think f2008 might allow that; nothing before does. Since I don't yet
> > have any f2003 compilers, I don't yet get too excited about f2008.

...


> Thinking about your comment on f2003 compilers, couldn't one defend the
> position that in the view of the standardization organization there is
> simply _the_ Fortran standard, which, after its official publication
> last year, is defined by the document specifying f2008.

One could probably defend that, and lots of other things. It's not my
viewpoint. My viewpoint (which I've ranted about several times before,
so I'll keep it short) is that it was a mistake to publish f2008 as was
done. I'll not repeat details at the moment.

0 new messages