Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any harm in putting comments first in C header file?

Is there any meaningful downside, on modern compilers, to putting comments starting at the beginning of a header file?

That is, something like the following in great_header.h:

/*
 * this file defines the secret to life
 * etc
 * (c) 2017 ascended being
 */

#pragma once
#ifndef NAMESPACE_GREAT_HEADER_H_
#define NAMESPACE_GREAT_HEADER_H_

... (actual contents)

#endif // ifndef NAMESPACE_GREAT_HEADER_H_

In the past, I remember caveats such as "#pragma once only first if it is the first line in the file", and similar rules for include-guard optimization - but I'm not sure if that is still the case. It would be convenient for me, and for automated tools which extract top-of-header info if comments could be the first thing in the file.

like image 452
BeeOnRope Avatar asked Feb 03 '17 21:02

BeeOnRope


1 Answers

According to GCC Preprocessor Internals manual, the multiple include optimization mechanism is not affected by comments:

The Multiple-Include Optimization

Under what circumstances is such an optimization valid? If the file were included a second time, it can only be optimized away if that inclusion would result in no tokens to return, and no relevant directives to process. Therefore the current implementation imposes requirements and makes some allowances as follows:

  1. There must be no tokens outside the controlling #if-#endif pair, but whitespace and comments are permitted.

It doesn't mention #pragma once there, which I suspect is treated separately. Referring to 2.5 Alternatives to Wrapper #ifndef:

Another way to prevent a header file from being included more than once is with the ‘#pragma once’ directive. If ‘#pragma once’ is seen when scanning a header file, that file will never be read again, no matter what.

like image 73
Grzegorz Szpetkowski Avatar answered Oct 03 '22 14:10

Grzegorz Szpetkowski