簡體   English   中英

關於規范中的§8/ 5,我需要一些幫助

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

§8/ 5:

trailing-return-type中的可選attribute-specifier-seq屬於指示的返回類型。 trailing-return-type中type-id包括盡可能長的abstract-declarator序列。 [注意:這解決了數組和函數聲明符的模糊綁定。 [例如:

 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  
int *f();

聲明一個函數()返回指向int指針。

int *f()[4];

聲明一個函數()返回4個指針到int數組。 請注意,這是不正確的。

int (*f())[4];

聲明一個函數()返回指向4 int數組的指針。

現在,在

  auto f() -> int(*)[4]
//     ^^^^^^^^^^^^^---

規則解析的是[4]是否是trailing-return-type的一部分,因此是函數聲明符的一部分。 如果[4]trailing-return-type的一部分 ,那么上面的聲明聲明了一個函數()返回指向4 int數組的指針。

如果沒有,則[4]將形成一個不屬於函數聲明符的數組聲明符,並且該解析將由[dcl.array] / p1管理:

TD的聲明中, D表格

 D1 [ constant-expression_opt ] attribute-specifier-seq_opt 

並且聲明T D1中的標識符類型是“ derived-declarator-type-list T ”[...,if]常量表達式的值是N ,[...] D的標識符的類型是“ N T derived-declarator-type-list數組”。

並且因為auto f()-> int (*)聲明f為“函數()返回指向int函數”,替換告訴我們這將聲明一個函數返回一個4指針到int的數組,就像int *f()[4];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM