#IFDEF VS#如果 - 这是更好/更安全的为启用/禁用code的特定部分的编译的方法? [英] #ifdef vs #if - which is better/safer as a method for enabling/disabling compilation of particular sections of code?

查看:100
本文介绍了#IFDEF VS#如果 - 这是更好/更安全的为启用/禁用code的特定部分的编译的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是风格问题,但有一个位在我们的开发团队一个鸿沟,我想知道,如果其他人有对此事的任何想法...

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...

基本上,我们有我们的正常发育过程中关闭一些调试打印语句。我个人preFER做到以下几点:

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
}

有些球队preFER以下虽则:

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.

推荐答案

我最初的反应的 #IFDEF ,当然的,但我认为#如果实际上有一些这方面的优势显著 - 这里的原因:

My initial reaction was #ifdef, of course, but I think #if actually has some significant advantages for this - here's why:

首先,你可以在preprocessor的的编译测试使用了 DEBUG_ENABLED 。示例 - 通常情况下,我想启用调试时长超时,因此使用#如果,我可以写这个

First, you can use DEBUG_ENABLED in preprocessor and compiled tests. Example - Often, I want longer timeouts when debug is enabled, so using #if, I can write this

  DoSomethingSlowWithTimeout(DEBUG_ENABLED? 5000 : 1000);

...而不是...

... instead of ...

#ifdef DEBUG_MODE
  DoSomethingSlowWithTimeout(5000);
#else
  DoSomethingSlowWithTimeout(1000);
#endif

二,你在一个更好的位置,如果你想从的#define 迁移到一个全局常量。 的#define ,则通常被大多数C ++程序员皱起了眉头。

Second, you're in a better position if you want to migrate from a #define to a global constant. #defines are usually frowned on by most C++ programmers.

第三,你说你在你的团队已经一分。我的猜测是,这意味着不同的成员已经采取了不同的方法,并且需要标准化。裁决#如果是pferred选择了$ P $使用意味着code #IFDEF 编译-and运行 - 甚至当 DEBUG_ENABLED 是假的。而且它的的容易追查并卸下时,它应不大于反之亦然生产调试输出。

And, Third, you say you've a divide in your team. My guess is this means different members have already adopted different approaches, and you need to standardise. Ruling that #if is the preferred choice means that code using #ifdef will compile -and run- even when DEBUG_ENABLED is false. And it's much easier to track down and remove debug output that is produced when it shouldn't be than vice-versa.

哦,还有一个小可读性点。您应该能够使用真/假,而不是在你的0/1 的#define ,并且因为值是一个单一的词汇标记,这是一次你不需要周围的括号。

Oh, and a minor readability point. You should be able to use true/false rather than 0/1 in your #define, and because the value is a single lexical token, it's the one time you don't need parentheses around it.

#define DEBUG_ENABLED true

而不是

#define DEBUG_ENABLED (1)

这篇关于#IFDEF VS#如果 - 这是更好/更安全的为启用/禁用code的特定部分的编译的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