How do you indent preprocessor statements?

前端 未结 5 1107
春和景丽
春和景丽 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:10

    First, make sure you actually need all the ifdef statements. Perhaps there's a way to refactor them to keep the number of nested if checks limited? Assuming you do need them:

    It would be a good idea to separate the first portion (the includes) into a separate include file:

    In your_header.h:

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

    Then in the implementation file, you can then keep things more sane. Liberal blank lines and comments are the approach I've typically taken regarding non-indented #ifdef checks.

    #ifndef __WIN32__
    #ifdef SOMEOTHER
    
    stmts
    
    // SOMEOTHER
    #endif
    
    maybe stmts
    
    // __WIN32__
    #endif
    

    All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable.

提交回复
热议问题