Home > Backend Development > C++ > Why Doesn't Argument-Dependent Lookup (ADL) Find Function Templates Without Explicit Namespace Qualification?

Why Doesn't Argument-Dependent Lookup (ADL) Find Function Templates Without Explicit Namespace Qualification?

Mary-Kate Olsen
Release: 2024-12-10 08:28:13
Original
871 people have browsed it

Why Doesn't Argument-Dependent Lookup (ADL) Find Function Templates Without Explicit Namespace Qualification?

Why Function Templates Resist ADL Discovery

In C , argument-dependent lookup (ADL) empowers compilers to locate functions without explicit namespace qualification. However, when it comes to function templates, ADL faces a limitation.

The crux of this is captured in C Standard 03's 14.8.1.6:

"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."

Let's break it down with an example:

In the following snippet, we have a function template frob and a non-template function non_template within namespace ns:

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&amp;) {}
    void non_template(foo const&amp;) {}
}
Copy after login

While invoking non_template directly is permissible, attempting to call frob<0> on an object of foo fails to compile.

Why?

Because the call frob<0>(f) is syntactically invalid without the namespace qualification. Without an explicit namespace, the compiler cannot know which frob template to find during ADL.

To rectify this, one must explicitly qualify the call to frob or bring the namespace into scope using using. By doing so, the compiler can then employ ADL to locate the correct template definition.

The above is the detailed content of Why Doesn't Argument-Dependent Lookup (ADL) Find Function Templates Without Explicit Namespace Qualification?. 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