本文介绍了typedef的名称与C结构标记冲突++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的关于C ++结构声明第二次调查。 (第一个是here)但现在我碰到this帖子。具体我不知道为什么,这是C,但不是在C ++完美的罚款。

This is my second investigation about structure declaration in C++. (The first is here) But now I came across this post. Specifically I am not sure why this is perfectly fine in C but not in C++.

typedef struct{
    int one;
    int two;
}myStruct;

struct myStruct; //forward declaration fails

void blah(myStruct* pStruct);

上面的code编译罚款我与海湾合作委员会的Ubuntu框。我有理由相信,这是因为第一个 MYSTRUCT 生活在正常的命名空间在哪里函数,变量名居住。第二个 MYSTRUCT 住在代码命名空间。当编译器看到 MYSTRUCT * 在函数原型,它会搜索这两个名字空间和正常namspace并找到 MYSTRUCT 名字恰巧是一个的typedef 的名字,所以可以有效的类型说明符。第二个 MYSTRUCT 以后可以定义为任何程序员希望如此。不会有任何混淆/冲突与第一未命名 MYSTRUCT ,因为程序员必须使用结构MYSTRUCT 来指第二个

The code above compiles fine on my Ubuntu box with GCC. I reason that it is because the first myStruct lives in the normal namespace where function, variable names live. The second myStruct lives in the Tag namespace. When compiler sees myStruct* in the function prototype, it searches in both namespaces and found myStruct in the normal namspace and that name happen to be a typedef name, so it can be a valid type specifier. The second myStruct can be defined later as whatever the programmer wants to be. There won't be any confusion/collision with the first unnamed myStruct since the programmer has to use struct myStruct to refer to the second one.

不过,在C ++中,根据发现的linked问题,我的理解是,第一个的typedef MYSTRUCT 生活在正常的命名空间如常。第二个 MYSTRUCT 也住在
正常的命名空间(没有具体的标签在C ++中的命名空间?),但可以通过其它标识来掩盖。所以我的问题是为什么不第一 MYSTRUCT 这是在相同的命名空间第二个 MYSTRUCT 阴影第二 MYSTRUCT

But in C++, according to the discussion found in the linked question, my understanding is that the first typedef myStruct lives in the normal namespace as usual. The second myStruct also lives inthe normal namespace(no specific tag namespace in C++?) but can be overshadowed by other identifiers. So my question is why wouldn't the first myStruct which is in the same namespace as the second myStruct shadow the second myStruct?

在更普遍的意义,比使用由C ++语言提供的空间设施由程序员引入明确的命名空间外,还有什么pre-定义的命名空间歧义利用标识符(包括吊牌,标签,typedef名称,对象/ functino标识符)和C一样? (C有4个命名空间pre定义在我的第一次调查中发现)。我可以在C ++标准中找到这些说明当这些名字属于?

In a more general sense, other than explicit namespaces introduced by the programmer using the namespace facility provided by the C++ language, are there any pre-defined namespaces disambiguating the use of identifiers (including tags, labels, typedef names, object/functino identifiers) like in C? (C has 4 namespaces pre-defined found in my first investigation). Can I find these in the C++ standard stating where these names belong?

修改:看来我没有问足够的问题清楚。所有我想知道的是

EDIT: It seems I didn't ask the question clear enough. All I want to know is

1)哪些命名空间(如果有这样的语言定义)都标贴,typedef名称,结构/联合/枚举,功能正常,正常的变量/对象名称的标签名称属于? (如果我错过了任何其他种类的名称,请补充。)

1) Which namespaces (if there are such defined in the language) do Lables, typedef names, tag names of struct/union/enum, normal function, normal variable/object name belong? (If I missed any other kinds of name, please add.)

2)为什么不能正常函数名,普通变量名阴影标记名称,而标签名称不能。

2) Why can normal function name, normal variable name shadow tag names, while tag names can NOT.

3)如果在任何C ++条款指定的名称空间,如在C(的)

3) If there is any clauses in C++ that specify the name spaces like in C (Section 6.2.1)

推荐答案

在C ++中,你不能使用结构MYSTRUCT 指一个无代码结构已的typedef 编辑。而你不能定义一个不同的结构MYSTRUCT ,因为这个名字碰撞的typedef名称。

In C++, you cannot use struct myStruct to refer to a tagless structure which has been typedefed. And you cannot define a different struct myStruct, because the name collides with the typedef name.

如果您添加标记,那么这两个结构MYSTRUCT MYSTRUCT 单独将引用类型,在这两个C和C ++:

If you add the tag, then both struct myStruct and myStruct alone will refer to the type, in both C and C++:

typedef struct myStruct {
    int one;
    int two;
} myStruct;

在这里有在C中没有碰撞++,因为名称解析为一种类型,这是专门由一个特殊的规则允许的。 C ++标准第7.1.3节包括以下规则:

Here there is no collision in C++ because the name resolves to just one type, and this is specifically allowed by a special rule. C++ Standard section 7.1.3 includes the following rules:

在一个给定的非类范围,一个的typedef 符可以用来惹得网络NE在该范围内声明引用的类型,它已经任意类型的名称指。

如果一个typedef特定连接器用于惹得网络NE在一个给定范围内可以使用精心设计的类型说明符引用一个实体,该实体可以继续通过精心设计的类型说明符或枚举或类名称引用在枚举或类去连接nition分别。

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.

在一个给定的范围内,一个typedef说明符不得用于惹得网络NE在该范围指二FF erent类型声明的任何类型的名称。

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.

同样,在一个给定的范围内,一类或枚举,不得使用相同的名称作为一个typedef名是在范围内声明,指的是不是类或枚举本身以外的其他类型声明。

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.

[注:的typedef名称名称类类型,或它们的CV-合格的版本,也是一个类名(9.1)。 如果一个typedef名是用来识别一个阐述类型说明符的主题(7.1.6.3),一类定义(第9条),构造函数声明(12.1),或析构函数(12.4),程序形成不良
   - 注完]

[ 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 ]

这篇关于typedef的名称与C结构标记冲突++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:20