Re: #if !defined(AFX_

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Bo Persson" <bop@xxxxxx> wrote in message
news:5lnf9oF92mfiU1@xxxxxxxxxxxxxxxxxxxxx
The next time the file is to be included, it just has to check if
SOME_SYMBOL is defined at that point. It doesn't matter where it comes
from.

My comment was about the claim that #pragma once is faster. It might be
more convenient to use, but it is not faster. It is less portable though.



This would be true if the code were written, e.g.

[foo.cpp]
#ifndef _BAR_H_INCLUDE_GUARD
#include "bar.h"
#endif

#ifndef _BAR2_H_INCLUDE_GUARD
#include "bar2.h"
#endif

....

[bar.h]
#ifndef _BAR_H_INCLUDE_GUARD
#define _BAR_H_INCLUDE_GUARD

#ifndef _BAR2_H_INCLUDE_GUARD
#include "bar2.h"
#endif

#endif

....

[bar2.h]
... // doesn't matter what is in bar2.h


Then you could say that the compiler would open and parse bar2.h only once.
However, most code is not written like this. Instead it is written like
this:

[foo.cpp]
#include "bar.h"
#include "bar2.h"

....

[bar.h]
#ifndef _BAR_H_INCLUDE_GUARD
#define _BAR_H_INCLUDE_GUARD

#include "bar2.h"

#endif

....

[bar2.h]
... // doesn't matter what is in bar2.h


In this case, there is no stopping the compiler from opening bar2.h a second
time when it is included in foo.cpp (the first time is when it is included
in bar.h).

#pragma once lets you write the clean code in the second example while
retaining the performance (lack of opening bar2.h a second time) of the
first example. Therefore, #pragma once does offer a value increase, and it
is not simply a worthless MS extension.

-- David


.



Relevant Pages

  • Re: Create Header File
    ... support #pragma once. ... I personally dislike using UUIDS as header file guards, ... where ORG is (short-ish) orgnization name, ...
    (microsoft.public.vc.mfc)
  • [SUMMARY] compiling gcc3.2.x
    ... This patch fixes the Tru64 UNIX V5.1B bootstrap failure reported by Olle ... for the pragma, gcc predefines __EXTERN_PREFIX. ... retrieving revision 1.29 ...
    (Tru64-UNIX-Managers)
  • Re: Compare sign in C++
    ... could be applied to floats or integer types too.... ... #pragma pack ... struct SignBit { ...
    (comp.lang.cpp)
  • Re: Tcl-ínterpreter in c++
    ... this to one of your source files: ... # ifdef USE_TCL_STUBS ... # pragma comment(lib, "tclstub" \ ...
    (comp.lang.tcl)
  • [PATCH] Suspend2 Merge: init hooks
    ... [Only sent this to Andrew the first time, ... This patch adds code to files in the init directory to fire suspend at ... can be loaded from an initrd, so we don't use straight initcalls. ...
    (Linux-Kernel)