Sign in to follow this  
Dezachu

#pragma once

Recommended Posts

Hi hi guys :)

Going into my second year of uni and it's about time I really knuckled down, endured many headaches and cracked on with some C++!

Quick question about '#pragma once'. I know it makes sure a header file is only included once in the compilation. Well, source files, not strictly headers [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]

Anyway, I wanted to know if it's worth including this in EVERY header file I write, or is there a particular reason one may wish to repeatedly include a particular file?


Thanks guys!

Share this post


Link to post
Share on other sites
1) Do not use it in source files. Source files are not "included" anyway.
2) You should use it in every header file. If you don't, you are just asking for trouble. And no, there is no reason to include a header file more than once.
3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).
The correct (i.e. portable) way is to do it like this:
#ifndef SOME_FILE_H
#define SOME_FILE_H

// all your definitions here

#endif Edited by sheep19

Share this post


Link to post
Share on other sites
[quote name='sheep19' timestamp='1348854775' post='4984792']
3) By using #pragma once you are not being "portable". That means your program is going to work only on windows, on the compiler that comes with visual studio (cl).
The correct (i.e. portable) way is to do it like this:
#ifndef SOME_FILE_H
etc
[/quote]
Yes and no.

1. "pragma once" is not as badly supported as portrayed (http://en.wikipedia.org/wiki/Pragma_once).
2. it is more straightforward and is trivial to "fix" (as it is superbly unambiguous) if target implementation does not support it.
3. i would welcome more pressure for other compiles to support it too as not supporting it is embarrassing.

If one makes some library for many platforms/compilers then one should use the old kind of guards tho.

Share this post


Link to post
Share on other sites
'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience. Edited by BinaryPhysics

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']
'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.

It is a far, far better idea to use header guards (defining a macro constant and testing for its existence) because all parts of the construct are standard compliant.

With respect to whether you should use it everywhere take a look at translation units and use the '/C' and '/P' flags for cl.exe; that spits out a pre-processed source file. Really the idea only needs to be used in header files because they are the only things that should really be 'included'.

There's no harm in using it in every file. In fact it probably makes your life easier in the long run in case you start re-adding the file elsewhere. That said, if you can guarantee you'll never include the file in another header then you can leave it out because it's not needed. Like I said though, probably easier to just get into the habbit of guarding every header for the delayed convenience.
[/quote]

#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.

However #pragma once is supported by MOST compilers worth using. Also using #pragma once instead of regular include guards can speed up compilation because it allows skipping of the file instead of parsing it for include guards.

So no it isn't a Visual studio only thing.

From the c++11 standard

16.6 Pragma directive [cpp.pragma]
1 A preprocessing directive of the form
# pragma pp-tokens opt new-line
causes the implementation to behave in an implementation-de?ned manner. The behavior might cause
translation to fail or cause the translator or the resulting program to behave in a non-conforming manner.
Any pragma that is not recognized by the implementation is ignored.

-------------------------------------------------------------

If you're paranoid you can use both, with out causing any errors
#pragma once
#ifndef MYFILE_H
#define MYFILE_H

#endif

The compiler will use the pragma if supported, if not, it will just ignore it and continue parsing. Edited by EddieV223

Share this post


Link to post
Share on other sites
Wow, cheers for all the replies guys :)

Basically #ifndef is understood by more compilers, but pragma is more efficient? My programs aren't going to be HUGE as I'm only just learning - I guess ifndef is better to work with for now?

Share this post


Link to post
Share on other sites
Obviously, #pragma once saves a few characters as compared to traditional include guards, though not enough to make a practical difference in most cases. In theory, #pragma once has a performance advantage over traditional include guards because the preprocessor doesn't reopen headers marked with #pragma once whereas with traditional include guards it needs to reopen the included file and reprocess the preprocessor directives before it can skip the body of the header. In practice, most compiler tool chains can perform the same optimization on include guard protected headers because include guards have a very simply pattern to them.

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses.[/quote]

[quote name='EddieV223' timestamp='1348873695' post='4984891']#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.[/quote]

Isn't that exactly what I said? 'pragma' is a pre-processor that allows an implementation to define it's own tokens.

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1348969079' post='4985216']
[quote name='BinaryPhysics' timestamp='1348871544' post='4984884']'pragma' is used to express implementation dependent preprocessor statements. The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses.[/quote]

[quote name='EddieV223' timestamp='1348873695' post='4984891']#pragma is standard, the uses for it such as 'once' are not. It is a way to provide additional information.[/quote]

Isn't that exactly what I said? 'pragma' is a pre-processor that allows an implementation to define it's own tokens.
[/quote]

[quote name='BinaryPhysics']
The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler.
[/quote]
That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say? Edited by EddieV223

Share this post


Link to post
Share on other sites
[quote name='EddieV223' timestamp='1348984586' post='4985275']That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say?[/quote]

But it's not written as part of the Standard. I was unaware of support external to MSVC++. Surely truly portable code is standard compliant because a compiler isn't required to support it.

Share this post


Link to post
Share on other sites
The issue is, what you said was "The idea, the use of 'once' that is, (as far as I know) is specific to the compiler that Visual Studio uses. Such a technique isn't portable if you wanted to jump to a different compiler."

You could have said it's not part of the standard and thus not guaranteed to work and you are unsure of the support outside of MSVC. That would have gotten your point across without making a wild and false assumption (false assumption have a tendency to annoy people).

Personally, if something is supported by Clang, GCC, Intel and MSVC I consider it standard enough for my purposes as that covers all platforms I have an interest in. I still use both #pragma once and a standard include guard though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this