priority

C++ compiler only

priority syntax

Read syntax diagramSkip visual syntax diagram#pragmapriority(n)

Description

The #pragma priority directive specifies the order in which static objects are to be initialized at runtime.

The value n is an integer literal in the range of INT_MIN to INT_MAX. The default value is 0. A negative value indicates a higher priority; a positive value indicates a lower priority.

The first 1024 priorities (INT_MIN to INT_MIN + 1023) are reserved for use by the compiler and its libraries. The #pragma priority can appear anywhere in the source file many times. However, the priority of each pragma must be greater than the previous pragma's priority. This is necessary to ensure that the runtime static initialization occurs in the declaration order.

Example

//File one called First.C

#pragma priority (1000)
class A { public: int a; A() {return;} } a;
#pragma priority (3000)
class C { public: int c; C() {return;} } c;
class B { public: int b; B() {return;} };
extern B b;
main()
{
    a.a=0;
    b.b=0;
    c.c=0;
}

//File two called Second.C
#pragma priority (2000)
class B { public: int b; B() {return;} } b;
In this example, the execution sequence of the runtime static initialization is:
  1. Static initialization with priority 1000 from file First.C
  2. Static initialization with priority 2000 from file Second.C
  3. Static initialization with priority 3000 from file First.C