6

I have a question regarding the standard ADL resolution in C++.

Here is a sample code explaining my enquiry:

#include <string>

// The mechanism:
namespace A {

 template< class C >
 ::std::string scope(const C*)
 { return "A"; }

 namespace B {

  template< class C >
  ::std::string scope(const C *foo)
  { return A::scope(foo)+"::B"; }

 } // namespace B
} // namespace A

::std::string scope(...)
{ return ""; }

// The test classes
struct foo {};
namespace A {
 struct foo {};
 namespace B {
  struct foo {};
 }
}

// The usage
int main()
{
  foo *Foo=0;
  A::foo *FooA=0;
  A::B::foo *FooB=0;

  scope(Foo);  // OK, returns ""
  scope(FooA); // OK, returns "A"
  scope(FooB); // On one compiler, OK returns "A::B" ; On another, compiler error "Ambiguous call" between A::scope() and A::B::scope()
}

So, my question is what is the standard regarding ADL? Should all the functions from parent namespaces of the argument be found, or only the functions available in the (nested) namespace of the argument + the global functions?

This program has been tested on MSVC 2008 (and compiles with SP but not without...)

DocZeD
  • 63
  • 5
  • Your code wouldn't even compile on ANY compiler, as `A::scope()` which takes no parameter doesn't exist, yet you call this function. – Nawaz May 12 '11 at 09:20
  • Is one compiler old and the other new, or are they from different manufacturers? – Bo Persson May 12 '11 at 09:25
  • @Bo Persson -> Both compilers are the same, i.e. MSVC provided with Visual Studio 2008, but one with the last updates installed (working) and the other not (compilation error) – DocZeD May 12 '11 at 11:20

1 Answers1

6

According to the standard, ADL works (modulo a couple of special rules) "as if" the function name were preceded by the namespace; in your last line, lookup should precede as if you'd written A::B::scope. Which does not look in the surrounding namespaces.

Note that even within namespace A::B, there would be no ambiguity; in A::B, A::B::scope hides A::scope. Unqualified name lookup stops in the scope where it first finds the name.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Thanks James, this is exactly what I thought :) So to resume, my code should compile without any compiler error caused by ambiguity... again a problem of standard conforming from MSVC isn't it :/ – DocZeD May 12 '11 at 11:23
  • @DocZeD Just curious, but what version of VC? – James Kanze May 12 '11 at 15:18