Is it possible to get a value of an enum member at compile time?

Just use if. Enums can be evaluated at compile time just fine. The compiler will optimize the impossible branches out:

if (FOO_LAST > 10) {
    // A
} else {
    // B
}

The compiler knows which of the two branches (A and B) cannot be reached, so it can eliminate the if completely.

Note however, that you should only use the enumerators directly. For example, in this:

int num = FOO_LAST;
if (num > 10) {
    // A
} else {
    // B
}

GCC will keep the if comparison.


#ifdef is interpreted by the preprocessor and not by the compiler. The pre-processor does not know anything about the enums's values. So this is not a way to go.