#if !defined() vs #ifndef

E

Evan

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?
 
S

Samuele Armondi

Evan said:
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?
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
 
S

Scott Condit

Evan said:
Is there any standard for when to use #if
!defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD?

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.
Also, how widely is #pragma once supported?

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
 
T

TR

Scott Condit said:
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.

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

Scott Condit

TR said:
He clearly knows pragma once isn't defined by the standard,
but that wasn't his question.

But might have thought its behavior would be the same on any
system that appeared to accept it. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top