【问题标题】:I need some help regarding §8/5 in the spec我需要一些关于规范中 §8/5 的帮助
【发布时间】:2015-04-06 21:06:33
【问题描述】:

§8/5:

trailing-return-type 中的可选 attribute-specifier-seq 属于指定的返回类型。中的 type-id trailing-return-type 包括可能的最长序列 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  

【问题讨论】:

  • 你能问个问题吗?
  • 大概 type-id 指的是-> 之后的整个事物,因为 trailing-type-specifier-seq 生成/是type-specifier-seq 的子集(查看 C++11 §7.1.6/1)。
  • @Cheersandhth.-Alf 我认为这是对的。不过写的比较草率。
  • 尾随返回类型的语法中曾经有一个type-idN2927 更改了那部分语法,并将您引用的段落从 §8.3.5/3 移至 §8/5,但没有更改措辞(这可能是一个疏忽)。

标签: c++ language-lawyer c++14 trailing-return-type


【解决方案1】:
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:

T D 的声明中,D 的格式为

D1 [ constant-expression_opt ] attribute-specifier-seq_opt

声明T D1中标识符的类型是 “derived-declarator-type-list T” [..., if] 的值 常量表达式是N,[...] D 的标识符的类型是 “derived-declarator-type-list 数组 N T”。

并且由于auto f()-> int (*)f 声明为“() 的函数返回指向int 的指针”,因此替换告诉我们这将声明一个函数返回一个由4 个指向int 的指针组成的数组,就像@ 987654351@.

【讨论】:

  • 根据[dcl.decl]p4中的语法,这个[4]的左边一定有一个noptr-declarator,但是好像f() -> int(*)不符合@ 987654355@.
  • ... 在N2541 中,f() -> int(*) 相当于direct-declarator,它可以出现在[4] 的左侧,因此需要消除歧义。但是从那时起,声明符的语法发生了相当多的变化。我想知道这些变化之一是否已经悄悄消除了歧义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 2014-07-18
  • 2011-08-25
  • 1970-01-01
相关资源
最近更新 更多