Why is the new int (*) [3] error?

typedef int (*A)[3]; int (**p)[3] = new A; // OK int (**q)[3] = new (int(*)[3]); // OK int (**r)[3] = new int (*)[3]; // error 

The error from GCC is equal to error: expected primary-expression before ')' token . Why are extra brackets required in this expression?

+8
c ++ new-operator syntax-error
source share
1 answer

The standard defines the identifier new-type as the longest sequence of new-declarators. There is also a note that illustrates a similar situation (although it highlights function pointers):

5.3.4 New [expr.new]

....

new identifier type:
type-specifier-seq new-declarator opt

new descriptor:
ptr-operator new-declarator opt
noptr new descriptor-

noptr new-descriptor:
[expression] Attribute specifier-cl <sub> non-automatic sub>
noptr new descriptor- [constant expression] Attribute specifier-cl <Sub> non-automatic sub>

....

The identifier of the new type in the new expression is the longest sequence of new declarators. [Note: this prevents ambiguities between declarator operators & , && , * and [] and their equivalent expressions. - end note] [Example:

 new int * i; // syntax error: parsed as (new int*) i, not as (new int)*i 

* is a pointer pointer, not a multiplication operator. - end of example]

[Note: parentheses in the new-type-id of a new expression can have amazing effects. [Example:

 new int(*[10])(); // error 

poorly formed because binding

 (new int) (*[10])(); // error 

Instead, the explicitly bracketed version of the new operator can be used to create objects of composite types (3.9.2):

 new (int (*[10])()); 

selects an array of 10 function pointers (without arguments and returning int . - end of example] - end of note]

+10
source share

All Articles