Organize includes

后端 未结 6 1125
臣服心动
臣服心动 2020-12-31 00:14
  • Is there some preferred way to organize ones include directives?
  • Is it better to include the files you need in the .cpp file instead of the
6条回答
  •  鱼传尺愫
    2020-12-31 01:00

    From the performance point of view:

    Changing any of the headers included from stdafx.h will trigger a new precompilation, so it depends on how "frozen" the code is. External libraries are typical candidates for stdafx.h inclusion, but you can certainly include your own libraries as well - it's a tradeoff based on how often you expect to change them.

    Also, with the Microsoft compiler you can put this at the top of each header file:

    #pragma once
    

    This allows the compiler to fully skip that file after the first occurrence, saving I/O operations. The traditional ifndef/define/endif pattern requires opening and parsing the file every time it's included, which of course takes some time. It can certainly accumulate and get noticeable!

    (Make sure to leave the traditional guards in there, for portability.)

提交回复
热议问题