#pragma once add inclusion guards?

#pragma once not standard, but is supported by compilers such as gcc and VC ++. This helps to avoid protection against inclusion.

But, internally, does the compiler add inclusion guards for #pragma once? If not, how does the compiler guarantee that such a header is included only once?

+6
source share
3 answers

I am sure that it works the same as include_once in PHP - there is a table of "files that were included." In this case, the compiler scans the list for the file that it should include, and if the file is already included, do not include it again. If the compiler, processing the file, sees #pragma once , and then adds this file to the "files that have already been included."

Thus, this is not the same as turning on guards at the level of detail, but it has the same effect as turning on guards. It also makes the code less portable, as there are many compilers that do not support this.

+10
source

No, the compiler will not add an include guard, but it does not matter since it will not include the same file again, so it will never receive changes to evaluate these guards in the first place.

0
source

When adding "#pragma once" to the file "file.h", the compiler will help us make sure that "no.ce" will be opened only.

But if I have a duplicate of "file.h" named "file_copy.h", if it is included, it will open.

-2
source

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


All Articles