How do you indent preprocessor statements?

前端 未结 5 1115
春和景丽
春和景丽 2021-02-03 23:21

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__
#         


        
5条回答
  •  情话喂你
    2021-02-04 00:21

    As you, I didn't make my mind yet about the best way to indent, but I found in more than one place this alternative indentation in which the # is placed always at the first column and just the keyword is indented:

    #ifdef __WIN32__
    #  include 
    #else
    #  include <..>
    #endif
    

    In Visual Studio, when you type # as the first character it always brings its indentation to the left, so it seems that MS either favors never indenting preprocessor statements, or using the above format.

    The big problem is when you have non-preprocessor and preprocessor statements mixed and indentation applied. It's hard to make code that looks good, no matter which option:

    option (a)

    for (...)
    {
      if (foo)
      {
        if (bar)
        {
    #ifdef __WIN32__
          c = GetTickCount();
    #else
          c = clock();
    #endif
        }
      }
    }
    

    option (b)

    for (...)
    {
      if (foo)
      {
        if (bar)
        {
          #ifdef __WIN32__
          c = GetTickCount();
          #else
          c = clock();
          #endif
        }
      }
    }
    

    option (c)

    for (...)
    {
      if (foo)
      {
        if (bar)
        {
    #     ifdef __WIN32__
            c = GetTickCount();
    #     else
            c = clock();
    #     endif
        }
      }
    }
    

    At this point, it becomes a question of personal taste, as so many other indentation styles.

提交回复
热议问题