Skip to content

Wrong evaluation of 0+number #7

Description

@r-lyeh

Hello @pfultz2 ,

Ty for the wiki doc. Very illustrative! :)

I found that this program fails in both gcc & clang. Works ok in VS.

// ref: https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
#define IIF_0(t, ...) __VA_ARGS__
#define IIF_1(t, ...) t

#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
#define COMPL_0 1
#define COMPL_1 0

#define EXPAND__( x ) x
#define CHECK_N(x, n, ...) n
#define CHECK(...) EXPAND__(CHECK_N(__VA_ARGS__, 0,))
#define PROBE(x)   x, 1,
#define NOT(x)  CHECK(EXPAND__(PRIMITIVE_CAT(NOT_, x)))
#define NOT_0   PROBE(~)
#define BOOL(x) COMPL(NOT(x))
#define IF(c)   IIF(BOOL(c))

#include <stdio.h>

int main() {
    IF(0+0)(puts("error"), puts("ok"));
    IF(0+1)(puts("ok"), puts("error"));
    IF(1+0)(puts("ok"), puts("error"));
    IF(1+1)(puts("ok"), puts("error"));
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions