Connecting Tech Pros Worldwide Forums | Help | Site Map

#if !defined() vs #ifndef

Evan
Guest
 
Posts: n/a
#1: Jul 19 '05
Is there any standard for when to use #if
!defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD?

Aside from being able to combine multiple things into an #if, is there
any difference? My VC++ docs say that thay are equivalent, but is
there any convention on when to use one vs. another?

Also, how widely is #pragma once supported?

Samuele Armondi
Guest
 
Posts: n/a
#2: Jul 19 '05

re: #if !defined() vs #ifndef



"Evan" <eed132@psu.edu> wrote in message
news:3f25c666.0307111216.4ddbc84c@posting.google.c om...[color=blue]
> Is there any standard for when to use #if
> !defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD?
>
> Aside from being able to combine multiple things into an #if, is there
> any difference? My VC++ docs say that thay are equivalent, but is
> there any convention on when to use one vs. another?
>
> Also, how widely is #pragma once supported?[/color]
I think #pragma is designed to allow compiler vendors to provide their own
preprocessor extension (MS VC++ offers things like pack and others). Hence
it will vary across different compilers/platforms.
HTH,
S. Armondi


Scott Condit
Guest
 
Posts: n/a
#3: Jul 19 '05

re: #if !defined() vs #ifndef


Evan wrote:[color=blue]
> Is there any standard for when to use #if
> !defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD?[/color]

Both are perfectly fine in Standard C++,

However, if you are actually writing an inclusion guard or
similar which defines a macro immediately afterwards, an
#ifndef and a #define on the subsequent line, match up quite
nicely, as you can see:

#ifndef A
#define A
....
#endif

It's just a matter of preference, style or coding standards.
[color=blue]
> Also, how widely is #pragma once supported?[/color]

What a #pragma does is entirely up to a compiler, and an
unknown #pragma is ignored. So it's "supported" everywhere,
but only causes the specific behavior you have in mind
on those compilers for which you know for certain that it
causes that specific behavior.

S





TR
Guest
 
Posts: n/a
#4: Jul 19 '05

re: #if !defined() vs #ifndef


"Scott Condit" <socode@socode.com> wrote in message
news:benedq$7dtph$1@ID-189137.news.uni-berlin.de...[color=blue][color=green]
> > Also, how widely is #pragma once supported?[/color]
>
> What a #pragma does is entirely up to a compiler, and an
> unknown #pragma is ignored. So it's "supported" everywhere,
> but only causes the specific behavior you have in mind
> on those compilers for which you know for certain that it
> causes that specific behavior.[/color]

Satisfying knowing you've helped someone out, isn't it?


Scott Condit
Guest
 
Posts: n/a
#5: Jul 19 '05

re: #if !defined() vs #ifndef


TR wrote:[color=blue]
> "Scott Condit" <socode@socode.com> wrote in message
> news:beoc1s$7c7pl$1@ID-189137.news.uni-berlin.de...
>[color=green]
>>Better to tell how it is than how someone would like it
>>to be, isn't it?[/color]
>
>
> He clearly knows pragma once isn't defined by the standard,
> but that wasn't his question.[/color]

But might have thought its behavior would be the same on any
system that appeared to accept it. See below.
[color=blue]
> If you don't know the answer, you're sometimes better not
> saying anything.[/color]



Closed Thread