Search code examples
c++indentationcode-formatting

How do you indent preprocessor statements?


When there are many preprocessor statements and many #ifdef cascades, it's hard to get an overview since normally they are not indented. e.g.

#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif

When I consider also indenting those preprocessor statements, I fear of getting confused with the general indentation level. So how do you solve this in a beautiful way?


Solution

  • Just because preprocessing directives are "normally" not indented is not a good reason not to indent them:

    #ifdef __WIN32__
        #include <pansen_win32>
    #else
        #include <..>
        #ifdef SOMEOTHER
            stmts
        #endif
        maybe stmts
    #endif
    

    If you frequently have multiple levels of nesting of preprocessing directives, you should rework them to make them simpler.