§8/5:
The optional attribute-specifier-seq in a trailing-return-type appertains to the indicated return type. The type-id in a trailing-return-type includes the longest possible sequence of abstract-declarator s. [ Note: This resolves the ambiguous binding of array and function declarators. [ Example:
auto f()->int(*)[4]; // function returning a pointer to array[4] of int // not function returning array[4] of pointer to int
—end example ] —end note ]
The " type-id in a trailing-return-type " doesn't make sense to me, simply because a trailing-return-type doesn't contain a type-id according to the grammar.
I also don't understand the "ambiguous binding" of array and function declaration. As far as I can understand
auto f() -> int*[4]; // function returning an array of 4 pointers to int
auto f() -> int(*)[4]; // function returning a pointer to an array of 4 ints
int *f();
Declares a function of ()
returning pointer to int
.
int *f()[4];
Declares a function of ()
returning array of 4 pointers to int
. Note that this is ill-formed.
int (*f())[4];
Declares a function of ()
returning pointer to array of 4 int
.
Now, in
auto f() -> int(*)[4]
// ^^^^^^^^^^^^^---
What the rule resolves is whether [4]
is part of the trailing-return-type , and hence part of the the function declarator. If [4]
is part of the trailing-return-type , then the above declaration declares a function of ()
returning pointer to array of 4 int
.
If not, then [4]
would form an array declarator that is not part of the function declarator, and the parse would be governed by [dcl.array]/p1:
In a declaration
TD
whereD
has the formD1 [ constant-expression_opt ] attribute-specifier-seq_opt
and the type of the identifier in the declaration
T D1
is “ derived-declarator-type-listT
” [..., if] the value of the constant expression isN
, [...] the type of the identifier ofD
is “ derived-declarator-type-list array ofN
T
”.
and since auto f()-> int (*)
declares f
as "function of ()
returning pointer to int
", substitution tells us that this would declare a function returning an array of 4 pointers to int
, just like int *f()[4];
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.