How to define a very large constant in an enumeration without warning?

I work with CMCHTPC MediaFoundation headers , and they seem to work, but they give compiler warnings at several points, which is how I get rid of. In most cases, the offender seems to have transfers, such as:

TMF_Plugin_Type = ( MF_Plugin_Type_MFT = 0, MF_Plugin_Type_MediaSource = 1, MF_Plugin_Type_MFT_MatchOutputType = 2, MF_Plugin_Type_Other = DWORD(-1) ); 

Regardless of whether this last value is defined as DWORD(-1) or DWORD($FFFFFFFF) or simply as $FFFFFFFF , the compiler always answers:

W1012 Constant expression violates subband boundaries

Is there a way to fix this definition without specifically warning this compiler? (Note that {$R-} does not work, this disables range checking at runtime, but not at compile time.)

+5
source share
3 answers

You can declare it as -1 and let {$ Z4} tell the compiler to use 4 bytes for this enumeration:

 {$Z4} type TMF_Plugin_Type = ( MF_Plugin_Type_MFT = 0, MF_Plugin_Type_MediaSource = 1, MF_Plugin_Type_MFT_MatchOutputType = 2, MF_Plugin_Type_Other = -1); 

It can also work without a compiler directive. The docs say:

To assign ordinality to a value, follow its identifier with = constantExpression, where constantExpression is a constant expression that evaluates to an integer.

An integer in the value -1 is internally represented as 4 bytes. Therefore, any method should work.

Update: I double-checked, and the compiler directive is really necessary (unless you set the same option in the project settings).

+8
source

You have two options, as far as I can tell.

  • Remove the DWORD cast and replace DWORD(-1) with -1 as described in Uwe's answer.
  • Suppress this compiler warning. You can do this with the code {$WARN BOUNDS_ERROR OFF} .

The disadvantage of option 1 is that you must find each such ad and modify it. This means making bulk changes to a third-party library. If you want to accept the new version of this library, you need to reapply these changes. For some libraries this may be problematic.

Therefore, in my opinion, suppressing warnings may well be a cleaner approach. You can put the suppression directive in the include file or at the head of the device. This means that the changes you make to the third-party code are narrower in scope.

0
source

I think you can see how TColor and TColorRec declared in System.UITypes and use the experience of Embarcadero declaring something like this:

  TMF_Plugin_Type = $0 .. $FFFFFFFF; TMF_Plugin_Type_Rec = record const MF_Plugin_Type_MFT = 0; MF_Plugin_Type_MediaSource = 1; MF_Plugin_Type_MFT_MatchOutputType = 2; MF_Plugin_Type_Other = TMF_Plugin_Type($FFFFFFFF); end; 
-2
source

Source: https://habr.com/ru/post/1240473/


All Articles