Some clarification needed on # pragma once

I searched all for some clarification that # the pragma actually does once and cannot find the final answers to some questions that I have.

Does pragma # make sure once that the header file in which it is included is called only as AS WELL AS, that the headers that are included in the specified header file are not yet included? Also, if it is called only once, does this mean a .cpp file that needs a specific header will not be able to access it? If the header file is marked # pragma once and included in .cpp, can this header file be used again elsewhere?

These are the clarifications that I cannot find. Sorry if there is documentation that clarifies this somewhere, but I really could not find anything specific enough.

+6
source share
3 answers

#pragma once protects only one file per translation unit, not counting its inclusion sub-hierarchy. (However, if the second inclusion of the file is prevented, it is not possible to double-enable anything else.)

You can enable it again from another .cpp .

A file is usually identified by its inode number.

Note that #pragma once strictly non-standard, and most still prefer the traditional #ifndef header protection.

+5
source

#pragma once causes the current source file to be included only once in one compilation. It essentially resembles the #include guards.

+3
source

Does #pragma provide that the header file that it is included is called only as AS WELL AS, that the headers that are included in the specified header file are not yet included?

Pragma does not affect other headers. if the header with the pragma "ah" includes "bh", "bh" can be re-enabled through the third header or directly.

Also, if it is called only once, does this mean that a .cpp file that needs a specific header will not be able to access it?

You can include the title from anywhere you want, as many times as you see fit.

If the header file is marked #pragma once and is included in .cpp, can this header file be used again elsewhere?

Yes, this is a common practice with headlines.


Where is the catch?

If you really need to include headers more than once, and each of them performs a different operation, than using the pragma once or the hourly macro. These cases are not common.

The advantage of pragma once is that it saves you from errors, such as the presence of 2 header files that accidentally have the same clock macro. this can happen when 2 header files have the same file name and the same coding style for macro names.

+2
source

All Articles