Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Constant Expressions

A constant expression is an expression that you can use as a kind type parameter, a named constant, or to specify an initial value for an entity. It is evaluated when a program is compiled.

In a constant expression, each operation is intrinsic and each primary is one of the following:

  • A constant or subobject of a constant

  • A specification inquiry where each designator or function argument is one of the following:

    • A constant expression

    • A variable whose properties inquired about are not assumed, deferred, or defined by an expression that is not a constant expression

  • A reference to the transformational function IEEE_SELECTED_REAL_KIND from the intrinsic module IEEE_ARITHMETIC, where each argument is a constant expression

  • A previously declared kind type parameter of the derived type being defined

  • A DO variable within an array constructor where each scalar integer expression of the corresponding DO loop is a constant expression

  • An array constructor where each element and each scalar integer expression of each ac-implied-do-control is a constant expression

  • An array constructor where each component-spec corresponding to an allocatable component or is a reference to the intrinsic function NULL, each pointer component is an initialization target or a reference to the intrinsic function NULL, and every other component-spec is a constant expression

  • A reference to a standard elemental intrinsic function where each argument is a constant expression

  • A reference to the intrinsic function TRANSFER where each argument is a constan expression and each ultimate pointer component of the SOURCE argument is a disassociated

  • A reference to a standard transitional intrinsic function other than COMMAND_ARGUMENT_COUNT, GET_TEAM, NULL, NUM_IMAGES, TEAM_NUMBER, THIS_IMAGE, or TRANSFER and each argument is a constant expression

  • A reference to the intrinsic function NULL does not have an argument with a type parameter that is assumed or is defined by a non-constant expression

  • A data-i-do-variable within a data-implied-do

  • Another constant expression enclosed in parentheses

Note that for all the above items, each subscript, section subscript, substring starting and ending point, and type parameter value is a constant expression.

If a constant expression invokes an inquiry function for a type parameter or an array bound of an object, the type parameter or array bound must be specified in a prior specification statement (or to the left of the inquiry function in the same statement). The previous specification cannot be in the same entity declaration unless the specification appears in an initialization.

If a reference to a generic entity is included in a constant expression that is in the specification part of a module or submodule, that generic entity shall have no specific procedures defined subsequent to the constant expression in the module or submodule.

Examples

Valid constant Expressions

-1 + 3

SIZE(B)

! B is a named constant.

7_2

INT(J, 4)

! J is a named constant.

SELECTED_INT_KIND (2)

SUM (A)

! This is permitted if A is a PARAMETER array previously declared.

! Otherwise, it is not permitted.

ATAND (45.0)

! This is a standard element intrinsic function with a constant argument.

Invalid constant Expressions

MY_SUM(A)

This is a user-defined function.

A/4.1 - K**1.2

The exponential does not have integer power (A and K are named constants).