[pcre-dev] [Bug 1918] #define ⇒ enum?

Top Page
Delete this message
Author: admin
Date:  
To: pcre-dev
Subject: [pcre-dev] [Bug 1918] #define ⇒ enum?
https://bugs.exim.org/show_bug.cgi?id=1918

Giuseppe D'Angelo <dangelog@???> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dangelog@???


--- Comment #4 from Giuseppe D'Angelo <dangelog@???> ---
The advantage at using enums rather than #defines is to make it a compile-time
error (warning?) if you pass an invalid value for a given API call.

To elaborate: right now we have

       int pcre2_config(uint32_t what, void *where);


and a bunch of #defines for symbolic constants you can pass to "what".

The problem is that this is has loose typing, so you can happily pass *any*
integer, in particular integers obtained by using the wrong #define.

/* PCRE2_CASELESS is for pcre2_compile, not for pcre2_config! */
pcre2_config(PCRE2_CASELESS, ...);

What's worse, this may or may not result in a runtime error, depending on
whether you accidentally passed a valid value (albeit obtained through the
wrong define, which should still be reported to the programmer as a potential
problem!).

The right way to do this is using enums:

    enum pcre2_config_what { pcre2_config_this, pcre2_config_that };
    int pcre2_config(enum pcre2_config_what, void *where);
    /* now I must pass exactly one of the enumerators or the code doesn't
compile */


Of course it's too late to do this now. This is an API and ABI break. Maybe for
PCRE3? :P

Last, but not least: unfortunately C has not a type-safe solution for bitmasks.
(C++ has.)

--
You are receiving this mail because:
You are on the CC list for the bug.