On 20 Apr 2005 22:56:43 -0700, lovecreatesbeauty
<lo***************@gmail.com> wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.
The only real 'defect' is that it's a single condition being tested.
This makes it fine for header file guards:
#ifndef H_MYHEADER
#define H_MYHEADER
...
#endif
but not so useful for complex conditions:
#ifdef HAVE_MYHEADER
# if VERSION > 3
...
# endif
#endif
can often more clearly be written:
#if defined(HAVE_MYHEADER) && VERSION > 3
...
#endif
It's largely a matter of style preference. I like the #ifndef for
header include guards because it's easy to see that the macro being
tested is immediately defined, but for complex conditions I generally
prefer using defined().
(Whether you do defined FRED or defined(FRED) is also a style issue,
I've worked in places where one is compulsory and the other forbidden
but there is no logical reason to prefer one to the other as far as I
can see.)
Chris C