关键词搜索

源码搜索 ×
×

do while(0) 在宏定义中的重要用途

发布2013-09-19浏览7915次

详情内容

      我们先来看一个简单的程序:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. cout << "a" << endl; \
  5. cout << "b" << endl;
  6. int main()
  7. {
  8. int i = 0;
  9. for(i = 0; i < 2; i++)
  10. {
  11. fun();
  12. }
  13. return 0;
  14. }

     结果为:

a

b

a

b

 

     我们再看看程序:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. cout << "a" << endl; \
  5. cout << "b" << endl;
  6. int main()
  7. {
  8. int i = 0;
  9. for(i = 0; i < 2; i++)
  10. fun();
  11. return 0;
  12. }

     结果为:

a

a

b

 

     上述两个程序的结果居然不一样,是for用错了吗?不是的。在第二个程序中,for虽然用得不好,但没有什么语法和逻辑错误,至于结果为什么不一样,那是因为在用宏的时候,没有使用大括号,所以应该加上大括号,如下:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. {\
  5. cout << "a" << endl; \
  6. cout << "b" << endl; \
  7. }
  8. int main()
  9. {
  10. int i = 0;
  11. for(i = 0; i < 2; i++)
  12. fun();
  13. return 0;
  14. }

     结果为:

a

b

a

b

     虽然上面程序中for的用法是对的,但并不好,所以尽量改为:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. {\
  5. cout << "a" << endl; \
  6. cout << "b" << endl; \
  7. }
  8. int main()
  9. {
  10. int i = 0;
  11. for(i = 0; i < 2; i++)
  12. {
  13. fun();
  14. }
  15. return 0;
  16. }

      结果为:

a

b

a

b

 

     这样就万事大吉了吗?非也,非也!请看程序:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. {\
  5. cout << "a" << endl; \
  6. cout << "b" << endl; \
  7. }
  8. int main()
  9. {
  10. if(1 > 0)
  11. fun();
  12. else
  13. fun();
  14. return 0;
  15. }

     这个程序的结果是什么呢?结果是:编译出错!为什么呢?下面,我们给出上述程序的等价形式:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. if(1 > 0)
  6. {
  7. cout << "a" << endl;
  8. cout << "b" << endl;
  9. }; // 注意:这里有个分号,所以if部分已经结束了
  10. else // 此处else没有匹配的if, 所以编译出错
  11. {
  12. cout << "a" << endl;
  13. cout << "b" << endl;
  14. }
  15. return 0;
  16. }

     现在明白了吧?那么,应该怎么改呢? 当然,如果你用下面的方式,那也是可以解决问题的:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() \
  4. {\
  5. cout << "a" << endl; \
  6. cout << "b" << endl; \
  7. }
  8. int main()
  9. {
  10. if(1 > 0)
  11. {
  12. fun();
  13. }
  14. else
  15. {
  16. fun();
  17. }
  18. return 0;
  19. }

      但是,你怎么可能保证项目中某些“程序猿”在用if else时能时时带上大括号呢?既然保证不了,那就用do while(0)吧,如下:

  1. #include <iostream>
  2. using namespace std;
  3. #define fun() do \
  4. {\
  5. cout << "a" << endl; \
  6. cout << "b" << endl; \
  7. } while(0) // 注意,此处不要加分号
  8. int main()
  9. {
  10. if(1 > 0)
  11. fun();
  12. else
  13. fun();
  14. int i = 0;
  15. for(i = 0; i < 2; i++)
  16. fun();
  17. cout << endl;
  18. // 当然,更好的方式是:
  19. if(1 > 0)
  20. {
  21. fun();
  22. }
  23. else
  24. {
  25. fun();
  26. }
  27. for(i = 0; i < 2; i++)
  28. {
  29. fun();
  30. }
  31. return 0;
  32. }

      OK, 至此才万事大吉。下次再看在项目中看到do while(0)的时候,不要再大吃一惊。

 







 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载