Home > Backend Development > C++ > Why Does Argument-Dependent Lookup (ADL) Fail to Find Function Templates with Explicit Template Arguments?

Why Does Argument-Dependent Lookup (ADL) Fail to Find Function Templates with Explicit Template Arguments?

Susan Sarandon
Release: 2024-12-11 12:42:12
Original
448 people have browsed it

Why Does Argument-Dependent Lookup (ADL) Fail to Find Function Templates with Explicit Template Arguments?

ADL and Function Templates

Argument-dependent lookup (ADL) plays a significant role in C by facilitating the automatic lookup of functions and objects in associated namespaces based on the types of arguments used. However, this mechanism has limitations when it comes to function templates.

Restriction on Finding Function Templates with ADL

The last call in the provided code snippet fails to compile due to a restriction in the C specification. According to Section 14.8.1.6 of the C Standard 03:

"But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call."

This means that ADL can only find function templates that are visible within the scope of the call. In the given example, frob<0> is a call to a function template with explicit template arguments. However, there is no visible declaration of the frob template at the point of the call in main.

Distinction from Simple Function Names

The restriction does not apply to simple function names. For such names, ADL is used even if the function is not visible within the scope of the call. This is because the call still maintains the syntactic form of a function call.

Example

The following example illustrates the behavior further:

namespace A {
  struct B { };
  template<int X> void f(B);
}
namespace C {
  template<class T> 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
}
Copy after login

In this example, the call to f<3>(b) is ill-formed because no visible function template named f exists at that point. The call to A::f<3>(b) is well-formed because the f template is visible within the A namespace. The call to C::f<3>(b) is ill-formed because ADL applies only to unqualified names. Using the using directive to alias C::f makes the call to f<3>(b) well-formed because the f name is now visible at the point of the call.

The above is the detailed content of Why Does Argument-Dependent Lookup (ADL) Fail to Find Function Templates with Explicit Template Arguments?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template