Article ID: 116240
Article Last Modified on 7/5/2005
APPLIES TO
- Microsoft Visual C++ 1.0 Professional Edition
- Microsoft Visual C++ 1.5 Professional Edition
- Microsoft Visual C++ 1.0 Professional Edition
- Microsoft Visual C++ 2.0 Professional Edition
- Microsoft Visual C++ 4.0 Standard Edition
- Microsoft Visual C++ 4.1 Subscription
- Microsoft Visual C++ 4.2 Professional Edition
- Microsoft Visual C++ 5.0 Standard Edition
- Microsoft Visual C++ 6.0 Service Pack 5
This article was previously published under Q116240
SYMPTOMS
Initializing a "const" reference to an array with a "non-const" array causes incorrect compiler errors to be generated. Two such errors are C2607 and C2561.
CAUSE
The C++ compiler does not properly handle initialization of a "const T&", where T is an array. For example, you should be able to initialize a variable of type "const float (&ref)[3][2]" with a variable of type "float [3][2]", but the compiler does not handle this case. Furthermore, interactions between const declarations and typedefs of arrays do not work in all circumstances.
RESOLUTION
To work around the problem, either do not use the const specifier or add explicit casts to the appropriate const types.
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
This problem was corrected in Microsoft Visual C++ .NET.
MORE INFORMATION
The sample code below demonstrates the problem. Compiling it yields at least one of the following error messages:
To work around the problem, use each commented line in the program to replace the line immediately above it.
Note: Simply removing the const specifier from the getMtx function's return type eliminates the problem.
Sample Code
/* Compile options needed: none */ typedef int Matrix[3][2]; class Data { Matrix Mtx; const Matrix& getMtx(); // const int (&getMtx() const)[3][2]; }; inline const Matrix& Data::getMtx() // inline const int (&Data::getMtx() const)[3][2] { return Data::Mtx; // return (const int (&)[3][2])Data::Mtx[0][0]; }
Additional query words: kbVC400bug 8.00 8.00c 9.00
Keywords: kbbug kbfix kbnoupdate kbcpponly kbcompiler KB116240