Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm working with the CMCHTPC MediaFoundation headers, and they seem to work, but they give compiler warnings at several points that I'd like to get rid of. The culprit in most cases seems to be with enums such as the following:

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)
    );

No matter how that last value is defined, as DWORD(-1) or DWORD($FFFFFFFF) or simply as $FFFFFFFF, the compiler always replies:

W1012 Constant expression violates subrange bounds

Is there any way to fix this definition without specifically suppressing that warning in the compiler? (Note that {$R-} does not work; this turns off range checking at runtime, but not at compile time.)

like image 615
Mason Wheeler Avatar asked Jan 12 '16 19:01

Mason Wheeler


1 Answers

You can declare it just as -1 and give a {$Z4} to tell the compiler to use 4 bytes for this enum:

{$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 may as well work without the compiler directive. The docs say:

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

An Integer of value -1 is internally represented as 4 bytes. So either way should work.

Update: I rechecked and the compiler directive is indeed necessary (unless you set the similar option in the project options).

like image 72
Uwe Raabe Avatar answered Oct 21 '22 22:10

Uwe Raabe