Dav*_*man 50 c++ namespaces initialization header
今天有人断言你绝不应该在头文件中使用匿名命名空间.通常这是正确的,但我似乎记得有人告诉我,其中一个标准库在头文件中使用匿名命名空间来执行某种初始化.
我记得没错吗?有人可以填写详细信息吗?
Jam*_*kin 26
标头中无名称空间可能有用的唯一情况是,您只想将代码分发为头文件.例如,Boost的一个大型独立子集纯粹是标题.
ignore另一个答案中提到的元组令牌是一个例子_1,_2等等绑定占位符是其他的.
Joh*_*itb 15
我没有看到将匿名命名空间放入头文件中的任何意义.我已经使用了标准和libstdc ++标题,发现标题中没有一个匿名名称空间tuple(C++ 1x内容):
// A class (and instance) which can be used in 'tie' when an element
// of a tuple is not required
struct _Swallow_assign
{
template<class _Tp>
_Swallow_assign&
operator=(const _Tp&)
{ return *this; }
};
// TODO: Put this in some kind of shared file.
namespace
{
_Swallow_assign ignore;
}; // anonymous namespace
Run Code Online (Sandbox Code Playgroud)
这是你可以做到的
std::tie(a, std::ignore, b) = some_tuple;
Run Code Online (Sandbox Code Playgroud)
some_tuple的元素在左侧分配变量(参见此处),此迭代器使用了类似的技术.第二个元素被忽略.
但正如他们所说,应将其放入.cpp文件中,并且所有用户都应共享一个实例.他们会像这样在头文件中声明它:
extern _Swallow_assign ignore;
Run Code Online (Sandbox Code Playgroud)
我已经看到它曾经为不同翻译单元中的变量提供默认值.但在名称冲突的情况下,它可能会导致意外行为.
例
a.hpp
namespace
{
const char name[] = "default";
}
// This macro will hide the anonymous variable "name"
#define SET_NAME(newname) \
static const char name[] = newname;
Run Code Online (Sandbox Code Playgroud)
b.cpp
#include "a.hpp"
SET_NAME("file b") // name is "file b" in this translation unit
Run Code Online (Sandbox Code Playgroud)
c.cpp
#include "a.hpp"
SET_NAME("file c") // name is "file c" in this translation unit
Run Code Online (Sandbox Code Playgroud)
d.cpp
#include "a.hpp"
// name is "default" in this translation unit
Run Code Online (Sandbox Code Playgroud)
e.cpp
#include "a.hpp"
static const char name[] = "unintended";
// accidently hiding anonymous name
Run Code Online (Sandbox Code Playgroud)