From Jason Turner

[class.mfct.non-static]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmppl2s_4hu/{from.md → to.md} +92 -13
tmp/tmppl2s_4hu/{from.md → to.md} RENAMED
@@ -1,8 +1,8 @@
1
- ### Nonstatic member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
2
 
3
- A *non-static* member function may be called for an object of its class
4
  type, or for an object of a class derived (Clause  [[class.derived]])
5
  from its class type, using the class member access syntax (
6
  [[expr.ref]],  [[over.match.call]]). A non-static member function may
7
  also be called directly using the function call syntax ([[expr.call]], 
8
  [[over.match.call]]) from within the body of a member function of its
@@ -13,24 +13,30 @@ that is not of type `X`, or of a type derived from `X`, the behavior is
13
  undefined.
14
 
15
  When an *id-expression* ([[expr.prim]]) that is not part of a class
16
  member access syntax ([[expr.ref]]) and not used to form a pointer to
17
  member ([[expr.unary.op]]) is used in a member of class `X` in a
18
- context where `this` can be used ([[expr.prim.general]]), if name
19
- lookup ([[basic.lookup]]) resolves the name in the *id-expression* to a
20
  non-static non-type member of some class `C`, and if either the
21
  *id-expression* is potentially evaluated or `C` is `X` or a base class
22
  of `X`, the *id-expression* is transformed into a class member access
23
  expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
24
- *postfix-expression* to the left of the `.` operator. If `C` is not `X`
25
- or a base class of `X`, the class member access expression is
26
- ill-formed. Similarly during name lookup, when an *unqualified-id* (
27
- [[expr.prim]]) used in the definition of a member function for class `X`
28
- resolves to a `static` member, an enumerator or a nested type of class
29
- `X` or of a base class of `X`, the *unqualified-id* is transformed into
30
- a *qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
31
- names the class of the member function.
 
 
 
 
 
 
32
 
33
  ``` cpp
34
  struct tnode {
35
  char tword[20];
36
  int count;
@@ -59,29 +65,102 @@ In the body of the member function `tnode::set`, the member names
59
  which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
60
  `tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
61
  refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
62
  not members of the class `tnode` and should be declared elsewhere.[^4]
63
 
 
 
64
  A non-static member function may be declared `const`, `volatile`, or
65
- `const` `volatile`. These *cv-qualifiers* affect the type of the `this`
66
  pointer ([[class.this]]). They also affect the function type (
67
  [[dcl.fct]]) of the member function; a member function declared `const`
68
  is a *const* member function, a member function declared `volatile` is a
69
  *volatile* member function and a member function declared `const`
70
  `volatile` is a *const volatile* member function.
71
 
 
 
72
  ``` cpp
73
  struct X {
74
  void g() const;
75
  void h() const volatile;
76
  };
77
  ```
78
 
79
  `X::g` is a `const` member function and `X::h` is a `const` `volatile`
80
  member function.
81
 
 
 
82
  A non-static member function may be declared with a *ref-qualifier* (
83
  [[dcl.fct]]); see  [[over.match.funcs]].
84
 
85
  A non-static member function may be declared *virtual* (
86
  [[class.virtual]]) or *pure virtual* ([[class.abstract]]).
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
2
 
3
+ A non-static member function may be called for an object of its class
4
  type, or for an object of a class derived (Clause  [[class.derived]])
5
  from its class type, using the class member access syntax (
6
  [[expr.ref]],  [[over.match.call]]). A non-static member function may
7
  also be called directly using the function call syntax ([[expr.call]], 
8
  [[over.match.call]]) from within the body of a member function of its
 
13
  undefined.
14
 
15
  When an *id-expression* ([[expr.prim]]) that is not part of a class
16
  member access syntax ([[expr.ref]]) and not used to form a pointer to
17
  member ([[expr.unary.op]]) is used in a member of class `X` in a
18
+ context where `this` can be used ([[expr.prim.this]]), if name lookup (
19
+ [[basic.lookup]]) resolves the name in the *id-expression* to a
20
  non-static non-type member of some class `C`, and if either the
21
  *id-expression* is potentially evaluated or `C` is `X` or a base class
22
  of `X`, the *id-expression* is transformed into a class member access
23
  expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
