#ifdef vs #if - which is better/safer as a method

2019-01-08 04:25发布

This may be a matter of style, but there's a bit of a divide in our dev team and I wondered if anyone else had any ideas on the matter...

Basically, we have some debug print statements which we turn off during normal development. Personally I prefer to do the following:

//---- SomeSourceFile.cpp ----

#define DEBUG_ENABLED (0)

...

SomeFunction()
{
    int someVariable = 5;

#if(DEBUG_ENABLED)
    printf("Debugging: someVariable == %d", someVariable);
#endif
}

Some of the team prefer the following though:

// #define DEBUG_ENABLED

...

SomeFunction()
{
    int someVariable = 5;

#ifdef DEBUG_ENABLED
    printf("Debugging: someVariable == %d", someVariable);
#endif
}

...which of those methods sounds better to you and why? My feeling is that the first is safer because there is always something defined and there's no danger it could destroy other defines elsewhere.

18条回答
我命由我不由天
2楼-- · 2019-01-08 04:44

I think it's entirely a question of style. Neither really has an obvious advantage over the other.

Consistency is more important than either particular choice, so I'd recommend that you get together with your team and pick one style, and stick to it.

查看更多
Viruses.
3楼-- · 2019-01-08 04:47

There is a difference in case of different way to specify a conditional define to the driver:

diff <( echo | g++ -DA= -dM -E - ) <( echo | g++ -DA -dM -E - )

output:

344c344
< #define A 
---
> #define A 1

This means, that -DA is synonym for -DA=1 and if value is omitted, then it may lead to problems in case of #if A usage.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-08 04:48

Both are exactly equivalent. In idiomatic use, #ifdef is used just to check for definedness (and what I'd use in your example), whereas #if is used in more complex expressions, such as #if defined(A) && !defined(B).

查看更多
仙女界的扛把子
5楼-- · 2019-01-08 04:49

I've always used #ifdef and compiler flags to define it...

查看更多
我只想做你的唯一
6楼-- · 2019-01-08 04:49

I used to use #ifdef, but when I switched to Doxygen for documentation, I found that commented-out macros cannot be documented (or, at least, Doxygen produces a warning). This means I cannot document the feature-switch macros that are not currently enabled.

Although it is possible to define the macros only for Doxygen, this means that the macros in the non-active portions of the code will be documented, too. I personally want to show the feature switches and otherwise only document what is currently selected. Furthermore, it makes the code quite messy if there are many macros that have to be defined only when Doxygen processes the file.

Therefore, in this case, it is better to always define the macros and use #if.

查看更多
虎瘦雄心在
7楼-- · 2019-01-08 04:51

That is not a matter of style at all. Also the question is unfortunately wrong. You cannot compare these preprocessor directives in the sense of better or safer.

#ifdef macro

means "if macro is defined" or "if macro exists". The value of macro does not matter here. It can be whatever.

#if macro

if always compare to a value. In the above example it is the standard implicit comparison:

#if macro !=0

example for the usage of #if

#if CFLAG_EDITION == 0
    return EDITION_FREE;
#elif CFLAG_EDITION == 1
    return EDITION_BASIC;
#else
    return EDITION_PRO;
#endif

you now can either put the definition of CFLAG_EDITION either in your code

#define CFLAG_EDITION 1 

or you can set the macro as compiler flag. Also see here.

查看更多
登录 后发表回答