For compiler-specific code, it’s common to see cpp directives such as:
#if defined (__GNUC__) && (__GNUC__ >= 4)
which is the preprocessor test I typically use – not exclusively for __GNUC__, but it’s a common example. Alternatively,
#if (__GNUC__ >= 4)
appears to satisfy the same requirements. Are there potential problems with the latter? Not only with gcc, but any standards-conforming preprocessor. Can the LHS be evaluated as a certain value, even if it’s not defined? Are there any pitfalls to the second approach that any language lawyers are aware of?
The preprocessor assumes undefined macros to have the value
0in comparisons, so your simplification is ok in this case. If you want to check against a lower version than 4 in gcc, you may get into trouble though since it would evaluate astruewith a<even if it’s not gcc.I think the reason for using both is also a question of understandability, if you check
it’s rather obvious you’re not already in a block with code that only is for GCC, while the simplification
does not make that obvious and can be read as a version check only when you already know it’s gcc.