6

I've searched all over for some clarification on what #pragma once actually does and can't find definitive answers for some questions I still have.

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

These are the sorts of clarifications I am not finding. Sorry if there is documentation that clarifies this somewhere, but I really couldn't find any thing specific enough.

NanoTree
  • 75
  • 1
  • 7

3 Answers3

8

#pragma once only guards a single file in a single translation unit, not counting its sub-hierarchy of inclusion. (However, if the file's second inclusion is prevented, it doesn't have an opportunity to doubly-include anything else.)

You can still include it again from another .cpp.

The file is usually identified by its inode number.

Note that #pragma once is strictly nonstandard, and most still prefer traditional #ifndef header guards.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • This helps me a little, and since I'm toying around with directX, the code is not platform mobile in the first place. I guess what I need to understand now, is what a translation unit is, which I'm betting is well documented, and also compiler specific? – NanoTree Feb 18 '14 at 03:28
  • 2
    @ManicCure, Put simply, it's one (cpp) file and all of the headers directly or indirectly included in it. If you want a standard definition, *The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.6.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit.* – chris Feb 18 '14 at 03:32
  • `standard => supported` `-->` https://en.wikipedia.org/wiki/Pragma_once – Andreas is moving to Codidact Jan 15 '19 at 14:26
  • @Andreas widely supported != good practice. There’s a lot of debate which doesn’t bear repeating here. – Potatoswatter Jan 15 '19 at 14:29
4

#pragma once causes the current source file to be included only once in a single compilation. It's essentially similar to #include guards.

Rivasa
  • 6,510
  • 3
  • 35
  • 64
shivakumar
  • 3,297
  • 19
  • 28
  • okay, so, can you help me understand what a compilation is? because what this sounds like to me is that a the compiler will ignore all includes of the same header file, even if it may be needed and wouldn't otherwise cause a compiler error, thus causing a compiler error. Confused? I know I am... #include guards have very evident logic; #pragma once does not because the actions the compiler takes is not communicated to the reader of the code. – NanoTree Feb 18 '14 at 03:20
3

Does #pragma once insure that the header file it is included in is only called once AS WELL AS that the headers which are included in said header file are not yet included?

The pragma doesn't affect other headers. if the header with the pragma 'a.h' includes 'b.h', 'b.h' can be included again via a third header or directly.

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

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

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

Yes, this is the normal practice with headers.


Where's the catch?

If you really need to include the headers more than once and every include performs a different operation than don't use pragma once or a sentry macro. These cases are not common.

A benefit to pragma once is that it saves you from bugs like having 2 header files that by chance have the same sentry macro. this can happen when 2 header files have the same file name and same coding style for macro names.

egur
  • 7,830
  • 2
  • 27
  • 47
  • In a since, #pragma once can be included in every header file you create and you will never have trouble with an undeclared identifier, assuming you've include all the right files? – NanoTree Feb 18 '14 at 03:36
  • 1
    @ManicCure `pragma once` only prevents duplicate declarations. Including all the right files is the tricky part. Problem is, C++ can get slow if you include too many false dependencies. – Potatoswatter Feb 18 '14 at 03:38
  • 1
    That's right. If you want better compile speed, read about precompiled headers. But that's another issue. – egur Feb 18 '14 at 03:41