I'm trying to understand Argument Dependent Lookup. The classic example is:

namespace NS {
   class T { };
   void f(T);
}
NS::T parm;
int main() {
   f(parm);                    //  OK: calls  NS::f
}

I'm looking at this page and in particular this statement:

If T is a class type, its associated classes are the class itself and its direct and indirect base classes. Its associated namespaces are the namespaces in which its associated classes are defined.

And I'm trying to understand why this code does not compile:

class NS {
public:
   class T { 
   public:
       void f(T)
       {
           std::cout << "there" << std::endl;
       }
   };

   void f(T)
   {
       std::cout << "here" << std::endl;
   }
};

NS::T parm;

int main() {
   f(parm);                    //  OK: calls  NS::f
   return 0;
}

One of the two f's should be found but neither one is found. Can someone help me understand why?

Thank you for your help,
Perry

For what it's worth, and it may not be worth much, here's my thoughts.

In the top example NS is a namespace, like namespace std is a namespace, and T is a class, like any other class. The scope operator tells the linker/compiler to look in the NS namespace for class T and to create an object of that class called parm. I think you probably need to use the scope opertor to call f() as well, or else list a using statement of some kind. As it stands, the call to f() within main is not connected to anything, so I would expect a compiler error, or at the very least, a warning about f() being called within main() being ambiguous.


In the second code there is a class NS which contains class T (class T is nested in class NS so T is sometimes called a nested class). No namespace is created and cout, which is located in namespace std is called with the scope operator, except that the header file containing cout, iostream, is not included in the code, so I would expect an error or no compile. In addition within class T a fuction called f() is declared that takes as a parameter an object of the class T. But, how can f() take a T object if T isn't even declared yet? I suspect this leads to a circular argument which prevents the compilation. Also, within main() there is a function call to f(), but it's a freestanding function, not something within NS or T as it's not preceded by either the dot or the arrow operator. But there isn't a free standing f() function declared, so that's an error as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.