typecheck()

From vegard.wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

It is possible (although not straightforward) to typecheck an expression (and get warnings, etc.) without actually compiling the expression.

Definition:

#define typecheck(...) \
    do { \
        (void) __builtin_constant_p(__VA_ARGS__); \
    } while(0)

Usage:

void foo()
{
    int i = -1;
    unsigned int j = 1;

    // warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int'
    typecheck(i != j);
}

Note: this only works in gcc/clang.

See also