Home > Backend Development > C++ > Why Does Argument-Dependent Lookup (ADL) Fail for C Function Templates?

Why Does Argument-Dependent Lookup (ADL) Fail for C Function Templates?

Mary-Kate Olsen
Release: 2024-12-11 08:48:11
Original
729 people have browsed it

Why Does Argument-Dependent Lookup (ADL) Fail for C   Function Templates?

ADL Failure in Finding Function Templates

In C , argument-dependent lookup (ADL) enables the search for declarations of function names within the namespaces associated with the arguments in a function call. However, there is a restriction in the C specification when it comes to function templates and ADL.

The Restriction

According to C Standard 14.8.1.6, ADL does not apply to function templates unless there is a visible function template with the same name at the point of the call. This means that, for function templates with explicit template arguments, the call must have the correct syntactic form with a visible function template.

Example

Consider the following example:

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

int main() {
    ns::foo f;
    non_template(f); // This is fine.
    frob<0>(f); // This is not.
}
Copy after login

In this example, ADL finds and resolves non_template for the first call because non_template is an ordinary function. However, for the second call, frob is a function template. Since there is no visible frob function template at the point of the call, ADL fails, and the call is not syntactically well-formed.

Addressing the Restriction

To overcome this restriction, ensure that the function template is visible within the scope of the call. This can be achieved by declaring the function template in the current namespace or by qualifying the call with the namespace containing the function template.

The above is the detailed content of Why Does Argument-Dependent Lookup (ADL) Fail for C Function Templates?. 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