Is there any harm in creating comments first in the C header file?

Does it make sense in modern compilers to post comments starting at the beginning of the 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 this is the first line in the file", and similar rules for optimizing inclusion-protection - but I'm not sure if this is still the case. It would be convenient for me for automated tools that extract top-level information if comments can be first in the file.

+5
source share
1 answer

According to the GCC Preprocessor Internals manual, the multi-inclusion comment mechanism is independent of comments:

Multi-Inclusion Optimization

Under what circumstances is such an optimization valid? If the file was included for the second time, it can only be optimized if this inclusion does not lead to the return of tokens and any relevant directives for processing. Therefore, the current implementation imposes requirements and makes some assumptions as follows:

  • There should be no tokens for the #if-#endif control pair, but spaces and comments are allowed .

It does not mention #pragma once , which, I suspect, is considered separately. Referring to 2.5 Wrapper Alternatives #ifndef :

Another way to prevent the inclusion of a header file more than once with the ' #pragma once ' #pragma once . If ' #pragma once visible when scanning the header file, this file will never be read again, no matter what .

+5
source

Source: https://habr.com/ru/post/1263804/


All Articles