使用其他命名空间内命名空间中的函数
Using functions from namespace inside other namespace
有什么方法可以省略顶级命名空间中其他命名空间中某些函数的外部命名空间名称吗?
void sample_func();
namespace foo {
void first_func();
namespace bar {
void second_func();
void sample_func();
}
对first_func()
来说,一切都是微不足道的:只需键入using foo::first_func;
就可以调用它,就像fist_func();
如果我想在没有任何前缀的情况下调用second_func
,一切都很简单:只需using foo::bar::second_func;
允许将其称为second_func();
但是有没有办法称之为bar::second_func();
?它将提高代码的可读性 - 最好键入并查看类似 bar::sample_func
的内容,而不是没有名称混淆的完整foo::bar::sample_func
:显然using namespace foo::bar
在这种情况下不是一种选择。
UPD 我对导入整个foo
或bar
命名空间(即 using namespace ...
指令!我只需要他们的一些功能。
您可以使用
namespace bar = foo::bar;
将foo::bar
作为 bar
导入到当前命名空间中。
如果不
在命名空间中,则用namespace::
或::
作为前缀,即
::sample_func();
foo::first_func();
bar::second_func();
bar::sample_func();
您可以使用
using namespace foo;
在您希望仅使用 first_func()
和 bar::sample_func()
的任何声明性区域中。
例:
int main()
{
using namespace foo;
first_func();
bar::sample_func();
}
相关文章:
- 在命名空间中定义函数还是限定函数
- CUDA内核和数学函数的显式命名空间
- 在命名空间中使用全局命名空间中的函数
- 是否可以将函数导入命名空间,但不能导出它?
- 使用另一个命名空间中的函数C++
- Visual Studio 无法解决类和命名空间中重载的明确函数
- 命名空间更改函数定义
- C++命名空间中调用 void 函数的错误
- C++ 17 个友元函数声明和内联命名空间
- MFC 函数来自 :: 命名空间?
- 使用类指针重载C++命名空间函数模板专用化替代方法?
- 在头文件中添加命名空间函数
- 命名空间函数的多重定义
- 在多个 cpp 文件中重用未命名的命名空间函数
- 如何正确声明命名空间函数
- 使用 libclang 解析命名空间函数
- C++命名空间函数
- 内联命名空间函数时出错
- 递归std::functional与递归匿名命名空间函数.使用哪一个
- 如何在命名空间函数中使用pragma weak