Стандарт C++ 98 (1119566), страница 14
Текст из файла (страница 14)
A violation of this rule on type identity does not require a diagnostic.11[Note: linkage to non-C++ declarations can be achieved using a linkage-specification (7.5). ]3.6 Start and termination3.6.1 Main function[basic.start][basic.start.main]1A program shall contain a global function called main, which is the designated start of the program. It isimplementation-defined whether a program in a freestanding environment is required to define a mainfunction. [Note: in a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]2An implementation shall not predefine the main function. This function shall not be overloaded.
It shallhave a return type of type int, but otherwise its type is implementation-defined. All implementationsshall allow both of the following definitions of main:int main() { /* ... */ }andint main(int argc, char* argv[]) { /* ... */ }In the latter form argc shall be the number of arguments passed to the program from the environment inwhich the program is run. If argc is nonzero these arguments shall be supplied in argv[0] throughargv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs)(17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents thename used to invoke the program or "". The value of argc shall be nonnegative. The value ofargv[argc] shall be 0.
[Note: it is recommended that any further (optional) parameters be added afterargv. ]3The function main shall not be used (3.2) within a program. The linkage (3.5) of main isimplementation-defined. A program that declares main to be inline or static is ill-formed. Thename main is not otherwise reserved. [Example: member functions, classes, and enumerations can becalled main, as can entities in other namespaces. ]4Calling the functionvoid exit(int);declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.5A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument.
If control reaches the endof main without encountering a return statement, the effect is that of executingreturn 0;43ISO/IEC 14882:1998(E)3.6.2 Initialization of non-local objects3.6.2 Initialization of non-local objects© ISO/IEC3 Basic concepts[basic.start.init]1The storage for objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any otherinitialization takes place.
Zero-initialization and initialization with a constant expression are collectivelycalled static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) withstatic storage duration initialized with constant expressions (5.19) shall be initialized before any dynamicinitialization takes place. Objects with static storage duration defined in namespace scope in the sametranslation unit and dynamically initialized shall be initialized in the order in which their definition appearsin the translation unit. [Note: 8.5.1 describes the order in which aggregate members are initialized.
Theinitialization of local static objects is described in 6.7. ]2An implementation is permitted to perform the initialization of an object of namespace scope with staticstorage duration as a static initialization even if such initialization is not required to be done statically, provided that— the dynamic version of the initialization does not change the value of any other object of namespacescope with static storage duration prior to its initialization, and— the static version of the initialization produces the same value in the initialized object as would be produced by the dynamic initialization if all objects not required to be initialized statically were initializeddynamically.[Note: as a consequence, if the initialization of an object obj1 refers to an object obj2 of namespacescope with static storage duration potentially requiring dynamic initialization and defined later in the sametranslation unit, it is unspecified whether the value of obj2 used will be the value of the fully initializedobj2 (because obj2 was statically initialized) or will be the value of obj2 merely zero-initialized.
Forexample,inline double fd() { return 1.0; }extern double d1;double d2 = d1;// unspecified:// may be statically initialized to 0.0 or// dynamically initialized to 1.0double d1 = fd();// may be initialized statically to 1.0—end note]3It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object ofnamespace scope is done before the first statement of main. If the initialization is deferred to some pointin time after the first statement of main, it shall occur before the first use of any function or object definedin the same translation unit as the object to be initialized.31) [Example:// – File 1 –#include "a.h"#include "b.h"B b;A::A(){b.Use();}// – File 2 –#include "a.h"A a;__________________31) An object defined in namespace scope having initialization with side-effects must be initialized even if it is not used (3.7.1).44© ISO/IEC3 Basic conceptsISO/IEC 14882:1998(E)3.6.2 Initialization of non-local objects// – File 3 –#include "a.h"#include "b.h"extern A a;extern B b;int main() {a.Use();b.Use();}It is implementation-defined whether either a or b is initialized before main is entered or whether theinitializations are delayed until a is first used in main.
In particular, if a is initialized before main isentered, it is not guaranteed that b will be initialized before it is used by the initialization of a, that is,before A::A is called. If, however, a is initialized at some point after the first statement of main, b willbe initialized prior to its use in A::A. ]4If construction or destruction of a non-local static object ends in throwing an uncaught exception, the resultis to call terminate (18.6.3.3).3.6.3 Termination[basic.start.term]1Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespacescope) are called as a result of returning from main and as a result of calling exit (18.3).
These objectsare destroyed in the reverse order of the completion of their constructor or of the completion of theirdynamic initialization. If an object is initialized statically, the object is destroyed in the same order as if theobject was dynamically initialized. For an object of array or class type, all subobjects of that object aredestroyed before any local object with static storage duration initialized during the construction of the subobjects is destroyed.2If a function contains a local object of static storage duration that has been destroyed and the function iscalled during the destruction of an object with static storage duration, the program has undefined behaviorif the flow of control passes through the definition of the previously destroyed local object.3If a function is registered with atexit (see <cstdlib>, 18.3) then following the call to exit, anyobjects with static storage duration initialized prior to the registration of that function shall not be destroyeduntil the registered function is called from the termination process and has completed.
For an object withstatic storage duration constructed after a function is registered with atexit, then following the call toexit, the registered function is not called until the execution of the object’s destructor has completed. Ifatexit is called during the construction of an object, the complete object to which it belongs shall bedestroyed before the registered function is called.4Calling the functionvoid abort();declared in <cstdlib> terminates the program without executing destructors for objects of automatic orstatic storage duration and without calling the functions passed to atexit().3.7 Storage duration1[basic.stc]Storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object.
The storage duration is determined by the construct used to create the object and is oneof the following:— static storage duration— automatic storage duration— dynamic storage duration45ISO/IEC 14882:1998(E)3.7 Storage duration© ISO/IEC3 Basic concepts2Static and automatic storage durations are associated with objects introduced by declarations (3.1) andimplicitly created by the implementation (12.2).
The dynamic storage duration is associated with objectscreated with operator new (5.3.4).3The storage class specifiers static and auto are related to storage duration as described below.4The storage duration categories apply to references as well. The lifetime of a reference is its storage duration.3.7.1 Static storage duration[basic.stc.static]1All objects which neither have dynamic storage duration nor are local have static storage duration. Thestorage for these objects shall last for the duration of the program (3.6.2, 3.6.3).2If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in12.8.3The keyword static can be used to declare a local variable with static storage duration.
[Note: 6.7describes the initialization of local static variables; 3.6.3 describes the destruction of local staticvariables. ]4The keyword static applied to a class data member in a class definition gives the data member staticstorage duration.3.7.2 Automatic storage duration[basic.stc.auto]1Local objects explicitly declared auto or register or not explicitly declared static or extern haveautomatic storage duration.