From Jason Turner

[diff.expr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpe_h5a2wa/{from.md → to.md} +93 -21
tmp/tmpe_h5a2wa/{from.md → to.md} RENAMED
@@ -1,64 +1,136 @@
1
  ### [[expr]]: expressions <a id="diff.expr">[[diff.expr]]</a>
2
 
3
- **Change:** Converting `void*` to a pointer-to-object type requires
4
- casting.
 
 
5
 
6
  ``` cpp
7
  char a[10];
8
  void* b=a;
9
  void foo() {
10
  char* c=b;
11
  }
12
  ```
13
 
14
- ISO C accepts this usage of pointer to `void` being assigned to a
15
- pointer to object type. C++ does not. **Rationale:** C++ tries harder
16
- than C to enforce compile-time type safety. **Effect on original
17
- feature:** Deletion of semantically well-defined feature. Can be
18
- automated. Violations will be diagnosed by the C++ translator. The fix
19
- is to add a cast. For example:
 
 
 
 
 
20
 
21
  ``` cpp
22
  char* c = (char*) b;
23
  ```
24
 
25
- This is fairly widely used but it is good programming practice to add
26
- the cast when assigning pointer-to-void to pointer-to-object. Some ISO C
27
- translators will give a warning if the cast is not used.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  **Change:** Decrement operator is not allowed with `bool` operand.
30
  **Rationale:** Feature with surprising semantics. **Effect on original
31
- feature:** A valid ISO C expression utilizing the decrement operator on
32
- a `bool` lvalue (for instance, via the C typedef in `<stdbool.h>`) is
33
  ill-formed in C++.
34
 
35
  **Change:** In C++, types can only be defined in declarations, not in
36
  expressions.
37
  In C, a `sizeof` expression or cast expression may define a new type.
38
- For example,
 
39
 
40
  ``` cpp
41
  p = (void*)(struct x {int i;} *)0;
42
  ```
43
 
44
- defines a new type, struct `x`. **Rationale:** This prohibition helps to
45
- clarify the location of definitions in the source code. **Effect on
46
- original feature:** Deletion of semantically well-defined feature.
47
- Syntactic transformation. Seldom.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  **Change:** The result of a conditional expression, an assignment
50
  expression, or a comma expression may be an lvalue. **Rationale:** C++
51
  is an object-oriented language, placing relatively more emphasis on
52
  lvalues. For example, function calls may yield lvalues. **Effect on
53
  original feature:** Change to semantics of well-defined feature. Some C
54
  expressions that implicitly rely on lvalue-to-rvalue conversions will
55
- yield different results. For example,
 
 
56
 
57
  ``` cpp
58
  char arr[100];
59
  sizeof(0, arr)
60
  ```
61
 
62
- yields `100` in C++ and `sizeof(char*)` in C. Programs must add explicit
63
- casts to the appropriate rvalue. Rare.
 
 
 
64
 
 
1
  ### [[expr]]: expressions <a id="diff.expr">[[diff.expr]]</a>
2
 
3
+ **Change:** Converting a prvalue of type “pointer to cv `void`” to a
4
+ pointer-to-object type requires casting.
5
+
6
+ [*Example 1*:
7
 
8
  ``` cpp
9
  char a[10];
10
  void* b=a;
11
  void foo() {
12
  char* c=b;
13
  }
14
  ```
15
 
16
+ C accepts this usage of pointer to `void` being assigned to a pointer
17
+ to object type. C++ does not.
18
+
19
+ *end example*]
20
+
21
+ **Rationale:** C++ tries harder than C to enforce compile-time type
22
+ safety. **Effect on original feature:** Deletion of semantically
23
+ well-defined feature. Can be automated. Violations will be diagnosed by
24
+ the C++ translator. The fix is to add a cast.
25
+
26
+ [*Example 2*:
27
 
28
  ``` cpp
29
  char* c = (char*) b;
30
  ```
31
 
32
+ *end example*]
33
+
34
+ Common.
35
+
36
+ **Change:** Operations mixing a value of an enumeration type and a value
37
+ of a different enumeration type or of a floating-point type are not
38
+ valid.
39
+
40
+ [*Example 3*:
41
+
42
+ ``` cpp
43
+ enum E1 { e };
44
+ enum E2 { f };
45
+ int b = e <= 3.7; // valid in C; ill-formed in C++{}
46
+ int k = f - e; // valid in C; ill-formed in C++{}
47
+ int x = 1 ? e : f; // valid in C; ill-formed in C++{}
48
+ ```
49
+
50
+ — *end example*]
51
+
52
+ **Rationale:** Reinforcing type safety in C++. **Effect on original
53
+ feature:** Well-formed C code will not compile with this International
54
+ Standard. Violations will be diagnosed by the C++ translator. The
55
+ original behavior can be restored with a cast or integral promotion.
56
+
57
+ [*Example 4*:
58
+
59
+ ``` cpp
60
+ enum E1 { e };
61
+ enum E2 { f };
62
+ int b = (int)e <= 3.7;
63
+ int k = +f - e;
64
+ ```
65
+
66
+ — *end example*]
67
+
68
+ Uncommon.
69
 
70
  **Change:** Decrement operator is not allowed with `bool` operand.
71
  **Rationale:** Feature with surprising semantics. **Effect on original
72
+ feature:** A valid C expression utilizing the decrement operator on a
73
+ `bool` lvalue (for instance, via the C typedef in `<stdbool.h>`) is
74
  ill-formed in C++.
75
 
76
  **Change:** In C++, types can only be defined in declarations, not in
77
  expressions.
78
  In C, a `sizeof` expression or cast expression may define a new type.
79
+
80
+ [*Example 5*:
81
 
82
  ``` cpp
83
  p = (void*)(struct x {int i;} *)0;
84
  ```
85
 
86
+ defines a new type, struct `x`.
87
+
88
+ *end example*]
89
+
90
+ **Rationale:** This prohibition helps to clarify the location of
91
+ definitions in the source code. **Effect on original feature:** Deletion
92
+ of semantically well-defined feature. Syntactic transformation. Seldom.
93
+
94
+ **Change:** C allows directly comparing two objects of array type; C++
95
+ does not. **Rationale:** The behavior is confusing because it compares
96
+ not the contents of the two arrays, but their addresses. **Effect on
97
+ original feature:** Deletion of semantically well-defined feature that
98
+ had unspecified behavior in common use cases. Violations will be
99
+ diagnosed by the C++ translator. The original behavior can be replicated
100
+ by explicitly casting either array to a pointer, such as by using a
101
+ unary `+`.
102
+
103
+ [*Example 6*:
104
+
105
+ ``` cpp
106
+ int arr1[5];
107
+ int arr2[5];
108
+ int same = arr1 == arr2; // valid C, ill-formed C++
109
+ int idem = arr1 == +arr2; // valid in both C and C++
110
+ ```
111
+
112
+ — *end example*]
113
+
114
+ Rare.
115
 
116
  **Change:** The result of a conditional expression, an assignment
117
  expression, or a comma expression may be an lvalue. **Rationale:** C++
118
  is an object-oriented language, placing relatively more emphasis on
119
  lvalues. For example, function calls may yield lvalues. **Effect on
120
  original feature:** Change to semantics of well-defined feature. Some C
121
  expressions that implicitly rely on lvalue-to-rvalue conversions will
122
+ yield different results.
123
+
124
+ [*Example 7*:
125
 
126
  ``` cpp
127
  char arr[100];
128
  sizeof(0, arr)
129
  ```
130
 
131
+ yields `100` in C++ and `sizeof(char*)` in C.
132
+
133
+ — *end example*]
134
+
135
+ Programs must add explicit casts to the appropriate rvalue. Rare.
136