关于用括号括起来的声明类型的问题
An issue about what's the type of a declaration which is surrounded by parentheses
在 dcl.meaning 部分,它说:
Thus, a declaration of a particular identifier has the form
T D
where T is of the form attribute-specifier-seq opt decl-specifier-seq and D is a declarator. Following is a recursive procedure for determining the type specified for the contained declarator-id by such a declaration.
[bullet 6] In a declaration T D where D has the form
( D1 )
the type of the contained declarator-id is the same as that of the contained declarator-id in the declaration
T D1
Parentheses do not alter the type of the embedded declarator-id, but they can alter the binding of complex declarators.
但是,考虑下面的代码
int main(){
int* (ptr) = nullptr;
}
在我的示例中,(ptr)
符合 (D1)
的形式,但它不是 声明符 ,在我的示例中完整的声明符是 *ptr
.根据上面提到的项目符号,(D1)
表示D
,D
是声明的declarator(注意强调部分),即即,(D1)
应为声明的声明符。只有形式 int (*ptr)
是项目符号 6 所谈论的情况。项目符号 6 似乎没有涵盖示例 int* (ptr)
。那么,如何解读这样的案例呢?这样的 declarator-id(int* (ptr)
) 的类型是什么?如果我误解了第6点,如何正确理解第6点?还是第 6 条是措辞上的缺陷而忽略了这种情况?
您必须继续下一节。项目符号 6 只是一种情况,不适用于此处,因为您的声明不是 T (D1)
的形式。它的形式是 T D
与 T = int
和 D = *(ptr)
。 D = *D1
(此处与D1 = (ptr)
匹配)由[dcl.ptr]
处理:
In a declaration T D
where D
has the form
*<em>attribute-specifier-seq</em><em>_opt</em> <em>cv-qualifier-seq</em><em>_opt</em> D1
and the type of the identifier in the declaration T D1
is "<em>derived-declarator-type-list</em> T
", then the type of the identifier of D
is "derived-declarator-type-list cv-qualifier-seq pointer to T
". The cv-qualifiers apply to the pointer and not to the object pointed to. Similarly, the optional attribute-specifier-seq appertains to the pointer and not to the object pointed to.
所以,为了知道int *(ptr)
是什么意思,我们首先考虑int (ptr)
是什么意思。 现在 你在 [dcl.meaning]
中的引用开始发挥作用,说这与 int ptr
相同。因此,int (ptr)
会将标识符 ptr
声明为类型 int
。然后,根据我引用的规则,int *(ptr)
将 ptr
声明为指向 int
.
的指针
在 dcl.meaning 部分,它说:
Thus, a declaration of a particular identifier has the form
T D
where T is of the form attribute-specifier-seq opt decl-specifier-seq and D is a declarator. Following is a recursive procedure for determining the type specified for the contained declarator-id by such a declaration.
[bullet 6] In a declaration T D where D has the form
( D1 )
the type of the contained declarator-id is the same as that of the contained declarator-id in the declaration
T D1
Parentheses do not alter the type of the embedded declarator-id, but they can alter the binding of complex declarators.
但是,考虑下面的代码
int main(){
int* (ptr) = nullptr;
}
在我的示例中,(ptr)
符合 (D1)
的形式,但它不是 声明符 ,在我的示例中完整的声明符是 *ptr
.根据上面提到的项目符号,(D1)
表示D
,D
是声明的declarator(注意强调部分),即即,(D1)
应为声明的声明符。只有形式 int (*ptr)
是项目符号 6 所谈论的情况。项目符号 6 似乎没有涵盖示例 int* (ptr)
。那么,如何解读这样的案例呢?这样的 declarator-id(int* (ptr)
) 的类型是什么?如果我误解了第6点,如何正确理解第6点?还是第 6 条是措辞上的缺陷而忽略了这种情况?
您必须继续下一节。项目符号 6 只是一种情况,不适用于此处,因为您的声明不是 T (D1)
的形式。它的形式是 T D
与 T = int
和 D = *(ptr)
。 D = *D1
(此处与D1 = (ptr)
匹配)由[dcl.ptr]
处理:
In a declaration
T D
whereD
has the form*<em>attribute-specifier-seq</em><em>_opt</em> <em>cv-qualifier-seq</em><em>_opt</em> D1
and the type of the identifier in the declaration
T D1
is "<em>derived-declarator-type-list</em> T
", then the type of the identifier ofD
is "derived-declarator-type-list cv-qualifier-seq pointer toT
". The cv-qualifiers apply to the pointer and not to the object pointed to. Similarly, the optional attribute-specifier-seq appertains to the pointer and not to the object pointed to.
所以,为了知道int *(ptr)
是什么意思,我们首先考虑int (ptr)
是什么意思。 现在 你在 [dcl.meaning]
中的引用开始发挥作用,说这与 int ptr
相同。因此,int (ptr)
会将标识符 ptr
声明为类型 int
。然后,根据我引用的规则,int *(ptr)
将 ptr
声明为指向 int
.