#pragma once

Started by
16 comments, last by tom_mai78101 11 years, 6 months ago
Most major compilers support it, and yes, it is efficient. I use it in all my headers to get rid of file problems, and if I run into any trouble, I use forward declarations (That servantofthelord told me about).

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
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.
I'll stick with #pragma for now then and if I run into any problems I'll switch to include guards then. Thanks all :)

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

'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.


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


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

[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.


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


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?

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

That's the part we are having trouble with. If most compilers support it that makes it rather portable wouldn't you say?


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.
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.
Wait a minute, I thought "#pragma once" is going to be part of C++11, so that all compilers following C++11 will know of that preprocessor command. Looks like it's not.

This topic is closed to new replies.

Advertisement