Re: non-const static member variale initialization
"George" <George@discussions.microsoft.com> wrote in message
news:908CAFEE-85A3-4B80-9E65-18392FA4F1B3@microsoft.com
Your reply is great! But I do not quite understand this. I think in
this situation, declaration is not the same as definition
Yes, a declaration of a static data member of a class is not a
definition (if that's what you mean by "this situation"). The definition
must be provided separately. Which is precisely what Giovanni showed you
in his very first response.
There is a special exception for static data members of integral or
enumeration types:
9.4.2/4 If a static data member is of const integral or const
enumeration type, its declaration in the class definition can specify a
constant-initializer which shall be an integral constant expression
(5.19). In that case, the member can appear in integral constant
expressions within its scope. The member shall still be defined in a
namespace scope if it is used in the program and the namespace scope
definition shall not contain an initializer.
The wording is further clarified by resolutions to defect reports #48
and #454:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#48
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#454
In particular, DR454 clarifies the definition of "used" to exclude
situations where the object's name appears as an integral constant
expression (basically, where a literal could appear instead).
How could we declare a static data member variable in initializer
Huh? I think you have badly misread the quote. Let me try and introduce
additional punctuation to make it clearer:
3.1/2 A declaration is a definition except in the following cases:
- it declares a function without specifying the function's body
(8.4);
- it contains the extern specifier (7.1.1) or a
linkage-specification (7.5), and it doesn't contain an initializer or
a function-body;
- it declares a static data member in a class declaration (9.4);
- it is a class name declaration (9.1);
- it is a typedef declaration (7.1.3);
- it is a using-declaration (7.3.3);
- it is a using-directive (7.3.4).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925