Argument-dependent lookup through base of a template class

Viewed 735

I have a template class NB::B<T> derived from a non-template class NA::A in a namespace. act<T> is a template function calling add_ref function on an instance of its template argument. Specifically, act<NB::B<int>> wants to find add_ref defined in the namespace of NB::B's base using ADL. The complete example is following:

template<class T>
void act() {
  T* p = 0;
  add_ref(p); // the failing line
}

namespace NA
{
  struct A { };

  // I want ADL to find this:
  void add_ref(A* p) {
  }
}

namespace NB
{
  // template class with non-template base
  template <class T>
  struct B: NA::A { };

  typedef B<int> Bi;

  // using NA::add_ref; // fixes the problem
}

int main()
{
  act<NB::Bi>();
}

This compiles okay in gcc (4.7.0). And in Comeau online. However clang (3.1) fails:

a.cpp:4:3: error: use of undeclared identifier 'add_ref'

At the same time, the standard reads:

3.4.2/2 …

— If T is a template-id, its associated namespaces and classes are the namespace in which the template is defined; for member templates, the member template’s class; the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters); the namespaces in which any template template arguments are defined; and the classes in which any member templates used as template template arguments are defined.

Surprisingly, template's bases aren't listed as paths to associated namespaces. Thus clang's behavior appears to be correct. And Comeau and gcc are accepting incorrect program.

At the same time, 3.4.2/3 states that using's in argument's namespaces have no effect:

When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (3.4.3.2) except that:

— Any using-directives in the associated namespace are ignored.

But when I uncomment the using NA::add_ref line clang is happy to compile the test.

To put my example into practical perspective, you can think that act was a method of boost::intrusive_ptr, add_ref(A*) was intrusive_ptr_add_ref(CBase*) and B was some template, deriving from base CBase.

Regarding this I have several questions:

  1. Am I right that clang's is correct rejecting my test program, and gcc and Comeau don't follow the standard?

  2. Is there a reason why the standard specifies such an impractical behavior (disallows template class bases as associated namespaces)?

  3. Is clang wrong accepting my test program with the using NA::add_ref directive on the grounds of 3.4.2/3?

  4. Should I report a bug? :)

P.S. I have read clang Language Compatibility FAQ and didn't find an answer there.

1 Answers
Related