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&) {} void non_template(foo const&) {} } int main() { ns::foo f; non_template(f); // This is fine. frob<0>(f); // This is not. }
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!