在实现文件中,我们应该更喜欢"using namespace"指令还是将实现包装在命名空间 { } 中?
In implementation files, should we prefer "using namespace" directives or wrapping the implementation in namespace { }?
我经常在 cpp 文件中看到写
成namespace foo
{
... // implementation of class methods etc, belonging to the namespace foo
} // namespace foo
其中实现由命名空间包装,以便不需要命名空间前缀,例如 foo::some_class::method((。
我也经常看到
using namespace foo;
... // implementation of class methods etc, belonging to the namespace foo
前者似乎暗示您将代码放在该命名空间中,而这并不是真正发生的事情。后者对我来说似乎更干净,更直接。这是纯粹的风格还是有理由不使用第二种形式?
更喜欢扭曲。对于类成员函数,几乎没有区别,因为当您编写:
void clazz::foo() {
// ...
}
然后clazz
将由常规的非限定查找查找,这将找到您使用 using 指令引入的类名。
但是,对于免费功能,差异是巨大的,这里
void foo() {
// ...
}
将引入一个新函数,在全局命名空间中声明和定义。它将与命名空间中声明的函数无关。
获得一致性的唯一方法(无需通过命名空间完全限定定义(是将定义包装在重新打开的命名空间中。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 为左值和右值的包装器实现C++范围
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 如何减少大量包装类的实现代码?
- 在实现文件中,我们应该更喜欢"using namespace"指令还是将实现包装在命名空间 { } 中?
- 使用可变参数模板函数在类周围实现基于 pImpl 的包装器
- 在C++中实现和包装函数组合以进行惰性计算
- 尝试实现 Windows API 包装器的事件处理程序时遇到问题
- C++适配器类来包装实际实现
- 用相同的数据c++包装/铸造类和结构,以实现外部api
- 线程包装器实现问题
- 包装 API 时,我是否应该避免在实现类中进行向上转换
- libPd 和 c++ 包装器实现
- 如何在C++中实现NodeJS JavaScript API的包装器
- 如何在c#中实现c++包装器?
- 是否有一种方法可以实现fscanf的包装函数?
- 基类中抽象方法的包装器实现
- 运算符重载 - 在 char 数组包装类中实现 C++ setter 的正确方法是什么