The use of double include guards in C++

前端 未结 5 2059
青春惊慌失措
青春惊慌失措 2020-12-08 01:36

So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double gua

5条回答
  •  情歌与酒
    2020-12-08 02:17

    The only benefit I can come up with is that it outright stops the linker from bothering to find the file.

    The linker will not be affected in any way.

    It could prevent the pre-processor from bothering to find the file, but if the guard is defined, that means that it has already found the file. I suspect that if the pre-process time is reduced at all, the effect would be quite minimal except in the most pathologically recursively included monstrosity.

    It has a downside that if the guard is ever changed (for example due to conflict with another guard), all the conditionals before the include directives must be changed in order for them to work. And if something else uses the previous guard, then the conditionals must be changed for the include directive itself to work correctly.

    P.S. __HEADER_A_HPP__ is a symbol that is reserved to the implementation, so it is not something that you may define. Use another name for the guard.

提交回复
热议问题