From Jason Turner

[class.static]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpkfuwlty4/{from.md → to.md} +100 -74
tmp/tmpkfuwlty4/{from.md → to.md} RENAMED
@@ -1,16 +1,15 @@
1
- ## Static members <a id="class.static">[[class.static]]</a>
2
 
3
- A data or function member of a class may be declared `static` in a class
4
- definition, in which case it is a *static member* of the class.
5
-
6
- A `static` member `s` of class `X` may be referred to using the
7
  *qualified-id* expression `X::s`; it is not necessary to use the class
8
- member access syntax ([[expr.ref]]) to refer to a `static` member. A
9
- `static` member may be referred to using the class member access syntax,
10
  in which case the object expression is evaluated.
11
 
 
 
12
  ``` cpp
13
  struct process {
14
  static void reschedule();
15
  };
16
  process& g();
@@ -19,17 +18,21 @@ void f() {
19
  process::reschedule(); // OK: no object necessary
20
  g().reschedule(); // g() is called
21
  }
22
  ```
23
 
24
- A `static` member may be referred to directly in the scope of its class
25
- or in the scope of a class derived (Clause  [[class.derived]]) from its
26
- class; in this case, the `static` member is referred to as if a
 
 
27
  *qualified-id* expression was used, with the *nested-name-specifier* of
28
  the *qualified-id* naming the class scope from which the static member
29
  is referenced.
30
 
 
 
31
  ``` cpp
32
  int g();
33
  struct X {
34
  static int g();
35
  };
@@ -37,55 +40,63 @@ struct Y : X {
37
  static int i;
38
  };
39
  int Y::i = g(); // equivalent to Y::g();
40
  ```
41
 
 
 
42
  If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
43
- `static` member following the member’s *declarator-id*, and name
44
- lookup ([[basic.lookup.unqual]]) finds that the *unqualified-id* refers
45
- to a `static` member, enumerator, or nested type of the member’s class
46
- (or of a base class of the member’s class), the *unqualified-id* is
47
- transformed into a *qualified-id* expression in which the
48
- *nested-name-specifier* names the class scope from which the member is
49
- referenced. See  [[expr.prim]] for restrictions on the use of non-static
50
- data members and non-static member functions.
 
51
 
52
  Static members obey the usual class member access rules (Clause 
53
  [[class.access]]). When used in the declaration of a class member, the
54
  `static` specifier shall only be used in the member declarations that
55
- appear within the *member-specification* of the class definition. It
56
- cannot be specified in member declarations that appear in namespace
57
- scope.
58
 
59
- ### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
 
60
 
61
- The rules described in  [[class.mfct]] apply to `static` member
62
- functions.
63
 
64
- A `static` member function does not have a `this` pointer (
65
- [[class.this]]). A `static` member function shall not be `virtual`.
66
- There shall not be a `static` and a non-static member function with the
67
- same name and the same parameter types ([[over.load]]). A `static`
68
- member function shall not be declared `const`, `volatile`, or
69
- `const volatile`.
70
 
71
- ### Static data members <a id="class.static.data">[[class.static.data]]</a>
 
72
 
73
- A `static` data member is not part of the subobjects of a class. If a
74
- `static` data member is declared `thread_local` there is one copy of the
75
- member per thread. If a `static` data member is not declared
 
 
 
 
 
 
 
76
  `thread_local` there is one copy of the data member that is shared by
77
  all the objects of the class.
78
 
79
- The declaration of a `static` data member in its class definition is not
80
- a definition and may be of an incomplete type other than cv-qualified
81
- `void`. The definition for a `static` data member shall appear in a
82
- namespace scope enclosing the member’s class definition. In the
83
- definition at namespace scope, the name of the `static` data member
84
- shall be qualified by its class name using the `::` operator. The
85
- *initializer* expression in the definition of a `static` data member is
86
- in the scope of its class ([[basic.scope.class]]).
 
 
 
87
 
88
  ``` cpp
89
  class process {
90
  static process* run_chain;
91
  static process* running;
@@ -93,44 +104,59 @@ class process {
93
 
94
  process* process::running = get_main();
95
  process* process::run_chain = running;
96
  ```
97
 
