Have you ever written a headline without guards?

I am wondering why C ++ compilers do not automatically create headers for headers?

// Why do I have to write this for every .hpp file I create?!! #ifndef myheader_hpp__ #define myheader_hpp__ // ... #endif 

I have not met a situation where they are not needed, when I write my headlines. I do not see a real case of using the opposite behavior, but I would be glad to see it. Are there any technical difficulties or is it just a story ?!

+7
source share
4 answers

There are some preprocessing tricks that require the same header, are included several times in the same compilation unit . Another link.

In addition, most compilers allow you to shorten all of this to:

 #pragma once 
+10
source

To the compiler, automatically enabling security devices is not really practical. You can define prototypes for / functions / classes / etc methods without problems, and this is usually done in the header. However, if the class is defined in the header, you are faced with the problem that the class defined more than once by the compiler, if it is included in two different .cpp files or other headers.

Indeed, the inclusion of guards is just one trick for headlines. You do not always need them, and there are times when you will not use them. Itโ€™s actually easier, believe it or not.

+2
source

Because it is a general-purpose mechanism for inserting one file into another.

It is simply that a general-purpose ability is used for a very general specific purpose in 99% of cases.

+2
source

I will just point you to the Clang / LLVM project.

In this project, they created a way to encode data using a simple descriptive language, which is then loaded into a tool (called tblgen for the Generator Table) that is designed to create a C ++ file. For example, diagnostics :

 let Component = "Sema" in { let CategoryName = "Semantic Issue" in { // Constant expressions def err_expr_not_ice : Error< "expression is not an integer constant expression">; .... 

Clang has several thousand diagnostic tools separated by several files. After tblgen processing, they will generate a huge .inc file, which for each diagnostics will contain a macro call. By defining a macro and including the file, you can create a C ++ table (or something else actually, but use it usually for tables):

 static const StaticDiagInfoRec StaticDiagInfo[] = { #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP, \ SFINAE,ACCESS,NOWERROR,SHOWINSYSHEADER, \ CATEGORY,BRIEF,FULL) \ { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, \ NOWERROR, SHOWINSYSHEADER, CATEGORY, \ STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t), \ STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t), \ STR_SIZE(FULL, uint16_t), \ #ENUM, GROUP, DESC, BRIEF, FULL }, #include "clang/Basic/DiagnosticCommonKinds.inc" #include "clang/Basic/DiagnosticDriverKinds.inc" #include "clang/Basic/DiagnosticFrontendKinds.inc" #include "clang/Basic/DiagnosticLexKinds.inc" #include "clang/Basic/DiagnosticParseKinds.inc" #include "clang/Basic/DiagnosticASTKinds.inc" #include "clang/Basic/DiagnosticSemaKinds.inc" #include "clang/Basic/DiagnosticAnalysisKinds.inc" #undef DIAG { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; 

And the same files can create different tables, since you can write macros as you wish.

This, of course, is a very specific use.

But donโ€™t worry, although the modules didnโ€™t get into C ++ 11, we can hope for C ++ 1x.

+2
source

All Articles