c++ - How to initialize a bitfield inside structure declaration? -


msvc++ gives me compiler error when try initialize bit field inside structure declaration:

struct somestruct {     bool : 1 = false; // compiler error     bool = false : 1; // compiler error } ; 

what syntax initializing bit fields inside structure declaration?

i bit surprised apparently visual studio correct here, if @ grammar section 9.2 of draft c++11 standard says:

member-declarator:   declarator virt-specifier-seqopt pure-specifieropt   declarator brace-or-equal-initializeropt   identifieropt attribute-specifier-seqopt: constant-expression 

and bit-fields not allowed have brace-or-equal-initializer. not clear me why restriction exists though. feels first time realized in-class initializer makes class non-aggregate.

this apparently defect:

the grammar member-declarator (9.2 [class.mem]) not, should, allow brace-or-equal-initializer on bit-field declarator.

this issue apparently caught before c++11 finalized can see issues found implementing c++0x:

  1. (richard smith) class.mem: bitfield members cannot have in-class initializers

the grammar not allow brace-or-equal-initializer bitfield member. seems oversight. brace-or-equal-initializer after constant- expression appears unambiguous.

clang behavior: clang implements letter of standard.

suggested resolution: change grammar follows:

member-declarator:     identifieropt attribute-specifier-seqopt : constant-expression brace-or-equal-initializeropt 

but apparently feel through cracks.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -