Definitions of Static Const Data Members
In pre-standard C++, all static data members required a definition outside of their class. However, during the C++ standardization process it was decided to lift this requirement for static const integral members. The intent was to allow uses such as:
struct C { static const int N = 10; }; char data; // N "used" without out-of-class definitionwithout a namespace scope definition for N
.
Nevertheless, the wording of the 1998 C++ standard still required a definition if the member was used in the program. This included the member appearing anywhere except as the operand to sizeof or typeid, effectively making the above ill-formed.
This was identified as a defect, and the wording was adjusted to allow such a member to appear anywhere a constant expression is required, without requiring an out-of-class definition. This includes array bounds, case expressions, static member initializers, and nontype template arguments.
struct C { static const int N = 10; static const int U = N; // Legal per C++03 }; char data; // Legal per C++03 templateHowever, using a static const integral member anywhere except where an integral constant-expression is required requires a definition:
struct C { static const int N = 10; }; int main { int i = C::N; // ill-formed, definition of C::N required }This requirement has been relaxed in the current C++ standard, C++11.
Read more about this topic: One Definition Rule
Famous quotes containing the words definitions of, definitions, data and/or members:
“What I do not like about our definitions of genius is that there is in them nothing of the day of judgment, nothing of resounding through eternity and nothing of the footsteps of the Almighty.”
—G.C. (Georg Christoph)
“What I do not like about our definitions of genius is that there is in them nothing of the day of judgment, nothing of resounding through eternity and nothing of the footsteps of the Almighty.”
—G.C. (Georg Christoph)
“To write it, it took three months; to conceive it three minutes; to collect the data in itall my life.”
—F. Scott Fitzgerald (18961940)
“The English people believes itself to be free; it is gravely mistaken; it is free only during election of members of parliament; as soon as the members are elected, the people is enslaved; it is nothing. In the brief moment of its freedom, the English people makes such a use of that freedom that it deserves to lose it.”
—Jean-Jacques Rousseau (17121778)