In discussions on Stack Overflow, concerns have emerged regarding Microsoft Visual C (MSVC )'s incorrect implementation of two-phase template instantiation. This process involves an initial syntax check followed by a full function/class body check.
As it turns out, MSVC performs only a basic syntax check on template entities. It does not fully verify that referenced names have been at least declared, resulting in potential errors being missed.
Beyond incorrect syntax checking, the two-phase lookup process itself faces issues in MSVC . During the first phase, dependent expressions such as function or member calls are not fully resolved. Instead, MSVC defers resolution to the second phase.
However, the issue with this approach becomes apparent in the second phase. While the standard mandates that only declarations added between the definition and instantiation points can be introduced during the second lookup phase, MSVC does not adhere to this restriction. It extends ordinary unqualified lookup to include declarations that were visible during the first phase, leading to potential binding errors and incorrect execution behavior.
One illustrative example that highlights these errors occurs when attempting to resolve the foo(0) expression within a template class S. While a standard-compliant compiler would resolve this call during the first phase and bind it to foo(void*), MSVC defers resolution and attempts to bind it to foo(int), causing an error.
This incorrect implementation affects code functionality and developer experience. It can lead to unexpected errors, which can be difficult to diagnose and resolve. Additionally, it can hinder the portability of code to other compilers and platforms.
The issue of broken two-phase template instantiation in Microsoft Visual C is a significant concern that has been acknowledged by developers. It is important for developers using MSVC to be aware of these problems and take appropriate precautions to avoid unexpected errors.
The above is the detailed content of Does Microsoft Visual C Correctly Implement Two-Phase Template Instantiation?. For more information, please follow other related articles on the PHP Chinese website!