@@ -128,10 +128,14 @@ int[3][5] x; // x is a static array of 5 static arrays of 3 ints
128
128
int[3]*[5] x; // x is a static array of 5 pointers to static arrays of 3 ints
129
129
--------------------
130
130
131
- $(H3 $(LNAME2 pointers-to-functions, Pointers to Functions))
131
+ $(P See $(DDSUBLINK spec/type, pointers, Pointers), $(DDLINK spec/arrays, Arrays, Arrays)
132
+ and $(GLINK2 type, TypeSuffix).)
133
+
134
+ $(H3 $(LNAME2 pointers-to-functions, Function Pointers))
132
135
133
136
$(P
134
- Pointers to functions are declared using the $(D function) keyword:
137
+ $(DDSUBLINK spec/function, function-pointers, Function Pointers)
138
+ are declared using the $(D function) keyword:
135
139
)
136
140
137
141
--------------------
@@ -147,19 +151,31 @@ int function(char)[] x; // x is an array of
147
151
$(H3 $(LNAME2 c-style-declarations, C-Style Declarations))
148
152
149
153
$(P
150
- C-style array, function pointer and pointer to array declarations are deprecated:
154
+ C-style array, function pointer and pointer to array declarations are
155
+ not supported. The following C declarations are for comparison only:
151
156
)
152
157
153
- --------------------
154
- int x[3]; // x is a static array of 3 ints
155
- int x[3][5]; // x is a static array of 3 arrays of 5 ints
158
+ $(CPPCODE
159
+ int x[3]; // C static array of 3 ints
160
+ int x[3][5]; // C static array of 3 arrays of 5 ints
156
161
157
- int (*x[5])[3]; // x is a static array of 5 pointers to static arrays of 3 ints
158
- int (*x)(char); // x is a pointer to a function taking a char argument
162
+ int (\ *x[5])[3]; // C static array of 5 pointers to static arrays of 3 ints
163
+ int (\ *x)(char); // C pointer to a function taking a char argument
159
164
// and returning an int
160
- int (*[] x)(char); // x is an array of pointers to functions
165
+ int (\ *[] x)(char); // C array of pointers to functions
161
166
// taking a char argument and returning an int
162
- --------------------
167
+ )
168
+
169
+ $(RATIONALE
170
+ * In D types are straightforward to read from right to left,
171
+ unlike in C where parentheses are sometimes required and the type is
172
+ read iteratively using the 'right-left' rule.
173
+ * For a C function pointer declaration `a (*b)(c);` a C parser needs
174
+ to attempt a type lookup in order to parse it unambiguously - it
175
+ could be a call to a function called `a` which returns a function
176
+ pointer, which is immediately called. D function pointer syntax
177
+ is unambiguous, avoiding the need for types to be forward declared.
178
+ )
163
179
164
180
$(H3 $(LNAME2 declaring-multiple-symbols, Declaring Multiple Symbols))
165
181
@@ -169,14 +185,17 @@ must be of the same type:
169
185
)
170
186
171
187
--------------------
172
- int x,y; // x and y are ints
173
- int* x,y; // x and y are pointers to ints
174
- int[] x,y; // x and y are arrays of ints
188
+ int x, y; // x and y are ints
189
+ int* x, y; // x and y are pointers to ints
190
+ int[] x, y; // x and y are arrays of ints
191
+ ---
175
192
176
- // invalid C-style declarations
177
- int x,*y; // error, multiple types
178
- int x[],y; // error, multiple types
179
- --------------------
193
+ $(P This is in contrast to C:)
194
+
195
+ $(CPPCODE
196
+ int x, *y; // x is an int, y is a pointer to int
197
+ int x[], y; // x is an array/pointer, y is an int
198
+ )
180
199
181
200
$(H2 $(LEGACY_LNAME2 AutoDeclaration, auto-declaration, Implicit Type Inference))
182
201
0 commit comments