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:
Am I right that clang
's is correct rejecting my test program, and gcc
and Comeau
don't follow the standard?
Is there a reason why the standard specifies such an impractical behavior (disallows template class bases as associated namespaces)?
Is clang
wrong accepting my test program with the using NA::add_ref
directive on the grounds of 3.4.2/3
?
Should I report a bug? :)
P.S. I have read clang Language Compatibility FAQ and didn't find an answer there.
From n3337, which is basically C++11 with minor editorial changes, the 3.4.2/2 reads:
For each argument type T in the function call [...] The sets of namespaces and classes are determined in the following way: [...]
- If T is a class type (including unions), its associated classes are: the class itself; the class of which it is a member, if any; and its direct and indirect base classes. Its associated namespaces are the namespaces of which its associated classes are members. Furthermore, if T is a class template specialization, ...
And it then continues with basically the same quote you posted in the question. The important difference here is the furthermore, which means that the list that you quoted (and I omitted) is in addition to the already mentioned namespaces, and that includes the namespaces of which the base class is a member.
Gcc and comeau are right, and clang++ is wrong in rejecting the code.
< does not apply >
Clang++ is wrong in rejecting it without the using NA::add_ref
.
Yes, you should probably report a bug. It seems that it has already been reported and fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With