24
+ *postfix-expression* to the left of the `.` operator.
25
+
26
+ [*Note 1*: If `C` is not `X` or a base class of `X`, the class member
27
+ access expression is ill-formed. *end note*]
28
+
29
+ Similarly during name lookup, when an *unqualified-id* ([[expr.prim]])
30
+ used in the definition of a member function for class `X` resolves to a
31
+ static member, an enumerator or a nested type of class `X` or of a base
32
+ class of `X`, the *unqualified-id* is transformed into a
33
+ *qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
34
+ names the class of the member function. These transformations do not
35
+ apply in the template definition context ([[temp.dep.type]]).
36
+
37
+ [*Example 1*:
38
 
39
  ``` cpp
40
  struct tnode {
41
  char tword[20];
42
  int count;
 
65
  which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
66
  `tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
67
  refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
68
  not members of the class `tnode` and should be declared elsewhere.[^4]
69
 
70
+ — *end example*]
71
+
72
  A non-static member function may be declared `const`, `volatile`, or
73
+ `const` `volatile`. These *cv-qualifier*s affect the type of the `this`
74
  pointer ([[class.this]]). They also affect the function type (
75
  [[dcl.fct]]) of the member function; a member function declared `const`
76
  is a *const* member function, a member function declared `volatile` is a
77
  *volatile* member function and a member function declared `const`
78
  `volatile` is a *const volatile* member function.
79
 
80
+ [*Example 2*:
81
+
82
  ``` cpp
83
  struct X {
84
  void g() const;
85
  void h() const volatile;
86
  };
87
  ```
88
 
89
  `X::g` is a `const` member function and `X::h` is a `const` `volatile`
90
  member function.
91
 
92
+ — *end example*]
93
+
94
  A non-static member function may be declared with a *ref-qualifier* (
95
  [[dcl.fct]]); see  [[over.match.funcs]].
96
 
97
  A non-static member function may be declared *virtual* (
98
  [[class.virtual]]) or *pure virtual* ([[class.abstract]]).
99
 
100
+ #### The `this` pointer <a id="class.this">[[class.this]]</a>
101
+
102
+ In the body of a non-static ([[class.mfct]]) member function, the
103
+ keyword `this` is a prvalue expression whose value is the address of the
104
+ object for which the function is called. The type of `this` in a member
105
+ function of a class `X` is `X*`. If the member function is declared
106
+ `const`, the type of `this` is `const` `X*`, if the member function is
107
+ declared `volatile`, the type of `this` is `volatile` `X*`, and if the
108
+ member function is declared `const` `volatile`, the type of `this` is
109
+ `const` `volatile` `X*`.
110
+
111
+ [*Note 1*: Thus in a `const` member function, the object for which the
112
+ function is called is accessed through a `const` access
113
+ path. — *end note*]
114
+
115
+ [*Example 1*:
116
+
117
+ ``` cpp
118
+ struct s {
119
+ int a;
120
+ int f() const;
121
+ int g() { return a++; }
122
+ int h() const { return a++; } // error
123
+ };
124
+
125
+ int s::f() const { return a; }
126
+ ```
127
+
128
+ The `a++` in the body of `s::h` is ill-formed because it tries to modify
129
+ (a part of) the object for which `s::h()` is called. This is not allowed
130
+ in a `const` member function because `this` is a pointer to `const`;
131
+ that is, `*this` has `const` type.
132
+
133
+ — *end example*]
134
+
135
+ Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
136
+ member functions when accessing the object and its non-static data
137
+ members.
138
+
139
+ A cv-qualified member function can be called on an object-expression (
140
+ [[expr.ref]]) only if the object-expression is as cv-qualified or
141
+ less-cv-qualified than the member function.
142
+
143
+ [*Example 2*:
144
+
145
+ ``` cpp
146
+ void k(s& x, const s& y) {
147
+ x.f();
148
+ x.g();
149
+ y.f();
150
+ y.g(); // error
151
+ }
152
+ ```
153
+
154
+ The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
155
+ non-`const` member function, that is, `s::g()` is less-qualified than
156
+ the object-expression `y`.
157
+
158
+ — *end example*]
159
+
160
+ Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
161
+ not be declared `const`, `volatile` or `const` `volatile`.
162
+
163
+ [*Note 2*: However, these functions can be invoked to create and
164
+ destroy objects with cv-qualified types, see  [[class.ctor]] and 
165
+ [[class.dtor]]. — *end note*]
166
+