(maybe) stupid C++ question Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
theigor
Experienced Solver
Experienced Solver
Posts: 78
Joined: Thu Jan 12, 2006 6:51 pm

(maybe) stupid C++ question

Post by theigor »

What's the difference between using

Code: Select all

#pragma once
in the beginning of a header file and a

Code: Select all

#ifndef FILE_H
#define FILE_H
...
#endif
:?:
protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX

Post by protocol »

Thats not a stupid question to me.

They are both the same but:
I wrote: #pragma once (and #import)
- is compiler specific

&&

the #define conditional works everywhere
Also: http://en.wikipedia.org/wiki/Pragma_once
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!
eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew

Post by eco »

The wikipedia article is incorrect. #pragma once also works on GCC 3.4+ (it was un-deprecated) and inclusion guards and #pragma once aren't quite the same thing. The result is the same but #pragma once improves compilation speed (see this Games from Within article for details). Use both to get the speed of #pragma once but still fallback to inclusion guards for compilers which don't support it.
cpp
I live to help wx-kind
I live to help wx-kind
Posts: 195
Joined: Wed Sep 28, 2005 9:42 pm

Post by cpp »

mhh, Isnt #pragma once part of ISO compliant C++??
Hier Kommt die Sonne...
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore

Post by lowjoel »

Yes, it's meant for "allowing various instructions to be given to a compiler". It is standard, but there may be some features present in compiler 'a' that isn't in compiler 'b', since it is implementation defined. IIRC only VC has #pragma once.

Most compilers wont stop and give an error if they encounter an unknown pragma, most will just issue a warning.
eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew

Post by eco »

To clarify what lowjoel was saying: "#pragma" is part of the standard. "#pragma once" is not. "#pragma" is for issuing compiler specific directives not covered by the standard. VC, for instance, supports warning disabling like this: "#pragma warning ( disable : 4101)". Also, as I said before, VC isn't the only compiler that supports "#pragma once". I know both GCC 3.4+ and Metrowerks support it. Others probably do as well.
cpp
I live to help wx-kind
I live to help wx-kind
Posts: 195
Joined: Wed Sep 28, 2005 9:42 pm

Post by cpp »

is it safe to say that #pragrma once (not just pragma), is supported by all modern & good compilers?
Hier Kommt die Sonne...
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore

Post by lowjoel »

no... IIRC I'd only dare use it under a VC-only environment
jbacigal
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Sep 19, 2005 8:22 pm

Post by jbacigal »

I generally use the #ifndef #defne header guards and then #pragma once beneath it. I don't think there is any adverse affect to using both - get the compile time boost if #pragma once is supported, if not, you still have inclusion guards.