标准中的规则在哪里指定具有尾随返回类型且返回类型包含占位符类型的函数应为“自动”

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

此代码段被 Clang 和 GCC 拒绝,并报告错误,即

带有尾随返回类型的“fun”函数将“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 的类型为“衍生声明类型列表 T”,T 应为单个类型说明符 auto

但是,根据dcl.meaning#3,它说:

因此,特定标识符的声明具有以下形式

TD

其中 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不是有效的声明符;语法不允许在该位置使用ptr 声明符。(因此,解释声明符的规则不适用于这种情况也就不足为奇了——正如评论中所讨论的那样。)


以上是标准中的规则在哪里指定具有尾随返回类型且返回类型包含占位符类型的函数应为“自动”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>