c++ - operator new syntax for array of pointers -
i have run next code
int main() { int **objects; objects=new (int(*[10])); // seems equal new int*[10]; delete[] objects; homecoming 0; }
i have not managed parse line "new (int(*[10]))". used standard syntax "new int*[10]" , totally surprised 1 above.
could explain me why "new (int(*[10]))" right , same "new int*[10]"?
here 2 grammatical constructions @ work, straight [c++11: 5.3.4]
:
new-expression: ::
optnew
new-placementopt new-type-id new-initializeropt ::
optnew
new-placementopt(
type-id )
new-initializeropt
you're used former have encountered latter. let's take closer @ one; type-id , how differ new-type-id?
[c++11: 5.3.4/3]:
new-type-id in new-expression longest possible sequence of new-declarators. [ note: prevents ambiguities between declarator operators &, &&, *, , [] , look counterparts. —end note ] [ example:
new int * i; // syntax error: parsed (new int*) i, not (new int)*i
the * pointer declarator , not multiplication operator. —end illustration ]
so, see, why new (int(*[10]))
valid new int(*[10])
not: outer parenthesis allows 5.3.4/3
kick in.
this addressed in next passage, validity of inner parenthesis in illustration very similar yours:
[c++11: 5.3.4/4]:
[ note: parentheses in new-type-id of new-expression can have surprising effects. [ example:
new int(*[10])(); // error
is ill-formed because binding is
(new int) (*[10])(); // error
instead, explicitly parenthesized version of new operator can used create objects of compound types (3.9.2):
new (int (*[10])());
allocates array of 10 pointers functions (taking no argument , returning int
.) —end illustration ]
—end note ]
in case, although using pointers rather pointers-to-functions (note additional ()
in above quote examples), int (*[10])
still compound type , same logic applies.
finally, int (*[10])
same int* [10]
because it is: that's how syntax of type-id constructions works:
<tomalak> << type_desc<int (*[10])>; <geordi> array of 10 pointers integers <tomalak> << type_desc<int* [10]>; <geordi> same output.
(using geordi)
c++
No comments:
Post a Comment