Can `real()` be used in a specification expression?

I recently came across a compiler that believes that the following MWE

character(len=nint(real(1))) :: c = 'a'

is not standards compliant because “Intrinsics which return a data type other than integer are not allowed in specification expressions”.

I am having trouble understanding this constraint in the (2018) standard. After reading section 10.1.11, it is clear to me that a specification expression must be a scalar integer expression, with the constraint of it being a restricted expression, but I cannot see where it is mandated that all restricted expressions must be integer expressions.

1 Like

Intel ifort -stand:f18 gives warning:

warning #6009: Fortran 2018 specifies that an elemental intrinsic function here be of type integer or character and each argument must be an initialization expr of type integer or character. [NINT]

gfortran -Wall -std=f2018 keeps silent.

Interestingly, if you change nint to int, no warnings are issued by any of those compilers.

Intel ifort -stand:f18 gives warning:

warning #6009: Fortran 2018 specifies that an elemental intrinsic function here be of type integer or character and each argument must be an initialization expr of type integer or character. [NINT]

That is strange, looks like a reminiscent of fortran - warning "an elemental intrinsic function here be of type integer or character" - Stack Overflow , which seemed to be about the initialization expression, not the type specification.

By the way, the NAG compiler (7.1) is also quiet about this (with -C=all, irrespective of requiring -f2018 or its default 2008 standard).

Looks like a compiler that has needed updating for decades. Its rule for intrinsics in specification expressions was even more restrictive than the f90 requirement where 7.1.6.2 (7) allowed “An elemental intrinsic function reference of type integer or character where each argument is a restricted expression of type integer or character”. But the argument of nint was of type real, so disallowed in f90.

This rule for specification expressions was relaxed in f95, and because nint and real are both elemental functions, and nint returns an integer,
character(len=nint(real(1))) :: c = ‘a’
is a valid declaration according to f95 and later standards.

1 Like