Why can't the compiler find the foo function?
namespace X { struct A { }; template<typename T> void foo(X::A) { } } void bar(X::A x) { foo<int>(x); } int main() { bar(X::A{}); } Why can't the compiler find the foo function?
namespace X { struct A { }; template<typename T> void foo(X::A) { } } void bar(X::A x) { foo<int>(x); } int main() { bar(X::A{}); } Because the record foo<int> is the instantiation of the foo template from the current namespace. ADL in this case does not even reach.
17.9.1 Explicit template argument specification [temp.arg.explicit]
8 (Note: For a function of the function, it depends on the scope of the call. This is because the call is still a function call (6.4.1). But it’s not a problem. If there is no syntactically well-formed and argument-dependent lookup, it does not apply. It can be found in other namespaces. [Example:namespace A {
struct B {};
template void f (B);
}
namespace C {
template void f (T t);
}
void g (A :: B b) {
f <3> (b); // ill-formed: not a function call
A :: f <3> (b); // well-formed
C :: f <3> (b); // ill-formed; argument dependent lookup applies only to unqualified names
using C :: f;
f <3> (b); // well-formed because C :: f is visible; then A :: f is found by argument dependent lookup
}—End example] —end note]
Source: https://ru.stackoverflow.com/questions/914543/
All Articles
X::(although a non-sample function would have been called normally) - VTT