Funktion nur einmal aufrufen


  • Administrator

    hustbaer schrieb:

    Das "static" in "static initialization" und das "static" in "static storage duration" sind zwei gänzlich unterschiedliche "static" - eins hat mit dem anderen nichts zu tun.

    Das ist schon klar, nur ...
    Ach, ich warte einfach bis du geantwortet hast. Würde mich freuen, wenn du mich da erhellen könntest. Habe mich erst vor kurzem damit etwas genauer beschäftigt. Dachte eigentlich, ich hätte es nun endlich raus. 😉

    Grüssli



  • Dravere schrieb:

    8.5 Abschnitt 5
    "To zero-initialize an object of type T means:"
    ...
    "— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);"

    Du schummelst. Dein "..." enthält etwas was du nicht weglassen darfst, wenn du die Bedeutung nicht total verdrehen willst.

    To zero-initialize an object of type T means:
    — if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
    — if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
    — if T is a union type, the object’s first named data member89) is zero-initialized;
    — if T is an array type, each element is zero-initialized;
    — if T is a reference type, no initialization is performed.
    To default-initialize an object of type T means: <------------------ *** WICHTIG ***
    — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

    — if T is an array type, each element is default-initialized;
    — otherwise, the object is zero-initialized.

    Steht ganz klar hier: der Aufruf eines ctors ist nicht zero-initialization. (Ich auch logisch) Default-initialization ist ein Begriff, der bezüglich der Reihenfolge bzw. des Zeitpunktes wann initialisiert wird überhaupt keine Bedeutung hat, da Default-initialization eben zero-initialization oder dynamic-initialization sein kann.

    Der Rest steht dann in 3.6.2: Initialization of non-local objects.

    Zusammengefasst (und grob vereinfacht): zuerst wird immer alles (alle non-local statics) "zero-initialized". Dann kommen PODs die direkt mit (compile-time-) konstanten Ausdrücken initialisiert werden.
    (BTW: ein POD kann keinen ctor haben, da ein POD ein aggregate ist, und ein aggregate eben keinen ctor haben kann.)

    Was den Zeitpunkt angeht zu dem Objekte initialisiert werden auf die das nicht zutrifft (keine PODs, oder mit nicht-konstanten Ausdrücken initialisiert)...
    3.6.2 Absatz 3:

    It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of
    namespace scope is done before the first statement of main. If the initialization is deferred to some point
    in time after the first statement of main, it shall occur before the first use of any function or object defined
    in the same translation unit as the object to be initialized.


Anmelden zum Antworten