C++ Applications Use STRICT Type Checking

From RAD Studio
Jump to: navigation, search

Go Up to C++ Specifics Index


The default type checking scheme for new C++ applications is STRICT, as if you had set #define STRICT (this applies to 64-bit Windows as well as 32-bit Windows applications). Applications created prior to C++Builder 2007 still use the previous default, #define NO_STRICT. The VCL now mangles Windows handle parameters to match the C++ STRICT mangling scheme, and compiling in STRICT mode has the advantages of providing enhanced type-safety and matching the native mangling of the VCL.

Migrating Your Code from NO_STRICT

It is recommended that you upgrade applications that define NO_STRICT, so that they will compile in STRICT mode (the first step is removing the NO_STRICT conditional define, as described in this topic). However, the VCL still provides entry-points for applications that define NO_STRICT, and if your code does not compile in STRICT mode, you can continue to use NO_STRICT. Be aware, though, that this might change in the future.

How to Know Whether An Application Uses NO_STRICT

A quick way to find code projects that have NO_STRICT defined is to search for NO_STRICT in your *.cbproj files.

Note: C++Builder project files (.cbproj files) cannot be displayed or edited in the Code Editor, and should not be altered in a different editor, but you can search a .cbproj file using another editor or a search utility. For example, you can use GREP.

For specific projects, you can use the IDE to remove the NO_STRICT define, as described in the following steps.

Removing the NO_STRICT Conditional Define

  1. Open a project that has NO_STRICT defined.
  2. Select Project > Options > C++ (Shared Options) (this is the very top page in the Project Options dialog box).
  3. In the Build Configurations field at the top of the page, select the configuration that you want to use the STRICT conditional define (such as the Base configuration).

    Note: If the Conditional defines field contains "Inherit Value from <configuration_name>", you need to select the named parent configuration (indicated in <configuration_name>) in the Build Configuration field.

  4. In the Conditional defines field, highlight NO_STRICT and delete it.
  5. Click OK on the Conditional defines dialog box.
  6. Click OK on the Project Options dialog box.
  7. Rebuild your project and correct any errors that appear as the result of type checking, especially in calls to Windows procedures.

Adding the STRICT Define (Optional)

Removing the NO_STRICT conditional define is equivalent to setting the STRICT conditional because STRICT is the default. However, you can optionally add the STRICT conditional define by following these steps:

  1. Select Project > Options > C++ (Shared Options) (this is the very top page in the Project Options dialog box).
  2. In the Build Configurations field at the top of the page, select the configuration that you want to use the STRICT conditional define (such as the Base configuration).

    Note: If the Conditional defines field contains "Inherit Value from <configuration_name>", you need to select the named parent configuration (indicated in <configuration_name>) in the Build Configuration field.

  3. On the Conditional defines field, click the ellipsis button Ellipsis.
  4. On the Conditional Defines dialog box, enter STRICT in the text entry field, and click Add.
  5. Click OK on the Conditional defines dialog box.
  6. Click OK on the Project Options dialog box.
  7. Rebuild your project and correct any errors that appear as the result of type checking, especially in calls to Windows procedures.

Type Checking Problems Using Older Windows Types

Before Windows 3.1, Windows types, like HWND and HMENUS, were of type void *. This meant that a Windows procedure could be called with HWND or an HMENUS as a parameter, and Windows could not tell them apart. With the introduction of Windows 3.1, many of these types were changed to distinct types. Now Windows can determine if you are calling with the wrong type.

This change left a huge task for developers of legacy applications -- to change types of parameters used in these Windows procedure calls. The NO_STRICT conditional define alleviates this conversion. With NO_STRICT set, the VCL provides entry points to overloads of Windows procedures that accept the old parameter types and do the conversion.