簡體   English   中英

標准中的規則在哪里指定具有尾隨返回類型的 function 其返回類型包含占位符類型應為“自動”

[英]Where does the rule in the standard specify that a function with a trailing-return-type whose return type contains a placeholder type should be `auto`

auto* fun()->int*{}
int main(){
}

代碼段被 Clang 和 GCC 拒絕,並報告錯誤,即

'fun' function 帶有尾隨返回類型,其類型為 'auto*' 而不是普通的 'auto'

但是,我沒有在標准中找到任何禁止這種用法的規則。 我覺得對應的唯一相關規則如下:

在聲明 TD 中,其中 D 具有以下形式

 D1 ( parameter-declaration-clause ) cv-qualifier-seq opt ref-qualifier opt noexcept-specifier opt attribute-specifier-seq opt trailing-return-type

並且聲明 T D1 中包含的 declarator-id 的類型是“derived-declarator-type-list T”, T 應為單個類型說明符 auto

但是,根據dcl.meaning#3 ,它說:

因此,特定標識符的聲明具有以下形式

運輸署

其中 T 的形式為 attribute-specifier-seq opt decl-specifier-seq 而 D 是一個聲明符。

因此,回到這個例子, T是 auto 並且聲明符是*fun()->int* 因此,這里的T是單個類型說明符 auto,聲明不違反任何規則。 為什么 GCC 和 Clang 都拒絕有效代碼? 如果我錯過了標准中禁止這種用法的規則,那么規則是什么?

是語法。 這是核心問題 681

declarator:
  ptr-declarator
  noptr-declarator parameters-and-qualifiers trailing-return-type

ptr-declarator:
  noptr-declarator
  ptr-operator ptr-declarator

noptr-declarator:
  declarator-id attribute-specifier-seq_opt
  noptr-declarator parameters-and-qualifiers
  noptr-declarator [ constant-expression_opt ] attribute-specifier-seq_opt
  ( ptr-declarator )

*f () -> whatever不是有效的聲明符 語法不允許該 position 處的ptr 聲明符。 (因此,解釋聲明符的規則不提供這種情況也就不足為奇了——正如評論中所討論的那樣。)

[dcl.fct]/2指出:

在聲明 TD 中,其中 D 具有以下形式

D1 ( parameter-declaration-clause ) cv-qualifier-seq-opt ref-qualifier-opt noexcept-specifier-opt attribute-specifier-seq-opt trailing-return-type

並且聲明 T D1 中包含的 declarator-id 的類型為“derived-declarator-type-list T”, T 應為單個類型說明符 auto。 [...]

強調我的

這是需要為auto的地方。

暫無
暫無

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

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