简体   繁体   English

关于规范中的§8/ 5,我需要一些帮助

[英]I need some help regarding §8/5 in the spec

§8/5: §8/ 5:

The optional attribute-specifier-seq in a trailing-return-type appertains to the indicated return type. trailing-return-type中的可选attribute-specifier-seq属于指示的返回类型。 The type-id in a trailing-return-type includes the longest possible sequence of abstract-declarator s. trailing-return-type中type-id包括尽可能长的abstract-declarator序列。 [ 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. trailing-return-type中的type-id ”对我来说没有意义,只是因为trailing-return-type根据语法不包含type-id

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指针。

int *f()[4];

Declares a function of () returning array of 4 pointers to int . 声明一个函数()返回4个指针到int数组。 Note that this is ill-formed. 请注意,这是不正确的。

int (*f())[4];

Declares a function of () returning pointer to array of 4 int . 声明一个函数()返回指向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. 规则解析的是[4]是否是trailing-return-type的一部分,因此是函数声明符的一部分。 If [4] is part of the trailing-return-type , then the above declaration declares a function of () returning pointer to array of 4 int . 如果[4]trailing-return-type的一部分 ,那么上面的声明声明了一个函数()返回指向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: 如果没有,则[4]将形成一个不属于函数声明符的数组声明符,并且该解析将由[dcl.array] / p1管理:

In a declaration TD where D has the form TD的声明中, D表格

 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 ”. 并且声明T D1中的标识符类型是“ derived-declarator-type-list T ”[...,if]常量表达式的值是N ,[...] D的标识符的类型是“ N T derived-declarator-type-list数组”。

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]; 并且因为auto f()-> int (*)声明f为“函数()返回指向int函数”,替换告诉我们这将声明一个函数返回一个4指针到int的数组,就像int *f()[4]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM