Re: #if !defined(AFX_
- From: "David Ching" <dc@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 23 Sep 2007 22:34:56 GMT
"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
.
- Follow-Ups:
- Re: #if !defined(AFX_
- From: Bo Persson
- Re: #if !defined(AFX_
- From: David Ching
- Re: #if !defined(AFX_
- References:
- #if !defined(AFX_
- From: Steve Wolstenholme
- Re: #if !defined(AFX_
- From: Giovanni Dicanio
- Re: #if !defined(AFX_
- From: Steve Wolstenholme
- Re: #if !defined(AFX_
- From: Joseph M . Newcomer
- Re: #if !defined(AFX_
- From: David Ching
- Re: #if !defined(AFX_
- From: Bo Persson
- Re: #if !defined(AFX_
- From: Joseph M . Newcomer
- Re: #if !defined(AFX_
- From: Bo Persson
- #if !defined(AFX_
- Prev by Date: Change SID
- Next by Date: Re: passing a string to a dll
- Previous by thread: Re: #if !defined(AFX_
- Next by thread: Re: #if !defined(AFX_
- Index(es):
Relevant Pages
|