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:

    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)

    The loosening, for some people, of rigid role definitions for men and women has shown that dads can be great at calming babies—if they take the time and make the effort to learn how. It’s that time and effort that not only teaches the dad how to calm the babies, but also turns him into a parent, just as the time and effort the mother puts into the babies turns her into a parent.
    Pamela Patrick Novotny (20th century)

    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)

    I esteem it the happiness of this country that its settlers, whilst they were exploring their granted and natural rights and determining the power of the magistrate, were united by personal affection. Members of a church before whose searching covenant all rank was abolished, they stood in awe of each other, as religious men.
    Ralph Waldo Emerson (1803–1882)