Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
coding c++ enum type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-02-24 (21:33)
From: Nathaniel Gray <n8gray@g...>
Subject: Re: [Caml-list] coding c++ enum type
On 2/24/07, micha <> wrote:
> when interfacing to c, what is the preferred method to represent enums
> which are used as flags in c?
> I can choose between:
> 1. using a variant type and  a list of those variants to represent the
> or-ed flags. Then I have to iterate over the list to calculate the
> combined flag value.
> 2. I can export global variables initialized with the real value of the
> flags and a function which combines  (with "or") them together,
> Is one method better than the other?

The Unix library uses both techniques -- using a single int for file
permissions but a list of flags in various other cases.

Personally, I think that as long as the bitfield fits in 31 bits then
it's better to keep it as a simple int.  Bitfields allow you to do
some things very simply that would be more painful with a list of
constructors.  For example, testing if flags a, b, and c are set can
be done very quickly and easily with a bitfield, but becomes somewhat
laborious and/or slow with lists.  You can easily provide an
additional "list of variants" interface on the ML side if you think
some users would prefer that.


>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- -->