🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2042

Re: Questions concerning initialization of nonlocals

#2042
From: Ajay Kamdar
Date: Tue, 26 Sep 1995 16:49
54 lines
2300 bytes
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. ]
Technical Details
Message-ID: <MATT.95Sep26094918@physics2.Berkeley.EDU>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <DFHC1v.Kq5@peacock.tcinc.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: ajay@lehman.com (Ajay Kamdar)
Newsgroups: comp.std.c++
Subject: Re: Questions concerning initialization of nonlocals
Date: 26 Sep 1995 16:49:18 GMT
Organization: Lehman Brothers, Inc.
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <MATT.95Sep26094918@physics2.Berkeley.EDU>
References: <DFHC1v.Kq5@peacock.tcinc.com>
NNTP-Posting-Host: physics2.berkeley.edu