Best practice for including from include files

后端 未结 6 1175
谎友^
谎友^ 2021-01-21 02:05

I was wondering if there is some pro and contra having include statements directly in the include files as opposed to have them in the source file.

Personally I like to

6条回答
  •  孤独总比滥情好
    2021-01-21 02:20

    The language makes no requirements, but the almost universally accepted coding rule is that all headers must be self sufficient; a source file which consists of a single statement including the include should compile without errors. The usual way of verifying this is for the implementation file to include its header before anything else.

    And the compiler only has to read each include once. If it can determine with certainty that it has already read the file, and on reading it, it detects the include guard pattern, it has no need to reread the file; it just checks if the controling preprocessor token is (still) defined. (There are configurations where it is impossible for the compiler to detect whether the included file is the same as an earlier included file. In which case, it does have to read the file again, and reparse it. Such cases are fairly rare, however.)

提交回复
热议问题