Better way to un-define a set of defines based on version numbers in C++? In a header I have something like:

#define OPENGL_TARGET_VERSION_MAJOR 4
#define OPENGL_TARGET_VERSION_MINOR 0

#define OPENGL_TARGET_VERSION ( OPENGL_TARGET_VERSION_MAJOR * 10 \
                              + OPENGL_TARGET_VERSION_MINOR * 1 )


And then a lot of checks like:

#if OPENGL_TARGET_VERSION &lt; 44
    #ifdef GL_VERSION_4_4
        #undef GL_VERSION_4_4
    #endif
#endif

#if OPENGL_TARGET_VERSION &lt; 43
    #ifdef GL_VERSION_4_3
        #undef GL_VERSION_4_3
    #endif
#endif

#if OPENGL_TARGET_VERSION &lt; 42
    #ifdef GL_VERSION_4_2
        #undef GL_VERSION_4_2
    #endif
#endif

#if OPENGL_TARGET_VERSION &lt; 41
    #ifdef GL_VERSION_4_1
        #undef GL_VERSION_4_1
    #endif
#endif


...

Is there any way I could avoid repeating these checks for every version-number?
While it's somewhat uncertain whether you'll like it (some compilers warn about doing so) it's entirely permissible to #undef a value that wasn't defined to start with, so you can consolidate these down to something like:

#if OPENGL_TARGET_VERSION &lt; 44
    #undef GL_VERSION_4_4
#endif

#if OPENGL_TARGET_VERSION &lt; 43
    #undef GL_VERSION_4_3
#endif

#if OPENGL_TARGET_VERSION &lt; 42
    #undef GL_VERSION_4_2
#endif

#if OPENGL_TARGET_VERSION &lt; 41
    #undef GL_VERSION_4_1
#endif


While this does (or at least sort of might) answer the question you asked, from a code review standpoint, I'd probably prefer the code as it's written in the question. It's longer, but:


it's almost certainly hidden in a header almost nobody really reads through very often
it's simple repetitions of a simple pattern, so reading it isn't much work,
eliminating the #ifdef will give warnings with some compilers

While I think that's a pretty stupid warning, eliminating the warning is still worth more than reducing the line count.