gpt4 book ai didi

c++ - 了解派生声明符类型列表和数组声明符

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:09 24 4
gpt4 key购买 nike

8.3.4/1 N3797 节:

In a declaration T D where D has the form

D1 [ constant-expressionopt] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type;

我已经从 Q|A 中了解什么是 derived-declarator-type-list .

但我对声明 T D 的示例感兴趣,这样声明 T D1 中的标识符类型不是 的形式“派生声明符类型列表 T”

这是 T D 的标识符类型,其中 D 的形式为

D1 [ constant-expressionopt] attribute-specifier-seqopt 

不是数组类型。有可能吗?

最佳答案

首先,我们在第 8 节中关于声明符。在此上下文中,[] 不能指定下标运算符(表达式,第 5.2.1 章)或 lambda 引入符(表达式,第 5.1.2 章),只能指定数组。

接下来,我们必须考虑我们有简单和更复杂的声明符:

int ndigit[10];    // T is int, D is ndigit[10] which is an array type 
// the type of the identifier ndigit is derived declarator type int
char *str[512]; // T is char*, D is str[10] wich is an array type
// the type of the identifier str is derived declarator type char*

知道这一切后,我们必须深吸一口气,从我们的语言习惯中退后一点。

作为程序员,当我们读到这样的英文句子时

    If P1 and P2 then P3

我们本能地理解有两个条件并在 c++ 中翻译:

    if (P1 && P2) P3;    // 2 conditions must be true and P3 will be done

但是英语不是 C++!还有其他有效的含义。以这个无关的数学例子为例:

    ax²+bx+c =0   
if Δ =b²-4ac and Δ >0 then there are two distinct roots

在此示例中,很明显,Δ =b²-4ac 不是具有两个根的附加条件。它只是一种引入(无需额外句子)在 condition 或 conclusion 中使用的命名约定的方法。

当然有人会说这种风格不是一种好的做法,我们应该这样写:

    ax²+bx+c =0   
let's define Δ =b²-4ac
If Δ >0 then there are two distinct roots

我完全同意!现在让我们将这种阅读应用到标准中:

    In a declaration T D where D has the form
D1 [ constant-expressionopt] attribute-specifier-seqopt
Let the type of the identifier in the declaration T D1 be “derived-declarator-type-list T”
Then the type of the identifier of D is an array type;

这对你来说会更容易理解吗?

这种句子清楚地表明,标准的主要目标不是易于阅读,而是表达明确的复杂语言结构,并完全转换所有语义和句法变体。

关于c++ - 了解派生声明符类型列表和数组声明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25882744/

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