컴퓨터공부/C & C++ & STL

#if 0

achivenKakao 2007. 3. 1. 05:03
  1. Use #if 0 rather than comments to temporarily kill blocks of code.

    Non-portable example:

      int  foo()  {    ...    a = b + c;    /*     * Not doing this right now.    a += 87;    if (a > b) (* have to check for the                  candy factor *)      c++;     */    ...  }

    This is a bad idea, because you always end up wanting to kill code blocks that include comments already. No, you can't rely on comments nesting properly. That's far from portable. You have to do something crazy like changing /**/ pairs to (**) pairs. You'll forget. And don't try using #ifdef NOTUSED, the day you do that, the next day someone will quietly start defining NOTUSED somewhere. It's much better to block the code out with a #if 0, #endif pair, and a good comment at the top. Of course, this kind of thing should always be a temporary thing, unless the blocked out code fulfills some amazing documentation purpose.

    Portable example:

      int  foo()  {    ...    a = b + c;  #if 0    /* Not doing this right now. */    a += 87;    if (a > b) /* have to check for the                  candy factor */      c++;  #endif    ...  }

+

 

#ifdef 가 무난할텐데요?
대개 다음 정도로 할 겁니다.

#if 0/* meaningless codes */#endif

체스맨님의 의견에 한표를 던지면서...
위의 코드같은 경우 0을 1로 바꾸면서 넣었다 뺐다 하는 거고...
덧붙여서 두개의 코드를 번갈아 테스트해야할 경우는

#if 0/* excluded */#else/* included */#endif

에서 0, 1 을 번갈아 하면 두개의 코드를 테스트 할 때 편하죠.

3개 이상은 다음과 같이....

#define FLAG 2#if FLAG == 0  printf("FLAG is 0");#endif#if FLAG == 1  printf("FLAG is 1");#endif#if FLAG == 2  printf("FLAG is 2");#endif

+

 

출처는 KLDP와 모질라 C++ portability guide

 

+

 

정말 편리하다..