One Definition Rule - Definitions of Static Const Data Members

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 definition

without 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 template struct D; template<> struct D {}; // Legal per C++03

However, 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:

    Lord Byron is an exceedingly interesting person, and as such is it not to be regretted that he is a slave to the vilest and most vulgar prejudices, and as mad as the winds?
    There have been many definitions of beauty in art. What is it? Beauty is what the untrained eyes consider abominable.
    Edmond De Goncourt (1822–1896)

    Lord Byron is an exceedingly interesting person, and as such is it not to be regretted that he is a slave to the vilest and most vulgar prejudices, and as mad as the winds?
    There have been many definitions of beauty in art. What is it? Beauty is what the untrained eyes consider abominable.
    Edmond De Goncourt (1822–1896)

    To write it, it took three months; to conceive it three minutes; to collect the data in it—all my life.
    F. Scott Fitzgerald (1896–1940)

    Every diminution of the public burdens arising from taxation gives to individual enterprise increased power and furnishes to all the members of our happy confederacy new motives for patriotic affection and support.
    Andrew Jackson (1767–1845)