98
- The `static` data member `run_chain` of class `process` is defined in
99
  global scope; the notation `process::run_chain` specifies that the
100
  member `run_chain` is a member of class `process` and in the scope of
101
- class `process`. In the `static` data member definition, the
102
- *initializer* expression refers to the `static` data member `running` of
103
- class `process`.
104
-
105
- Once the `static` data member has been defined, it exists even if no
106
- objects of its class have been created. in the example above,
107
- `run_chain` and `running` exist even if no objects of class `process`
108
- are created by the program.
109
-
110
- If a non-volatile `const` `static` data member is of integral or
111
- enumeration type, its declaration in the class definition can specify a
112
- *brace-or-equal-initializer* in which every *initializer-clause* that is
113
- an *assignment-expression* is a constant expression ([[expr.const]]). A
114
- `static` data member of literal type can be declared in the class
115
- definition with the `constexpr` specifier; if so, its declaration shall
116
- specify a *brace-or-equal-initializer* in which every
117
- *initializer-clause* that is an *assignment-expression* is a constant
118
- expression. In both these cases, the member may appear in constant
119
- expressions. The member shall still be defined in a namespace scope if
120
- it is odr-used ([[basic.def.odr]]) in the program and the namespace
121
- scope definition shall not contain an *initializer*.
122
-
123
- There shall be exactly one definition of a `static` data member that is
124
- odr-used ([[basic.def.odr]]) in a program; no diagnostic is required.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  Unnamed classes and classes contained directly or indirectly within
126
- unnamed classes shall not contain `static` data members.
127
 
128
- `Static` data members of a class in namespace scope have external
129
- linkage ([[basic.link]]). A local class shall not have `static` data
130
- members.
131
 
132
- `Static` data members are initialized and destroyed exactly like
133
- non-local variables ([[basic.start.init]],  [[basic.start.term]]).
 
134
 
135
- A `static` data member shall not be `mutable` ([[dcl.stc]]).
136
 
 
1
+ ### Static members <a id="class.static">[[class.static]]</a>
2
 
3
+ A static member `s` of class `X` may be referred to using the
 
 
 
4
  *qualified-id* expression `X::s`; it is not necessary to use the class
5
+ member access syntax ([[expr.ref]]) to refer to a static member. A
6
+ static member may be referred to using the class member access syntax,
7
  in which case the object expression is evaluated.
8
 
9
+ [*Example 1*:
10
+
11
  ``` cpp
12
  struct process {
13
  static void reschedule();
14
  };
15
  process& g();
 
18
  process::reschedule(); // OK: no object necessary
19
  g().reschedule(); // g() is called
20
  }
21
  ```
22
 
23
+ *end example*]
24
+
25
+ A static member may be referred to directly in the scope of its class or
26
+ in the scope of a class derived (Clause  [[class.derived]]) from its
27
+ class; in this case, the static member is referred to as if a
28
  *qualified-id* expression was used, with the *nested-name-specifier* of
29
  the *qualified-id* naming the class scope from which the static member
30
  is referenced.
31
 
32
+ [*Example 2*:
33
+
34
  ``` cpp
35
  int g();
36
  struct X {
37
  static int g();
38
  };
 
40
  static int i;
41
  };
42
  int Y::i = g(); // equivalent to Y::g();
43
  ```
44
 
45
+ — *end example*]
46
+
47
  If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
48
+ static member following the member’s *declarator-id*, and name lookup (
49
+ [[basic.lookup.unqual]]) finds that the *unqualified-id* refers to a
50
+ static member, enumerator, or nested type of the member’s class (or of a
51
+ base class of the member’s class), the *unqualified-id* is transformed
52
+ into a *qualified-id* expression in which the *nested-name-specifier*
53
+ names the class scope from which the member is referenced.
54
+
55
+ [*Note 1*: See  [[expr.prim]] for restrictions on the use of non-static
56
+ data members and non-static member functions. — *end note*]
57
 
58
  Static members obey the usual class member access rules (Clause 
59
  [[class.access]]). When used in the declaration of a class member, the
60
  `static` specifier shall only be used in the member declarations that
61
+ appear within the *member-specification* of the class definition.
 
 
62
 
