quant
quant

Reputation: 23082

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

Suppose I want to use the std::max function in my namespace A. How would I do this?

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
}

}

Is there a way of doing this?

Upvotes: 1

Views: 231

Answers (1)

M.M
M.M

Reputation: 141618

You can "import" individual symbols by naming them in a using declaration:

namespace A
{
    using std::max;

This means that A::max is defined and designates the same function as std::max; so attempts to look up max in namespace A will find the intended function.

(This is an Answer version of Brandon's comment on the original post)

Upvotes: 1

Related Questions