为什么 ANSI C 没有命名空间? [英] Why doesn't ANSI C have namespaces?

查看:20
本文介绍了为什么 ANSI C 没有命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大多数语言来说,拥有命名空间似乎是轻而易举的事.但据我所知,ANSI C 不支持它.为什么不?是否有计划将其包含在未来的标准中?

Having namespaces seems like no-brainer for most languages. But as far as I can tell, ANSI C doesn't support it. Why not? Any plans to include it in a future standard?

推荐答案

C 确实有命名空间.一种用于结构标签,一种用于其他类型.考虑以下定义:

C does have namespaces. One for structure tags, and one for other types. Consider the following definition:

struct foo
{
    int a;
};

typedef struct bar
{
    int a;
} foo;

第一个有tag foo,后一个用typedef做成类型foo.仍然没有发生名称冲突.这是因为结构标签和类型(内置类型和 typedef 的类型)存在于不同的命名空间中.

The first one has tag foo, and the later is made into type foo with a typedef. Still no name-clashing happens. This is because structure tags and types (built-in types and typedef'ed types) live in separate namespaces.

C 不允许随意创建 new 命名空间.C 在被认为在语言中很重要之前已经标准化,并且添加名称空间也会威胁向后兼容性,因为它需要名称修改才能正常工作.我认为这可以归因于技术问题,而不是哲学.

What C doesn't allow is to create new namespace by will. C was standardized before this was deemed important in a language, and adding namespaces would also threaten backwards-compatibility, because it requires name mangling to work right. I think this can be attributed due to technicalities, not philosophy.

JeremyP 幸运地纠正了我并提到了我错过的命名空间.标签和结构/联合成员也有命名空间.

JeremyP fortunately corrected me and mentioned the namespaces I missed. There are namespaces for labels and for struct/union members as well.

这篇关于为什么 ANSI C 没有命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