#include guard before or after comment block?
Asked Answered
F

1

7

I have somewhere read (sorry can't find the link anymore) that on the first line of a header should always be the #include guard, because compilers can see it without opening the header file. So if a header file has already been included, it won't open the file just to close it again and this speeds up the building process.
But I always have a comment block at the start of every file. So my question is, should the #include guard be written before the comment block or after?

Is this style better:

///////////////////////
// Name:        code.h
// Author:      Me
// Date:        dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////

#ifndef CODE_H_
#define CODE_H_

...

#endif

Or ist this style better:

#ifndef CODE_H_
#define CODE_H_

///////////////////////
// Name:        code.h
// Author:      Me
// Date:        dd.mm.yyyy
// Description: This code executes a specific task
///////////////////////

...

#endif

Or doesn't it matter at all?

Ferrel answered 23/11, 2017 at 9:44 Comment(5)
If you really want to safe some nanoseconds, you MUST WRITE IT AT THE FIRST POSSIBLE PLACE! ;) Such things are really important for the life of all software guys. If that is one of the most important problems you have, you have a very good position! BTW: The compiler must open the file anyway... The include gard is not represented elsewhere.Georgetown
@Georgetown I know that this isn't really a big thing, but I just got curious. :)Ferrel
Before. The fewer lines the preprocessor has to process, the better. I used to have a compiler (Watcom) that would print out direct and included line counts, and it was always in the ratio of at least 1:100.Warton
It's a pity you can't find a link, because otherwise I would tell you to ignore everything that source says from now on. That is, if it indeed says what you claim it says. It is likely that you misremember. Compilers cannot see include guards without opening the file. What they can do, and indeed do, is remember that a certain file has include guards so that they can avoid opening it again. It doesn't matter if the guard is the first thing in the file or follows some comments.Selftaught
Well in 'C++ Coding Standards' Sutter and Alexandrescu wrote 'Don’t put any code or comments before and after the guarded portion, and stick to the standard form as shown. Today’s preprocessors can detect include guards, but they might have limited intelligence and expect the guard code to appear exactly at the beginning and end of the header.' Should we now ignore all that Herb Sutter says? Just asking...Fideicommissary
L
6

From my experience and others'

is that it's NOT the parsing and reading of files that take the time, but the various optimisation and code-generation passes

So opening a file to parse the inclucde guard should be negligible for a large project's build time.

I mean it couldn't be the bottleneck of the procedure!

So choose either style you prefer, it's really opinion based.


Another interesting comment found here:

One upon a time, there might have been one or two compilers stupid enough to open the file each time to check the include guard. No compiler produced in this millennium would do that, as it can just keep a table of files and include guards and consult that before opening the file.

Read more in File-wide include-guards.

Livingstone answered 23/11, 2017 at 9:50 Comment(2)
The question is not to use include guards or not, it asks for before or after a comment block. So optimisation and code-generation passes are not relevant here.Georgetown
@Georgetown yes but my point is to show that parsing and reading the file is really done fast enough to not worry the OP. How would you propose to edit my question? Also my answer doesn't intend to answer the use or no use of guards. It answers the OP's question I believe. =)Livingstone

© 2022 - 2024 — McMap. All rights reserved.