tmp/tmp4m0s3z_r/{from.md → to.md}
RENAMED
|
@@ -12,48 +12,47 @@ cv-qualifiers affect object and function types. — *end note*]
|
|
| 12 |
Redundant cv-qualifications are ignored.
|
| 13 |
|
| 14 |
[*Note 2*: For example, these could be introduced by
|
| 15 |
typedefs. — *end note*]
|
| 16 |
|
| 17 |
-
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 18 |
-
[[dcl.stc]]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 24 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 25 |
const-qualified access path cannot be used to modify an object even if
|
| 26 |
the object referenced is a non-const object and can be modified through
|
| 27 |
some other access path.
|
| 28 |
|
| 29 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 30 |
-
cannot be subverted without casting
|
| 31 |
-
[[expr.const.cast]]). — *end note*]
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
[[basic.life]]
|
| 36 |
|
| 37 |
[*Example 1*:
|
| 38 |
|
| 39 |
``` cpp
|
| 40 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 41 |
-
ci = 4; //
|
| 42 |
|
| 43 |
int i = 2; // not cv-qualified
|
| 44 |
const int* cip; // pointer to const int
|
| 45 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 46 |
-
*cip = 4; //
|
| 47 |
|
| 48 |
int* ip;
|
| 49 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 50 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 51 |
|
| 52 |
const int* ciq = new const int (3); // initialized as required
|
| 53 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 54 |
-
*iq = 4; // undefined: modifies a const object
|
| 55 |
```
|
| 56 |
|
| 57 |
For another example,
|
| 58 |
|
| 59 |
``` cpp
|
|
@@ -66,14 +65,14 @@ struct Y {
|
|
| 66 |
Y();
|
| 67 |
};
|
| 68 |
|
| 69 |
const Y y;
|
| 70 |
y.x.i++; // well-formed: mutable member can be modified
|
| 71 |
-
y.x.j++; //
|
| 72 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 73 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 74 |
-
p->x.j = 99; // undefined: modifies a const
|
| 75 |
```
|
| 76 |
|
| 77 |
— *end example*]
|
| 78 |
|
| 79 |
The semantics of an access through a volatile glvalue are
|
|
|
|
| 12 |
Redundant cv-qualifications are ignored.
|
| 13 |
|
| 14 |
[*Note 2*: For example, these could be introduced by
|
| 15 |
typedefs. — *end note*]
|
| 16 |
|
| 17 |
+
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 18 |
+
[[dcl.stc]] and its usability in constant expressions [[expr.const]]. As
|
| 19 |
+
described in [[dcl.init]], the definition of an object or subobject of
|
| 20 |
+
const-qualified type must specify an initializer or be subject to
|
| 21 |
+
default-initialization. — *end note*]
|
| 22 |
|
| 23 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 24 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 25 |
const-qualified access path cannot be used to modify an object even if
|
| 26 |
the object referenced is a non-const object and can be modified through
|
| 27 |
some other access path.
|
| 28 |
|
| 29 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 30 |
+
cannot be subverted without casting [[expr.const.cast]]. — *end note*]
|
|
|
|
| 31 |
|
| 32 |
+
Any attempt to modify ([[expr.ass]], [[expr.post.incr]],
|
| 33 |
+
[[expr.pre.incr]]) a const object [[basic.type.qualifier]] during its
|
| 34 |
+
lifetime [[basic.life]] results in undefined behavior.
|
| 35 |
|
| 36 |
[*Example 1*:
|
| 37 |
|
| 38 |
``` cpp
|
| 39 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 40 |
+
ci = 4; // error: attempt to modify const
|
| 41 |
|
| 42 |
int i = 2; // not cv-qualified
|
| 43 |
const int* cip; // pointer to const int
|
| 44 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 45 |
+
*cip = 4; // error: attempt to modify through ptr to const
|
| 46 |
|
| 47 |
int* ip;
|
| 48 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 49 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 50 |
|
| 51 |
const int* ciq = new const int (3); // initialized as required
|
| 52 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 53 |
+
*iq = 4; // undefined behavior: modifies a const object
|
| 54 |
```
|
| 55 |
|
| 56 |
For another example,
|
| 57 |
|
| 58 |
``` cpp
|
|
|
|
| 65 |
Y();
|
| 66 |
};
|
| 67 |
|
| 68 |
const Y y;
|
| 69 |
y.x.i++; // well-formed: mutable member can be modified
|
| 70 |
+
y.x.j++; // error: const-qualified member modified
|
| 71 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 72 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 73 |
+
p->x.j = 99; // undefined behavior: modifies a const subobject
|
| 74 |
```
|
| 75 |
|
| 76 |
— *end example*]
|
| 77 |
|
| 78 |
The semantics of an access through a volatile glvalue are
|