Freigeben über


How to: Migrate to /clr:safe (C++/CLI)

Visual C++ can generate verifiable components by using /clr:safe, which causes the compiler to generate errors for each non-verifiable code construct.

Remarks

The following issues generate verifiability errors:

  • Native types. Even if it isn't used, the declaration of native classes, structures, pointers, or arrays will prevent compilation.

  • Global variables

  • Function calls into any unmanaged library, including common language runtime function calls

  • A verifiable function cannot contain a static_cast Operator for down-casting. The static_cast Operator can be used for casting between primitive types, but for down-casting, safe_cast (C++ Component Extensions) or a C-Style cast (which is implemented as a safe_cast (C++ Component Extensions)) must be used.

  • A verifiable function cannot contain a reinterpret_cast Operator (or any C-style cast equivalent).

  • A verifiable function cannot perform arithmetic on an interior_ptr (C++/CLI). It may only assign to it and dereference it.

  • A verifiable function can only throw or catch pointers to reference types, so value types must be boxed before throwing.

  • A verifiable function can only call verifiable functions (such that calls to the common language runtime are not allowed, include AtEntry/AtExit, and so global constructors are disallowed).

  • A verifiable class cannot use Explicit.

  • If building an EXE, a main function cannot declare any parameters, so GetCommandLineArgs must be used to retrieve command-line arguments.

  • Making a non-virtual call to a virtual function. For example:

    // not_verifiable.cpp
    // compile with: /clr
    ref struct A {
       virtual void Test() {}
    };
    
    ref struct B : A {};
    
    int main() {
       B^ b1 = gcnew B;
       b1->A::Test();   // Non-virtual call to virtual function
    }
    

Also, the following keywords cannot be used in verifiable code:

See Also

Reference

Pure and Verifiable Code (C++/CLI)