Saturday, October 30, 2010

Argument Dependent Lookup (ADL)

Given the following program:

   #include <iostream>
   using namespace std;
   namespace A
   {
      struct A
      {
         int i;
      };
      void funcA(A a)
      {
         cout << "A::funcA" << endl;
      }
   }
   namespace B
   {
      void funcA(A::A a)
      {
         cout << "B::funcA" << endl;
      }
   }
   namespace C
   {
      void funcC()
      {
         A::A a;
         funcA(a);
      }
   }
   int main()
   {
      C::funcC();
      return 0;
   }


The following will happen:

A::funcA(A a) will be called. This is an example of Argument Dependent Lookup (ADL) also known as Koenig Lookup. If the compiler does not find funcA() in the current scope, it will look in the namespace where funcA()'s formal parameter was declared.

Reference: http://en.wikipedia.org/wiki/Koenig_Lookup

No comments:

Post a Comment