Initialization of constants...

W

Werner

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
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top