问题描述
在C ++(C ++ 11)标准的不同地方,声明以 derived-declarator-type-list 的形式来描述。我正在研究右值引用,并且在该上下文中使用该术语是至关重要的(§8.3.2):
,类别 derived-declarator-type 从未在标准中定义。 (我通过每次使用派生这个词,此外,这可能得到确认和。)
因为 derived-declarator-type-list 是斜体,我认为它是指一个类别,而不是一个变量标签,例如 T
(因此,我不同意Doug Gwyn在第二个链接中的评估,我刚才说:我们可以使用 X
,而不是 derived-declarator-type-list )。
derived-declarator-type 的定义?
被定义在那里然后。这是一种携带 T
到下一个类型的方式,类似于:
< some stuff> T
< some stuff>引用T
这是 T
T D1
类型
例如,如果您有声明 int& (* const * p)[30]
, T
是 int
, D
是& (* const * p)[30]
和 D1
是 c $ c>。
T D1
的类型是指向const int的数组的const指针。因此,根据你引用的规则, p
的类型是指向const指向数组的指针的30个引用int。
当然,这个声明被§3.4.2/ 5禁止:
我认为它的非正式术语是一个声明类型列表来自C标准对派生类型的定义(类似于C ++中的复合类型):
响应注释:似乎你在类型和声明之间感到困惑。例如,如果 int * p
是声明符,则 p
的类型是指向int的指针。
: int *(& p)[类型] 30]
这是一个声明 TD
其中(§8.3.1指针) :
-
T
- > int
-
D
- > *(& p)[3]
D
的格式如下:
b $ b
其中 D1
是(& p)[3]
。这意味着 T D1
的形式是 int(& p)[3]
of 3 int
(你递归的工作,下一步使用§8.3.4数组等)。 int
之前的所有内容都是 derived-declarator-type-list 。因此,我们可以推断在我们的原始声明中的 p
类型为引用数组3指向 int
。魔法!
示例2 : float(*(*(& e)[10]) [5]
这是一个声明 TD
):
-
T
- > float
-
D
- > (*(*(& e)[10] ())[5]
D
是以下形式:
其中 D1
是(*(*(& e)[10 ])())
。这意味着 T D1
的形式是 float(*(*(& e)[10])())
它具有类型引用数组的指针到函数的函数of()返回指针到浮动(你通过应用§8.3/ 6和然后§8.3.1指针等工作)。 float
之前的所有内容都是 derived-declarator-type-list 。因此,我们可以推断在我们的原始声明中的 p
类型是引用数组的10指针到函数的()返回指针到数组5浮点。魔法!
In different places in the C++ (C++11) standard, declarations are described in terms of derived-declarator-type-list. I am studying rvalue references and the use of this term is critical in that context (§8.3.2):
Unfortunately, the category "derived-declarator-type" is never defined in the standard. (I looked through every use of the word "derived", and in addition this is possibly confirmed here and here.)
Because "derived-declarator-type-list" is italicized, I assume it refers to a category, and not to a variable label such as T
(and therefore, I disagree with Doug Gwyn's assessment in the second link I just gave that "we could have used X
instead of 'derived-declarator-type-list' ").
What is the definition of derived-declarator-type in the C++11 standard?
解决方案 It's being defined right there and then. It's a way of carrying whatever comes before T
across to the next type, similar to:
<some stuff> T
<some stuff> reference to T
It's just whatever comes before T
in the type of T D1
.
For example, if you have the declaration int& (*const * p)[30]
, T
is int
, D
is & (*const * p)[30]
and D1
is (*const * p)[30]
. The type of T D1
is "pointer to const pointer to array of 30 int". And so, according to the rule you quoted, the type of p
is "pointer to const pointer to array of 30 reference to int".
Of course, this declaration is then disallowed by §3.4.2/5:
I think the informal terminology of it being a derived declarator type list comes from the C standard's definition of a derived type (similar to a compound type in C++):
In response to the comments: It seems you're getting confused between the type and the declarator. For example, if int* p
is the declarator, then the type of p
is "pointer to int". The type is expressed as these English-like sentences.
Example 1: int *(&p)[30]
This is a declaration T D
where (§8.3.1 Pointers):
T
-> int
D
-> *(&p)[3]
D
has the form:
where D1
is (&p)[3]
. That means T D1
is of the form int (&p)[3]
which has type "reference to array of 3 int
" (you work this out recursively, next step using §8.3.4 Arrays and so on). Everything before the int
is the derived-declarator-type-list. So we can infer that p
in our original declaration has type "reference to array of 3 pointer to int
". Magic!
Example 2: float (*(*(&e)[10])())[5]
This is a declaration T D
where (§8.3.4 Arrays):
T
-> float
D
-> (*(*(&e)[10])())[5]
D
is of the form:
where D1
is (*(*(&e)[10])())
. This means T D1
is of the form float (*(*(&e)[10])())
which has type "reference to array of 10 pointer to function of () returning pointer to float" (which you work out by applying §8.3/6 and then §8.3.1 Pointers and so on). Everything before the float
is the derived-declarator-type-list. So we can infer that p
in our original declaration has type "reference to array of 10 pointer to function of () returning pointer to array of 5 float". Magic again!
这篇关于在标准中,什么是“派生 - 声明器类型”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!