如何将单个函数引入当前命名空间

How do I bring a single function into the current namespace?

本文关键字:命名空间 函数 单个      更新时间:2023-10-16

假设我想在名称空间A中使用std::max函数。我该怎么做?

namespace A {
void fun()
{
  double x = std::max(5.0, 1.0); // I don't want to have to write the std::
}
void fun()
{
  using namespace std;
  double x = max(5.0, 1.0); // I don't want to have to use the using directive to introduce the entire namespace
}
}

有办法做到这一点吗?

您可以通过在using声明中命名来"导入"单个符号:

namespace A
{
    using std::max;

这意味着A::max被定义并指定与std::max相同的功能;因此尝试在CCD_ 7中查找CCD_。

(这是Brandon对原始帖子评论的答案版本)