Clarification of __attribute__ init_priority

Vladimir Simonov sv@sw.ru
Tue May 17 16:59:00 GMT 2011


Hi Ian,

Could you please clarify __attribute__ init_priority.
According info gcc:
6.7 C++-Specific Variable, Function, and Type Attributes
========================================================

Some attributes only make sense for C++ programs.

`init_priority (PRIORITY)'
      In Standard C++, objects defined at namespace scope are guaranteed
      to be initialized in an order in strict accordance with that of
      their definitions _in a given translation unit_.  No guarantee is
      made for initializations across translation units.  However, GNU
      C++ allows users to control the order of initialization of objects
      defined at namespace scope with the `init_priority' attribute by
      specifying a relative PRIORITY, a constant integral expression
      currently bounded between 101 and 65535 inclusive.  Lower numbers
      indicate a higher priority.

      In the following example, `A' would normally be created before
      `B', but the `init_priority' attribute has reversed that order:

           Some_Class  A  __attribute__ ((init_priority (2000)));
           Some_Class  B  __attribute__ ((init_priority (543)));

      Note that the particular values of PRIORITY do not matter; only
      their relative ordering.

==============================
[~]$ gcc -v
....
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)

Questions:
1. "However, GNU C++ allows users to control the order of initialization of objects
defined at namespace scope with the `init_priority' attribute by specifying a relative 
PRIORITY". Because no "in a given translation unit" here does it mean that
init_priority works over all translation units and has "link unit" scope?
2. If 1 is true what about anonymous namespace? Is it considered the same namespace
for different translation units?
3. "Note that the particular values of PRIORITY do not matter; only their relative 
ordering." IMO it may be correct only if default priority value > 65535.
But it means that init priority can't be set lover(value greater) then default.
Is it correct?

Thank you in advance
Vladimir Simonov



More information about the Gcc-help mailing list