Cook Computing

Koenig Lookup

July 16, 2002 Written by Charles Cook

Following a link on the private virtual function thread I mentioned yesterday I ended up reading several articles on the C/C++ Users Journal site. One article mentioned Koenig lookup, another C++ feature I'd never encountered before.

The gist of Koenig lookup is that when resolving an unqualified function name (i.e. without a namespace prefix), the namespace of each argument is added to the list of namespaces in which the function name is searched for. For example:


namespace Tst
{
  class TstStruct { };
  void Foo(TstStruct ts) { } 
};

void main(void)
{
  Tst::TstStruct ts;
  Foo(ts);
}

Foo in main can be resolved because the compiler adds the namespace containing TstStruct to the list of namespaces to be searched.

If you only use VC++ then Koenig lookup is a moot point. I compiled this code with gcc on linux after I discovered that VC++ does not support Koenig. So I don't feel so bad about being completely ignorant of it.

This article explains Koenig lookup in more detail.