Include guards vs pragma once

Post Reply
FluffyFreak
Posts: 1343
Joined: Tue Jul 02, 2013 1:49 pm
Location: Beeston, Nottinghamshire, GB
Contact:

Include guards vs pragma once

Post by FluffyFreak »

Just throwing this out there.
Noticed some discussion about using include guards vs pragma once in the UnrealEngine4 sourcecode, I hadn't given it much thought before but "pragma once" is supposed to be quicker and is supported by all of the compilers now.

Just checked and we're not using it at all on Pioneer.

Is it ok to update the coding standards and start using it as well or instead of include guards?

By include guards I means MACRO usage like:

Code: Select all

#ifndef _MATERIAL_H
#define _MATERIAL_H
// code goes here
...
#endif
Andy
lwho
Posts: 72
Joined: Thu Jul 04, 2013 9:26 pm
Location: Germany

Re: Include guards vs pragma once

Post by lwho »

Pragmas are not portable. So, at least one should use both in case the pragma is ignored:

Code: Select all

#pragma once

#ifndef _MATERIAL_H
#define _MATERIAL_H
// code goes here
...
#endif
I suspect though, that the speed-up of compilation time (if any) is so minor, that one should better stay on the territory covered by the standard.
FluffyFreak
Posts: 1343
Joined: Tue Jul 02, 2013 1:49 pm
Location: Beeston, Nottinghamshire, GB
Contact:

Re: Include guards vs pragma once

Post by FluffyFreak »

On the portability bit: that was true a few years ago but now...
robn
Posts: 302
Joined: Mon Jul 01, 2013 1:11 am
Location: Melbourne, Australia

Re: Include guards vs pragma once

Post by robn »

#pragma once isn't standard, but is supported everywhere we care about. Most compilers already specifically optimise include guards though, so I doubt there's any significant performance gain (but profiling, you know).

There's an argument that pragma once is easier to use especially when copying a header to make a new one. There's an equal argument that guards are more familiar, and so more maintainable.

So with equal points to both sides, I officially don't care! I wouldn't bother because it's work for no gain, but if you want to do it then do it.
Post Reply