Initialization of constants...

Discussion in 'C++' started by Werner, Oct 30, 2012.

  1. Werner

    Werner Guest

    Hi all,

    The following code snippet does not compile under GCC > 4.6:

    namespace { //anonymous
    const bool error_notImplemented;
    }//end namespace anonymous

    Removing the const causes it to compile. I'm surprised by
    this behaviour. I would have thought that zero-initialization
    would kick in here. I can't make up my mind about what the
    standard says about this case. Any hints?

    {PS. I realize const bool error_NotImplemented( false ) would be
    sufficient and more verbose, but is it required by standard?}

    Kind regards,

    Werner
    Werner, Oct 30, 2012
    #1
    1. Advertising

  2. On 10/30/2012 3:58 AM, Werner wrote:
    > The following code snippet does not compile under GCC > 4.6:
    >
    > namespace { //anonymous
    > const bool error_notImplemented;
    > }//end namespace anonymous
    >
    > Removing the const causes it to compile. I'm surprised by
    > this behaviour. I would have thought that zero-initialization
    > would kick in here. I can't make up my mind about what the
    > standard says about this case. Any hints?


    Since it's a namespace object, it has static storage duration, and it's
    supposed to be zero-initialized (i.e. to 'false') before the program
    starts. Hence there should be no need to explicitly initialize it (see
    [basic.start.init]/2). _HOWEVER_ there is another requirement (see
    [dcl.type.cv]/1) which says that "If a cv-qualifier appears in a
    decl-specifier-seq, the initdeclarator-list of the declaration shall not
    be empty." It means that a declaration of an object with 'const' *must*
    have an initializer.

    > {PS. I realize const bool error_NotImplemented( false ) would be
    > sufficient and more verbose, but is it required by standard?}


    Yes AFAICS.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 30, 2012
    #2
    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. JKop
    Replies:
    10
    Views:
    909
  2. Bret Pehrson
    Replies:
    4
    Views:
    495
    Bret Pehrson
    Dec 9, 2004
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    413
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    443
    Ron Natalie
    Dec 11, 2005
  5. Steve Folly
    Replies:
    3
    Views:
    449
    James Kanze
    Apr 20, 2007
Loading...

Share This Page