Reputation: 9839
#pragma once
is not standard, but is supported by compilers like gcc and VC++. It helps to avoid inclusion guards.
But, internally, does the compiler add inclusion guards for #pragma once
? If not, how does the compiler ensure that such a header is included only once?
Upvotes: 6
Views: 521
Reputation: 2857
When adding "#pragma once" to file "file.h",the compiler help us ensure the "file.h" will only be opened noce.
But if I have a duplication of "file.h" named "file_copy.h",if it is included, it will be opened.
Upvotes: -3
Reputation: 129454
I'm sure it works just like include_once
in PHP - there is a table of "files that has been included". The compiler, in this case, looks in the list for the file it is about to include and if a file has already been included, don't include it again. If the compiler, while processign the file, sees a #pragma once
, then add this file to "files that have already been included".
So it's not the same as inclusion guards on a detail level, but it has the same effect as inclusion guards. It also makes the code less portable, since there are plenty of compilers that doesn't support this.
Upvotes: 10
Reputation: 208406
No, the compiler will not add include guards, but that should not matter, as it won't include the same file again, so it would never get a change to evaluate those guards in the first place.
Upvotes: 0