简体   繁体   English

C ++为什么命名空间不会与变量和函数碰撞?

[英]C++ Why don't structs / classes collide with variables and functions when namespaces do?

How come namespaces, functions and variables all collide if they have the same name within the same scope, whereas structs / classes, while colliding with the first one, don't collide with variables and functions? 如果命名空间,函数和变量在同一范围内具有相同的名称,它们如何碰撞,而结构/类在与第一个碰撞时不会与变量和函数冲突?

I could see why if structs / classes and namespaces collided with each other but not with functions and variables but I find it odd that structs / classes don't collide with variables and functions when namespaces do, perhaps because they are used in the same way (::) and both make out sorts of namespaces which would explain that they needed to be distinct from each other (whereas the result now seems a bit inconsequent). 我可以看到为什么结构/类和命名空间相互冲突而不是与函数和变量冲突但我发现结构/类在命名空间不与变量和函数冲突时很奇怪,可能是因为它们以相同的方式使用(::)并且两者都列出了各种命名空间,这些命名空间可以解释它们需要彼此不同(而结果现在似乎有点不相干)。

Example 1: 例1:

int A;
struct A {};
//void A() {} //Collision with int A
//namespace A {} //Collision with int A (and also struct A if int A is removed)

Example 2: 例2:

struct A {};
void A() {} 
//int A; //Collision with function A
//namespace A {} //Collision with function A (and also struct A if int A is removed)

Example 3: 例3:

namespace A {}   
//struct A {}; //Collision with namespace A
//void A() {} //Collision with namespace A
//int A; //Collision with namespace A (and function A if namespace A is removed)

I believe the reason why structs, as you say, don't "collide" with variables and functions is compatibility with C. 我相信结构的原因,正如你所说,不与变量和函数“碰撞”是与C的兼容性。

In C, a struct type has to be tagged with the struct keyword unless it's been typedef ed. 在C中,结构类型必须用struct关键字标记,除非它是typedef ed。 So you have things like struct stat and the stat function, both declared in <sys/stat.h> . 所以你有像struct statstat函数这样的东西,都在<sys/stat.h> In C there's no problem with this, because when you type stat alone, it means the function, and when you type struct stat , it means the type; 在C中没有问题,因为当你单独输入stat时,它表示函数,当你输入struct stat ,它表示类型; there's no collision. 没有碰撞。

But what's supposed to happen when you #include <sys/stat.h> in C++ then? 但是当你在C ++中使用#include <sys/stat.h>时会发生什么呢? It's going to try to declare both the struct and the function in the global scope, with the same name, and in C++ you don't need the keyword struct when referring to a type. 它将尝试在全局范围内声明结构和函数,具有相同的名称,而在C ++中,在引用类型时不需要关键字struct C++ must allow this "collision", otherwise it simply won't be possible to include such a header a C++ program. C ++必须允许这种“冲突”,否则就不可能将这样的头部包含在C ++程序中。 Furthermore, C++ must have the rule that when a function and a struct type declared in the same scope have the same name, that name alone means the function, whereas preceding it with struct names the type. 此外,C ++必须具有以下规则:当在同一范围内声明的函数和结构类型具有相同的名称时,该名称单独表示该函数,而在其之前使用struct名称表示该类型。 Only then will compatibility with C be preserved. 只有这样才能保持与C的兼容性。

For classes, it's presumably just because classes are supposed to be identical to structs except for default visibility of members and bases. 对于类,它可能只是因为除了成员和基础的默认可见性之外,类应该与结构相同。 It would unnecessarily complicate the language to introduce more differences. 它会不必要地使语言复杂化以引入更多差异。

There's no good reason to allow namespaces to have the same names as variables and functions in the same scope, since there are no namespaces in C. So it's not allowed. 没有充分的理由允许名称空间与同一作用域中的变量和函数具有相同的名称,因为C中没有名称空间。所以不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM