在头文件中使用匿名命名空间 [英] Uses for anonymous namespaces in header files
问题描述
今天有人声称,你不应该在头文件中使用匿名命名空间。通常这是正确的,但我似乎记得一旦有人告诉我,其中一个标准库使用头文件中的匿名命名空间来执行某种初始化。
Someone asserted on SO today that you should never use anonymous namespaces in header files. Normally this is correct, but I seem to remember once someone told me that one of the standard libraries uses anonymous namespaces in header files to perform some sort of initialization.
记得正确吗?有人可以填写详细信息吗?
Am I remembering correctly? Can someone fill in the details?
推荐答案
标题中的无名命名空间很有用的唯一情况是,仅将代码分发为头文件。例如,Boost的一个大的独立子集是纯粹的头。
The only situation in which a nameless namespace in header can be useful is when you want to distribute code as header files only. For example, a large standalone subset of Boost is purely headers.
对于元组,令牌忽略
answer是一个例子, _1
, _2
等绑定占位符是其他。
The token ignore
for tuples, mentioned in another answer is one example, the _1
, _2
etc. bind placeholders are others.
这篇关于在头文件中使用匿名命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!