Article ID: 315948
Article Last Modified on 4/11/2003
APPLIES TO
- Microsoft Visual C++ .NET 2002 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
This article was previously published under Q315948
SYMPTOMS
When you pass a reference to a global variable as a template argument, you may receive the following compiler error (or a similar one):
You receive this error message only if you compile without using precompiled headers.
CAUSE
Because of a known bug, the compiler cannot generate correct code for a construct. In this case, the compiler cannot resolve the global variable.
RESOLUTION
To work around this problem, declare the variable at global scope, either in a separate header file, or in the global scope of the file in which the file is referenced.
STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.
MORE INFORMATION
Steps to Reproduce the Behavior
The following sample code, when compiled, reproduces the problem:
//Repro.cpp template <int* i> void Test() { *i = 0; } //int i; //Uncomment this line to resolve this problem. int main() { extern int i;/*This would be okay if this was at global scope.*/ Test<&i>(); }
You can run the following command at the command prompt to get this error:
cl /TP repro.cpp
When you use the Microsoft Visual Studio .NET Integrated Development Environment (IDE) to build this code, make sure that you do not use precompiled headers. To set this compiler option in the Visual Studio .NET IDE, follow these steps:
- In the Solution Explorer pane of the project, right-click the project name, and then click Properties.
- In the left pane, click the C/C++ folder.
- Click the Precompiled Headers node.
- In the right pane, click Create/Use Precompiled Header, and then click Not Using Precompiled Headers.
Keywords: kbbug kbfix kbcompiler KB315948