gpt4 book ai didi

c++ - 关于c++1z中[dcl.spec]/3的疑问

转载 作者:搜寻专家 更新时间:2023-10-31 01:35:36 25 4
gpt4 key购买 nike

[dcl.spec]/3 in c++1z (重点是我的):

If a type-name is encountered while parsing a decl-specifier-seq, it is interpreted as part of the decl-specifier-seq if and only if there is no previous defining-type-specifier other than a cv-qualifier in the decl-specifier-seq. The sequence shall be self-consistent as described below. [ Example:

typedef char* Pc;
static Pc; // error: name missing

Here, the declaration static Pc is ill-formed because no name was specified for the static variable of type Pc. To get a variable called Pc, a type-specifier (other than const or volatile) has to be present to indicate that the typedef-name Pc is the name being (re)declared, rather than being part of the decl-specifier sequence.

不过,下面的代码片段无法在 g++ 和 clang 中编译。这是为什么?

typedef char* Pc;
static int Pc;

最佳答案

该段的目的是表明 typedef char* Pc 是有效的,而 static Pc 不是,不是Pc 重新声明为其他内容(在同一范围内)。换句话说,static Pcstatic int Pc 不是不同的实体。没有任何更改允许您执行此操作。

[basic.scope.declarative]/4

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

(4.1) — they shall all refer to the same entity, ...

关于c++ - 关于c++1z中[dcl.spec]/3的疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37037842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com