Skip to content

Commit bf74729

Browse files
author
Colin Robertson
committed
Add another example of /Zc:twoPhase- behavior
1 parent 243330e commit bf74729

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/build/reference/zc-twophase.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ As a result, if the template body has syntax errors and the template is never in
4848
4949
Another effect of this behavior is in overload resolution. Because of the way the token stream is expanded at the site of instantiation, symbols that were not visible at the template declaration may be visible at the point of instantiation and participate in overload resolution. This can lead to templates that change behavior based on code that was not visible when the template was defined, contrary to the standard.
5050
51+
For example, consider this code:
52+
53+
```cpp
54+
#include <cstdio>
55+
56+
void func(void*) { std::puts("The call resolves to void*") ;}
57+
58+
template<typename T> void g(T x)
59+
{
60+
func(0);
61+
}
62+
63+
void func(int) { std::puts("The call resolves to int"); }
64+
65+
int main()
66+
{
67+
g(3.14);
68+
}
69+
```
70+
71+
When compiled under **/Zc:twoPhase-**, this program prints "The call resolves to int". In conformance mode, this program prints "The call resolves to void*", because the second overload of `func` is not visible when the compiler encounters the template.
72+
5173
### Update your code for two-phase conformance
5274

5375
Older versions of the compiler do not require the keywords `template` and `typename` everywhere the C++ Standard requires them. These keywords are needed in some positions to disambiguate how compilers should parse a dependent name during the first phase of lookup. For example:

0 commit comments

Comments
 (0)