Odd Macro Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael B Allen

    #1

    Odd Macro Problem

    I have a macro that can be used like:

    E(errno);

    but for reasons of brevity I frequently use the form:

    E(errno = EINVAL);

    But I would like to be able to have two definitions such that it evaluates
    to an active form like:

    printf("%d\n", errno);
    OR
    printf("%d\n", errno = EINVAL);

    and an inactive form that does nothing. The problem is, if I define the
    macro as simply:

    #ifdef INACTIVATE
    #define E(val)
    #else
    #define E(val) printf("%d\n", (val))
    #endif

    then the inactive form will not emit the 'errno = EINVAL' expression
    and set the value. If I define the inactive macro as:

    #define E(val) (val)

    that can emit a useless expression like:

    errno;

    Is there a way to write this macro to solve this problem and still
    satisfy all of the other requirements?

    Thanks,
    Mike
  • Eric Sosman

    #2
    Re: Odd Macro Problem

    Michael B Allen wrote:[color=blue]
    > I have a macro that can be used like:
    >
    > E(errno);
    >
    > but for reasons of brevity I frequently use the form:
    >
    > E(errno = EINVAL);
    >
    > But I would like to be able to have two definitions such that it evaluates
    > to an active form like:
    >
    > printf("%d\n", errno);
    > OR
    > printf("%d\n", errno = EINVAL);
    >
    > and an inactive form that does nothing. The problem is, if I define the
    > macro as simply:
    >
    > #ifdef INACTIVATE
    > #define E(val)
    > #else
    > #define E(val) printf("%d\n", (val))
    > #endif
    >
    > then the inactive form will not emit the 'errno = EINVAL' expression
    > and set the value. If I define the inactive macro as:
    >
    > #define E(val) (val)
    >
    > that can emit a useless expression like:
    >
    > errno;
    >
    > Is there a way to write this macro to solve this problem and still
    > satisfy all of the other requirements?[/color]

    No, because "all the other requirements" leaves too much
    to the imagination ... Nonetheless, I'll don my Carnak The
    Magnificent turban, dust off the Magic Eight-Ball, oil the
    Ouija board, cast the yarrow stalks, burn a chicken feather,
    and intone:

    YE MAY BE WELL-PLEASED BY ONE FROM YE OLDE LISTE:

    #define E(val) (val)
    /* "Useless expression," pfui. Damn the torpedoes! */

    #define E(val) ((void)(val))
    /* Still useless, but perhaps less noisy. */

    #define E(val) report_error(va l)
    /* Let the function decide what to do, if anything. */

    AN NONE OF THESE PLEASE YE, ABANDON THY LOVE OF "BREVITY"
    AND ESCHEW YON SIDE-EFFECTS IN YR MACRO ARGUMENTS. TREMBLE,
    MORTAL, FOR THOU HAST BEEN WARNED!

    (Alarums and excursions. Blinding lightning and
    cataclysmic thunder. When sight returns, the Oracle has
    vanished, leaving behind only a whiff of brimstone and one
    small demon buzzing around in hope of finding Cyrano.)

    --
    Eric Sosman
    esosman@acm-dot-org.invalid

    Comment

    Working...