我需要一些关于规范中§8/5 的帮助
I need some help regarding §8/5 in the spec
§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-declarators. [ 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 ]
trailing-return-type 中的“type-id”对我来说没有意义,仅仅是因为 trailing-return-type 根据语法不包含 type-id。
我也不明白数组和函数声明的"ambiguous binding"。据我了解
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();
声明一个 ()
return 指向 int
的函数。
int *f()[4];
声明了一个 ()
returning 数组的函数,指向 int
的 4 个指针。请注意,这是错误的格式。
int (*f())[4];
声明了一个 ()
return 指向 4 int
数组的指针的函数。
现在,在
auto f() -> int(*)[4]
// ^^^^^^^^^^^^^---
规则解决的是 [4]
是否是 trailing-return-type 的一部分,因此是函数声明符的一部分。如果 [4]
是 trailing-return-type 的一部分,则上述声明声明了 ()
returning 指针的函数到 4 int
的数组。
如果不是,则 [4]
将形成一个不属于函数声明符的数组声明符,并且解析将由 [dcl.array]/p1:
控制
In a declaration T D
where D
has the form
D1 [ constant-expression_opt ] attribute-specifier-seq_opt
and the type of the identifier in the declaration T D1
is
“derived-declarator-type-list T
” [..., if] the value of the
constant expression is N
, [...] the type of the identifier of D
is
“derived-declarator-type-list array of N
T
”.
并且由于 auto f()-> int (*)
将 f
声明为 "function of ()
returning pointer to int
",替换告诉我们这将声明一个函数 return 指向 [=16= 的 4 个指针的数组], 就像 int *f()[4];
.
§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-declarators. [ 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 ]
trailing-return-type 中的“type-id”对我来说没有意义,仅仅是因为 trailing-return-type 根据语法不包含 type-id。
我也不明白数组和函数声明的"ambiguous binding"。据我了解
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();
声明一个 ()
return 指向 int
的函数。
int *f()[4];
声明了一个 ()
returning 数组的函数,指向 int
的 4 个指针。请注意,这是错误的格式。
int (*f())[4];
声明了一个 ()
return 指向 4 int
数组的指针的函数。
现在,在
auto f() -> int(*)[4]
// ^^^^^^^^^^^^^---
规则解决的是 [4]
是否是 trailing-return-type 的一部分,因此是函数声明符的一部分。如果 [4]
是 trailing-return-type 的一部分,则上述声明声明了 ()
returning 指针的函数到 4 int
的数组。
如果不是,则 [4]
将形成一个不属于函数声明符的数组声明符,并且解析将由 [dcl.array]/p1:
In a declaration
T D
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
”.
并且由于 auto f()-> int (*)
将 f
声明为 "function of ()
returning pointer to int
",替换告诉我们这将声明一个函数 return 指向 [=16= 的 4 个指针的数组], 就像 int *f()[4];
.