Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Initialization of constants... (http://www.velocityreviews.com/forums/t954036-initialization-of-constants.html)

Werner 10-30-2012 07:58 AM

Initialization of constants...
 
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


Victor Bazarov 10-30-2012 12:34 PM

Re: Initialization of constants...
 
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


All times are GMT. The time now is 05:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.