这是我第二次研究 C++ 中的结构声明。 (第一个是 here )但是现在我遇到了 this post .具体来说,我不确定为什么这在 C 中完全没问题,但在 C++ 中却不行。
typedef struct{
int one;
int two;
}myStruct;
struct myStruct; //forward declaration fails
void blah(myStruct* pStruct);
上面的代码在我的带有 GCC 的 Ubuntu 机器上编译得很好。我认为这是因为第一个 myStruct
位于函数、变量名称所在的普通命名空间中。第二个 myStruct
位于 Tag 命名空间中。当编译器在函数原型(prototype)中看到 myStruct*
时,它会在两个命名空间中搜索并在正常命名空间中找到 myStruct
并且该名称恰好是 typedef
名称,因此它可以是一个有效的类型说明符。第二个 myStruct
可以稍后定义为程序员想要的任何内容。与第一个 未命名 myStruct
不会有任何混淆/冲突,因为程序员必须使用 struct myStruct
来引用第二个。
但是在 C++ 中,根据在 linked question 中找到的讨论,我的理解是第一个 typedef myStruct
像往常一样存在于普通命名空间中。第二个 myStruct
也存在于
普通命名空间(在 C++ 中没有特定的 tag 命名空间?)但可以被其他标识符所掩盖。所以我的问题是,为什么与第二个 myStruct
位于同一命名空间的第一个 myStruct
不会隐藏第二个 myStruct
?
在更一般的意义上,除了程序员使用 C++ 语言提供的命名空间工具引入的显式命名空间之外,是否存在任何预定义的命名空间来消除标识符(包括标记、标签、typedef 名称、对象/函数标识符)就像在 C 中一样? (C 在我的第一次调查中发现了 4 个预定义的 namespace )。我能否在说明这些名称所属位置的 C++ 标准中找到它们?
编辑:看来我问的问题不够清楚。我只想知道
1) Labels, typedef names, struct/union/enum 的tag names, normal function, normal variable/object names属于哪些命名空间(如果语言中有定义的话)? (如果我遗漏了任何其他类型的名称,请添加。)
2) 为什么普通函数名、普通变量名可以影子标签名,而标签名不能。
3) 如果 C++ 中有任何像 C 中那样指定 namespace 的子句 ( Section 6.2.1 )
最佳答案
在 C++ 中,您不能使用 struct myStruct
来引用已被 typedef
编辑的无标记结构。并且您不能定义不同的 struct myStruct
,因为名称与 typedef 名称冲突。
如果添加标签,那么在 C 和 C++ 中,struct myStruct
和 myStruct
都将引用该类型:
typedef struct myStruct {
int one;
int two;
} myStruct;
这里在 C++ 中没有冲突,因为名称只解析为一种类型,并且这是由特殊规则明确允许的。 C++ 标准第 7.1.3 节包括以下规则:
In a given non-class scope, a
typedef
specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.If a typedef specifier is used to redefine in a given scope an entity that can be referenced using an elaborated-type-specifier, the entity can continue to be referenced by an elaborated-type-specifier or as an enumeration or class name in an enumeration or class definition respectively.
In a given scope, a typedef specifier shall not be used to redefine the name of any type declared in that scope to refer to a different type.
Similarly, in a given scope, a class or enumeration shall not be declared with the same name as a typedef-name that is declared in that scope and refers to a type other than the class or enumeration itself.
[ Note: A typedef-name that names a class type, or a cv-qualified version thereof, is also a class-name (9.1). If a typedef-name is used to identify the subject of an elaborated-type-specifier (7.1.6.3), a class definition (Clause 9), a constructor declaration (12.1), or a destructor declaration (12.4), the program is ill-formed. — end note ]
关于c++ - Typedef-name 与 C++ 中的 struct 标记冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386191/