在 C++ (C++11) 标准的不同地方,声明是根据 derived-declarator-type-list 来描述的。我正在研究右值引用,在这种情况下使用这个术语是至关重要的(第 8.3.2 节):
In a declaration T D where D has either of the forms
& attribute-specifier-seqopt D1
&& attribute-specifier-seqopt D1
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 “derived-declarator-type-list reference to T.”
不幸的是,标准中从未定义“derived-declarator-type”类别。 (我查看了“派生”一词的所有用法,此外,这可能得到证实 here 和 here。)
因为“derived-declarator-type-list”是斜体,我假设它指的是一个类别,而不是一个变量标签,如 T
(因此,我不同意 Doug Gwyn 在我刚刚给出的第二个链接中的评估,即“我们本可以使用 X
而不是 'derived-声明符类型列表' ")。
derived-declarator-type在C++11标准中的定义是什么?
最佳答案
它被立即定义。这是一种将 T
之前的内容传递到下一个类型的方法,类似于:
<some stuff> T
<some stuff> reference to T
这只是 T D1
类型中 T
之前的任何内容。
例如,如果你有声明 int& (*const * p)[30]
, T
是 int
, D
是 & (*const * p)[30]
并且 D1
是 (*const * p)[30]
。 T D1
的类型是“指向 30 int 数组的 const 指针的指针”。因此,根据您引用的规则,p
的类型是“指向 30 引用 int 数组的 const 指针的指针”。
当然,该声明随后被 §3.4.2/5 禁止:
There shall be no references to references, no arrays of references, and no pointers to references.
我认为它是 派生 声明类型列表的非正式术语来自 C 标准对 派生类型 的定义(类似于 C++ 中的复合类型):
Any number of derived types can be constructed from the object, function, and incomplete types, as follows:
- An array type [...]
- An structure type [...]
- An union type [...]
- An function type [...]
- An pointer type [...]
回应评论:您似乎对类型和声明符感到困惑。例如,如果 int* p
是声明符,则 p
的类型是“指向 int 的指针”。类型表示为这些类似英语的句子。
示例 1:int *(&p)[30]
这是一个声明 T D
where (§8.3.1 Pointers):
T
->int
D
->*(&p)[3]
D
的形式为:
*
attribute-specifier-seqopt cv-qualifier-seqoptD1
其中 D1
是 (&p)[3]
。这意味着 T D1
的形式是 int (&p)[3]
,其类型为“对 3 int
数组的引用”(你工作递归,下一步使用 §8.3.4 数组等)。 int
之前的所有内容都是derived-declarator-type-list。所以我们可以推断出我们原始声明中的 p
类型为“reference to array of 3 pointer to int
”。魔法!
示例 2:float (*(*(&e)[10])())[5]
这是一个声明 T D
where (§8.3.4 Arrays):
T
->float
D
->(*(*(&e)[10])())[5]
D
的形式为:
D1 [
constant-expressionopt]
attribute-specifier-seqopt
其中 D1
是 (*(*(&e)[10])())
。这意味着 T D1
的形式为 float (*(*(&e)[10])())
,其类型为“对 10 个指向函数的数组的引用() 返回指向 float 的指针”(您可以通过应用 §8.3/6 和 §8.3.1 指针等来计算)。 float
之前的所有内容都是derived-declarator-type-list。所以我们可以推断出我们原始声明中的 p
具有类型“对 10 的数组的引用,指向 () 的函数返回指向 5 float 组的指针”。再次魔法!
关于c++ - 在标准中,什么是 "derived-declarator-type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779273/