Leo*_*eon 7 c++ language-lawyer trailing-return-type c++14
§8/ 5:
可选属性说明符-SEQ的尾返回型 appertains所指示的返回类型.的类型ID的 尾返回型包括的可能的最长序列抽象声明符秒.[注意:这解决了数组和函数声明符的模糊绑定.[例如:
Run Code Online (Sandbox Code Playgroud)auto f()->int(*)[4]; // function returning a pointer to array[4] of int // not function returning array[4] of pointer to int- 末端示例] - 尾注]
" trailing-return-type中的type-id "对我来说没有意义,只是因为trailing-return-type根据语法不包含type-id.
我也不理解数组和函数声明的"模糊绑定".据我所知
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
Run Code Online (Sandbox Code Playgroud)
int *f();
Run Code Online (Sandbox Code Playgroud)
声明一个()返回指针的函数int.
int *f()[4];
Run Code Online (Sandbox Code Playgroud)
声明一个()返回4个指针数组的函数int.请注意,这是不正确的.
int (*f())[4];
Run Code Online (Sandbox Code Playgroud)
声明一个()返回指向4数组的指针的函数int.
现在,在
auto f() -> int(*)[4]
// ^^^^^^^^^^^^^---
Run Code Online (Sandbox Code Playgroud)
规则解析的是是否[4]是trailing-return-type的一部分,因此是函数声明符的一部分.如果[4]是trailing-return-type的一部分,则上面的声明声明了一个()返回指向4数组的指针的函数int.
如果没有,那么[4]将形成一个不属于函数声明符的数组声明符,并且该解析将由[dcl.array]/p1管理:
在具有表格的声明
T D中DRun Code Online (Sandbox Code Playgroud)D1 [ constant-expression_opt ] attribute-specifier-seq_opt并且声明中标识符的类型
T D1是" derived-declarator-type-listT"[...,if]常量表达式的值是N,[...]标识符的类型D是" derived-declarator" -type-list数组NT".
并且因为auto f()-> int (*)声明f为" ()返回指针的函数int",替换告诉我们这将声明一个函数返回一个包含4个指针的数组int,就像int *f()[4];.