Early dynamic initialization permitted for local statics?

Discussion in 'C++' started by Triple-DES, May 28, 2008.

  1. Triple-DES

    Triple-DES Guest

    I've seen this question raised more than once, but I have not yet seen
    a definite, conclusive answer. Consider the following code:

    struct C {
    C() {} // might throw
    };

    int main() {
    try {
    static C c;
    }
    catch(...) {
    //...
    }
    }

    Now according to 6.7/4:
    "(...) An implementation is permitted to perform early initialization
    of other local objects with static storage duration under the same
    conditions that an implementation is permitted to statically
    initialize an object with static storage duration in namespace scope
    (3.6.2).(...)"

    Are the following statements correct?
    1)
    An implementation may perform early initialization of c, possibly
    resulting in an uncaught exception.

    2)
    Early initialization can be prevented by making C::C() modify an
    arbitrary object of namespace scope with static storage duration, per
    3.6.2/2

    Thanks in advance.
    DP
     
    Triple-DES, May 28, 2008
    #1
    1. Advertising

  2. Triple-DES

    Bo Persson Guest

    Triple-DES wrote:
    > I've seen this question raised more than once, but I have not yet
    > seen a definite, conclusive answer. Consider the following code:
    >
    > struct C {
    > C() {} // might throw
    > };
    >
    > int main() {
    > try {
    > static C c;
    > }
    > catch(...) {
    > //...
    > }
    > }
    >
    > Now according to 6.7/4:
    > "(...) An implementation is permitted to perform early
    > initialization of other local objects with static storage duration
    > under the same conditions that an implementation is permitted to
    > statically initialize an object with static storage duration in
    > namespace scope (3.6.2).(...)"


    This talks about statically initializing with a constant expression.
    As your example involves a constructor, this is more likely a dynamic
    initlialization.

    The static keyword doesn't help. :)

    >
    > Are the following statements correct?
    > 1)
    > An implementation may perform early initialization of c, possibly
    > resulting in an uncaught exception.


    I don't think so.

    >
    > 2)
    > Early initialization can be prevented by making C::C() modify an
    > arbitrary object of namespace scope with static storage duration,
    > per
    > 3.6.2/2


    No.


    The other question is why you need a static variable inside main()?!
    As main can only be called once, there is little risk of initializing
    the local variable more than once.



    Bo Persson
     
    Bo Persson, May 28, 2008
    #2
    1. Advertising

  3. Triple-DES

    Triple-DES Guest

    On 28 Mai, 20:09, "Bo Persson" <> wrote:
    > Triple-DES wrote:
    > > struct C {
    > >  C() {} // might throw
    > > };

    >
    > > int main() {
    > >  try {
    > >    static C c;
    > >  }
    > >  catch(...) {
    > >   //...
    > >  }
    > > }

    >
    > > Now according to 6.7/4:
    > > "(...) An implementation is permitted to perform early
    > > initialization of other local objects with static storage duration
    > > under the same conditions that an implementation is permitted to
    > > statically initialize an object with static storage duration in
    > > namespace scope (3.6.2).(...)"

    >
    > This talks about statically initializing with a constant expression.
    > As your example involves a constructor, this is more likely a dynamic
    > initlialization.
    >


    Sure, I have considered that interpretation. But see for instance this
    thread:
    http://groups.google.com/group/comp...e_frm/thread/c9e3870d8b58a39/7b9a16407c86419a

    Notably, the comment by Francis W. Glassborow:
    (...)And I would much prefer that it was not allowed to do dynamic
    initialisation early, not least because of implications on exception
    safety.(...)

    > The static keyword doesn't help.  :)

    [snip]
    > The other question is why you need a static variable inside main()?!
    > As main can only be called once, there is little risk of initializing
    > the local variable more than once.


    That's just to illustrate the problem.

    DP
     
    Triple-DES, May 29, 2008
    #3
  4. > Triple-DES wrote:
    >> I've seen this question raised more than once, but I have not yet
    >> seen a definite, conclusive answer. Consider the following code:
    >>
    >> struct C {
    >> C() {} // might throw
    >> };
    >>
    >> int main() {
    >> try {
    >> static C c;
    >> }
    >> catch(...) {
    >> //...
    >> }
    >> }
    >>


    Interestingly, if:

    void f() {
    static C aC;
    /* ... */
    }

    If the ctor throws an exception, when f called the second time, C ctor
    is called again. At least in my implementation.

    Is this behavior specified in the standard and can I rely on it ?
     
    Irin Kotchencko, May 29, 2008
    #4
  5. Triple-DES

    Triple-DES Guest

    On 29 Mai, 07:38, Irin Kotchencko <> wrote:
    > Interestingly, if:
    >
    > void f() {
    >     static C aC;
    >     /* ... */
    >
    > }
    >
    > If the ctor throws an exception, when f called the second time, C ctor
    > is called again. At least in my implementation.
    >
    > Is this behavior specified in the standard and can I rely on it ?


    Yes, this is also specified in 6.7/4:
    "(...)If the initialization exits by throwing an exception, the
    initialization is not complete, so it will be tried again next time
    control enters the declaration(...)"

    DP
     
    Triple-DES, May 29, 2008
    #5
  6. > On 29 Mai, 07:38, Irin Kotchencko <> wrote:
    >> Interestingly, if:
    >>
    >> void f() {
    >> static C aC;
    >> /* ... */
    >>
    >> }
    >>
    >> If the ctor throws an exception, when f called the second time, C ctor
    >> is called again. At least in my implementation.
    >>
    >> Is this behavior specified in the standard and can I rely on it ?

    >
    > Yes, this is also specified in 6.7/4:
    > "(...)If the initialization exits by throwing an exception, the
    > initialization is not complete, so it will be tried again next time
    > control enters the declaration(...)"
    >
    > DP

    thank you
     
    Irin Kotchencko, May 29, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee
    Replies:
    3
    Views:
    312
    John C. Bollinger
    Feb 5, 2004
  2. Charles L
    Replies:
    5
    Views:
    1,218
    Ron Natalie
    Feb 8, 2005
  3. Chris McDonald

    bit-fields and permitted types (and [OT] gcc)

    Chris McDonald, Apr 18, 2005, in forum: C Programming
    Replies:
    1
    Views:
    609
    Eric Sosman
    Apr 18, 2005
  4. Stuart MacMartin
    Replies:
    5
    Views:
    565
    Victor Bazarov
    Jul 27, 2005
  5. Marcel Müller

    Initialization of local statics

    Marcel Müller, Sep 26, 2010, in forum: C++
    Replies:
    5
    Views:
    354
    Marcel Müller
    Sep 27, 2010
Loading...

Share This Page