Search code examples
c++namespacesmethod-hiding

C++ namespace name hiding


Suppose this piece of code:

using namespace std;
namespace abc {
    void sqrt(SomeType x) {}

    float x = 1;
    float y1 = sqrt(x); // 1) does not compile since std::sqrt() is hidden
    float y2 = ::sqrt(x); // 2) compiles bud it is necessary to add ::
}

Is there a way how to call std::sqrt inside abc namespace without ::? In my project, I was originally not using namespaces and so all overloaded functions were visible. If I introduce namespace abc it means that I have to manually check all functions that are hidden by my overload and add ::

What is the correct way to handle this problem?


Solution

  • I tried this and it works fine:

    namespace abc {
        void sqrt(SomeType x) {}
        using std::sqrt;
    
        float x = 1;
        float y1 = sqrt(x);
        float y2 = sqrt(x);
    }