Search code examples
c++argument-dependent-lookup

Doesn't ADL considers namespaces associated with template argument?


Consider the simple code :

template<int N> struct foo{};

namespace N
{
    const int a=1;
    void bar(foo<1>& x){}
}

int main()
{
    bar(foo<N::a>());
    return 0;
}

Shouldn't the code work? why it isn't working I wonder. Thanks


Solution

  • [basic.lookup.argdep]/2:

    [ Note: Non-type template arguments do not contribute to the set of associated namespaces.—end note ]

    For the code linked in the comments, a typedef in a namespace isn't sufficient either, but for other reasons. The lookup is based on the resolved type, not on the namespace containing the typedef itself (or, equivalently, using).

    For example, if you had code like:

    namespace A { 
        class T {};
    }
    
    namespace B { 
        typedef A::T TT;
    }
    

    Using B::TT as an argument would add namespace A to the lookup, but would not add namespace B to the lookup.