Skip to content

Wrong evaluation of 0+number #7

Open
@r-lyeh

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

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