Question

When I call a function declared in a namespace from outside this namespace, I usually need to explicitly prefix with the namespace:

namespace ns1 {
    void myfunc();
}

myfunc();        // compiler complains
ns1::myfunc();   // good

However I have this situation where it seems the compiler can automatically devise what function I want to use:

namespace mymath {
    struct Vector3 {
        float x, y, z;
    };
    Vector3 vector_cross(const Vector3& lhs, const Vector3 &rhs)
    {
        return lhs; // Pretend this is a cross product
    }
} // ns

int main(int argc, char** argv)
{
    mymath::Vector3 v, w, x;
    x = vector_cross(v, w);   // <---- Here, I do not need to
                              //       prefix with the namespace
    return 0;
}

Q1: Is it because the compiler can automatically select the appropriate function based on the arguments type? Or something else?


After more testing, I found out that my compiler does not complain if I add another function with the same declaration in another namespace:

namespace mymath {
    // same as above
} // ns

namespace math2 {
    mymath::Vector3 vector_cross(const mymath::Vector3& lhs, const mymath::Vector3 &rhs)
    {
        return rhs; // Return rhs this time
    }
} // ns

int main(int argc, char** argv)
{
    mymath::Vector3 v, w, x;
    x = vector_cross(v, w);   // <---- Here, which one did the compiler chose?
    return 0;
}

Q2: How can I de-activate this behavior?

Edit: My compiler: gcc version 4.7.2 (Debian 4.7.2-5)

Was it helpful?

Solution

Q1: 'appropriate' is debated, but essentially yes, it selects function combining all kinds of "associated" namespaces, that works fine for many samples.

Q2: You make a few dozen people join WG21 and finally vote one of the 'fix ADL' proposals to life, this far the attempts fell flat.

Q2, pragmatic answer: ADL only applies to unqualified lookup, so you always have the option to use qualified names. (prefix :: std:: and so on...). Or stop using namespaces completely. Other than that you are subject to "Koenig uncertainty principle".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top