63
+ [*Note 2*: It cannot be specified in member declarations that appear in
64
+ namespace scope. — *end note*]
65
 
66
+ #### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
 
67
 
68
+ [*Note 1*: The rules described in  [[class.mfct]] apply to static
69
+ member functions. *end note*]
 
 
 
 
70
 
71
+ [*Note 2*: A static member function does not have a `this` pointer (
72
+ [[class.this]]). — *end note*]
73
 
74
+ A static member function shall not be `virtual`. There shall not be a
75
+ static and a non-static member function with the same name and the same
76
+ parameter types ([[over.load]]). A static member function shall not be
77
+ declared `const`, `volatile`, or `const volatile`.
78
+
79
+ #### Static data members <a id="class.static.data">[[class.static.data]]</a>
80
+
81
+ A static data member is not part of the subobjects of a class. If a
82
+ static data member is declared `thread_local` there is one copy of the
83
+ member per thread. If a static data member is not declared
84
  `thread_local` there is one copy of the data member that is shared by
85
  all the objects of the class.
86
 
87
+ The declaration of a non-inline static data member in its class
88
+ definition is not a definition and may be of an incomplete type other
89
+ than cv `void`. The definition for a static data member that is not
90
+ defined inline in the class definition shall appear in a namespace scope
91
+ enclosing the member’s class definition. In the definition at namespace
92
+ scope, the name of the static data member shall be qualified by its
93
+ class name using the `::` operator. The *initializer* expression in the
94
+ definition of a static data member is in the scope of its class (
95
+ [[basic.scope.class]]).
96
+
97
+ [*Example 1*:
98
 
99
  ``` cpp
100
  class process {
101
  static process* run_chain;
102
  static process* running;
 
104
 
105
  process* process::running = get_main();
106
  process* process::run_chain = running;
107
  ```
108
 
109
+ The static data member `run_chain` of class `process` is defined in
110
  global scope; the notation `process::run_chain` specifies that the
111
  member `run_chain` is a member of class `process` and in the scope of
112
+ class `process`. In the static data member definition, the *initializer*
113
+ expression refers to the static data member `running` of class
114
+ `process`.
115
+
116
+ *end example*]
117
+
118
+ [*Note 1*:
119
+
120
+ Once the static data member has been defined, it exists even if no
121
+ objects of its class have been created.
122
+
123
+ [*Example 2*:
124
+
125
+ In the example above, `run_chain` and `running` exist even if no objects
126
+ of class `process` are created by the program.
127
+
128
+ *end example*]
129
+
130
+ *end note*]
131
+
132
+ If a non-volatile non-inline `const` static data member is of integral
133
+ or enumeration type, its declaration in the class definition can specify
134
+ a *brace-or-equal-initializer* in which every *initializer-clause* that
135
+ is an *assignment-expression* is a constant expression (
136
+ [[expr.const]]). The member shall still be defined in a namespace scope
137
+ if it is odr-used ([[basic.def.odr]]) in the program and the namespace
138
+ scope definition shall not contain an *initializer*. An inline static
139
+ data member may be defined in the class definition and may specify a
140
+ *brace-or-equal-initializer*. If the member is declared with the
141
+ `constexpr` specifier, it may be redeclared in namespace scope with no
142
+ initializer (this usage is deprecated; see [[depr.static_constexpr]]).
143
+ Declarations of other static data members shall not specify a
144
+ *brace-or-equal-initializer*.
145
+
146
+ [*Note 2*: There shall be exactly one definition of a static data
147
+ member that is odr-used ([[basic.def.odr]]) in a program; no diagnostic
148
+ is required. — *end note*]
149
+
150
  Unnamed classes and classes contained directly or indirectly within
151
+ unnamed classes shall not contain static data members.
152
 
153
+ [*Note 3*: Static data members of a class in namespace scope have the
154
+ linkage of that class ([[basic.link]]). A local class cannot have
155
+ static data members ([[class.local]]). — *end note*]
156
 
157
+ Static data members are initialized and destroyed exactly like non-local
158
+ variables ([[basic.start.static]], [[basic.start.dynamic]],
159
+ [[basic.start.term]]).
160
 
161
+ A static data member shall not be `mutable` ([[dcl.stc]]).
162