Compiler Warning (level 1) C4303
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Compiler Warning (level 1) C4303

Error Message

'cast' from 'type1' to 'type2' is deprecated, use static_cast, __try_cast or dynamic_cast

C-style type casting or function-style casting is not supported when using Managed Extensions for C++. To cast, use either dynamic_cast Operator or static_cast Operator.

C4303 is not reachable using /clr and the new syntax; see C-Style Casts with /clr for more information.

The following sample generates C4303:

// C4303.cpp
// compile with: /clr:oldSyntax /W1
__gc struct A { };
__gc struct B { };

int main() {
   B *b = new B;
   try {

   // C4303 old 'c' style cast
   A *a = (A*)b;   // C4303
   // run time exception becaused casting to incompatible types

   // try the following line instead
   // A *a = __try_cast<A*>(b);

   catch (System::InvalidCastException * e) {
© 2015 Microsoft