#pragma once vs. include guards

Started by
25 comments, last by kRogue 15 years, 6 months ago
I have read up on the pros and cons of #pragma once and include guards but I wanted to get a few opinions. Do you find yourself using #pragma once or include guards?
Advertisement
I use include guards, simply because Code::Blocks puts them there for me :)

I'm unsure there will be any difference between the two, I think most compilers have now caught up and handle it accordingly. Maybe some console developers here have some opinions?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Supposedly, if #pragma once works, it might improve compilation time.

The only thing is it could not work, but as the above post mentions, I think they smoothed that over.
I always use header guards as they are guaranteed to always work.
Include guards. I really doubt that the time saved by using #pragma once will be noticible. If in doubt, you can always use both to get the best of both worlds.
Include guards will never hurt.
Pragma once will never hurt.

So I use both together.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
Pragma once will never hurt.

Unless your code is compiled by an older version of gcc or LCC, both of which have had buggy implementation of pragma once such symbolic or hard links would confuse them.
#pragma once is not guaranteed to be supported on all compiliers. Thus I personally use inclusion guards.
Quote:Original post by Crypter
#pragma once is not guaranteed to be supported on all compiliers. Thus I personally use inclusion guards.

Yup that was my understanding and so I see inclusion guards being used in any books that are trying to be portable like Stroustroup's.
I just finished reading Horton's book on Visual Studio and he uses #pragma once all through the book and even admits it may not work on all compilers but still uses it since that's what Visual Studio uses. Personally, I use #pragma once if I can get away with it since I'm a lazy programmer and it's less typing;)

p.s. Actually, if you are using any of the various Microsoft wizards to generate MFC programs or something else it will automatically stick #pragma once in your header files for you!


[Edited by - daviangel on September 28, 2008 5:14:19 PM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by SiCrane
Quote:Original post by Promit
Pragma once will never hurt.

Unless your code is compiled by an older version of gcc or LCC, both of which have had buggy implementation of pragma once such symbolic or hard links would confuse them.
Although it seems odd to have symlinks or hardlinks involved, I suppose that's a fair enough reason otherwise.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement