The information in this article applies to:
SUMMARYThe Microsoft compilers listed above, in accordance with restrictions defined by the Annotated Reference Manual (ARM) and later X3J16 working papers, will prevent type conversions that would potentially allow the modification of data qualified as type "const". An example of this is as follows:
If the code above is compiled using C/C++ version 7.0, the following
two errors will be generated:
If this same code is compiled using Visual C++ version 4.0, the following error will be generated: If this same code is compiled using Visual C++ version 5.0, the following error will be generated: The Microsoft C/C++ compiler is correctly generating the errors. According to the ANSI committee for C++, the compiler should produce an error. MORE INFORMATIONThere are eight legal conversions allowed by C++ (see ARM section 13.2 page 3), two of which are applicable to this example (if T represents a type):
Using the T symbol, the example given above (ppcI = &pI) could be
expressed as follows:
To demonstrate why this conversion is not legal, substitute for T in
the rules given earlier and show that the conversion "T** --> const
T**" cannot be derived from those two rules:
You might immediately assume that because "T const" is the same as
"const T", then "int** const" is the same as "const int**", but this
is not true. The "const" keyword modifies whatever falls directly to
the right of it. For example:
Hence, the first legal conversion (T --> T const) does not apply to
this example.Try the second rule, and let 'int*' be T, which gives you:
The type "int* const *" can be translated as "a constant pointer to a
pointer that points to an integer." The target is "a pointer to a
pointer that points to a constant integer." So the second legal
conversion is also not applicable to this example. With this, it is
apparent that there is no legal combination of rules that will allow
the conversion "T** --> const T**" to be made.
Additional query words: 8.00 8.00c 9.00
Keywords : kberrmsg kbCompiler kbCPPonly kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 |
Last Reviewed: July 6, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |