Topic: Questions concerning initialization of nonlocals


Author: "Wesley E. Munsil" <wmunsil@tcinc.com>
Date: 1995/09/25
Raw View
Please consider the following program schema and questions.

// Declare class A with non-trivial constructor
#include "a.h"

static A a;

int main()
{
   // Nontrivial use of a
   return 0;
}

(1) Will the standard require that a's constructor be called?

(2) If the answer to (1) is "yes", will the standard impose any constraints
    as to the time at or by which the call must occur?

(3) If the answer to (1) is "yes" and the answer to (2) is "no", does not
    this imply that a compiler is free to time the call to occur whenever
    it likes, say, immediately prior to termination of the program?

(4) If the answer to (1) is "yes" and the answer to (2) is "yes", what are
    those constraints?  Please answer in the most general form.

Thank you.


[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]






Author: "Wesley E. Munsil" <wmunsil@tcinc.com>
Date: 1995/09/26
Raw View
ajay@lehman.com (Ajay Kamdar) wrote:

..

>If the static object above were not in the same translation unit as main(),
>then a copule of other possibilities exist. In such a situation,
>theoritically, the ctor could be executed some time before the start of
>main(), OR it could be deferred to any point in time before the first use
>of an object or function in that translation unit.

..

On the contrary, a noted compiler vendor, active on the committee, has told me
that this was rendered inoperative at the last meeting -- in fact, the
constructor call may occur AFTER the use of some object or function in the
translation unit, since enforcing the policy stated in the DWP is "impossible".

Thus, my questions.  Will the standard impose any constraints as to the time at
or by which the call must occur?


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: ajay@lehman.com (Ajay Kamdar)
Date: 1995/09/26
Raw View
In article <DFHC1v.Kq5@peacock.tcinc.com>,
Wesley E. Munsil <wmunsil@tcinc.com> wrote:
>Please consider the following program schema and questions.
>
>// Declare class A with non-trivial constructor
>#include "a.h"
>
>static A a;
>
>int main()
>{
>   // Nontrivial use of a
>   return 0;
>}
>
>(1) Will the standard require that a's constructor be called?

Yes.

>(2) If the answer to (1) is "yes", will the standard impose any constraints
>    as to the time at or by which the call must occur?

Yes.

>(4) If the answer to (1) is "yes" and the answer to (2) is "yes", what are
>    those constraints?  Please answer in the most general form.

In the example above, the constructor for "a" will be called before the
start of main.

If the static object above were not in the same translation unit as main(),
then a copule of other possibilities exist. In such a situation,
theoritically, the ctor could be executed some time before the start of
main(), OR it could be deferred to any point in time before the first use
of an object or function in that translation unit.

Note that the DWP explicitly states that the order in which non-local
objects in different translation units will be initialized is not specified.
In fact, program execution errors due to incorrect order of non-local
static object initialization (i.e., different from what the programmer
thought it would be) are very common.

DWP 3.6.2 "Initialization of non-local objects" has more on this topic.


--
Ajay Kamdar        |    Email: ajay@lehman.com    |    Standard Disclaimer
Lehman Brothers    |    Phone: (201) 524-5048     |

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]