头文件中的匿名名称空间 [英] Anonymous namespace in header files

查看:374
本文介绍了头文件中的匿名名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道人们的意见/最佳做法是,您认为在头文件中包含匿名名称空间是好事还是坏事.

感谢您的输入.

Hi,

I was wondering on peoples opinion''s / best practices whether you think it is a good or bad thing to have a anonymous namespace in a header file.

Thanks for your input.

推荐答案

我要说的是,头文件中的匿名名称空间通常是邪恶的.

实际上,未命名的命名空间将为每个源文件生成一个具有唯一编译器生成名称的命名空间.

因此,每个包含该标头的源文件都会看到一个不同的定义,这可能不是您想要的.

通常,如果要避免符号对其他源文件可见,请仅在源文件中使用它们.
I would say that anonymous namespace in header file are generally evil.

In practice an unnammed namespace would generate a namespace with a unique compiler generated name for each source file.

Thus each source file that would include that header would see a different definition which is probably not what you intend.

As a general use, uses them only in source file if you want to avoid symbols to be visible to other source files.


这取决于应用程序.像C ++中的许多事物一样,匿名名称空间如果使用得当,则是好工具,如果使用得不好,则是不好的工具.您为什么决定使用匿名名称空间?您要隐藏内容吗?据我记得,匿名名称空间的要点是无法发现,因此无法使用.
It depends on the application. Anonymous namespaces, like many things in C++, are good tools if used well, and bad if used badly. Why would you decide to use an anonymous namespace ? Do you want to hide the contents ? The point of an anonymous namespace is that it can''t be discovered and therefore cannot be used, as far as I recall.


这篇关于头文件中的匿名名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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