clang vs gcc : 代码出错提示功能对比

来源:互联网 发布:数据库全书 编辑:程序博客网 时间:2024/05/18 01:05

今天研究c++ const 的问题,顺便做了一下gcc 与 clang 的代码编译错误提示对比,结论是,clang这个晚辈确实比gcc人性化多了。

参考:

http://www.cnblogs.com/Fancyboy2004/archive/2008/12/23/1360810.html

以下是测试代码:

 

 

Cpp代码
  1. #include <cstdlib>  
  2.   
  3. class Apple  
  4. {  
  5.     public:  
  6.         Apple (){};     /* constructor */  
  7.   
  8.         int GetCount()  /* 非const 成员函数 */  
  9.         {  
  10.             return m_iData;  
  11.         };  
  12.     protected:  
  13.         int m_iData;  
  14. }; /* -----  end of class Apple  ----- */  
  15.   
  16. using namespace std;  
  17.   
  18.   
  19. int main ( int argc, char *argv[] )  
  20. {  
  21.     const Apple apple;  
  22.     apple.GetCount();    /* 错误 */  
  23.   
  24.     return EXIT_SUCCESS;  
  25. }       // ----------  end of function main  ----------   
  26. //save to a.cc  
 

 

 

 

gcc编译命令:

Shell代码
  1. $ g++ a.cc  
  2. a.cc: In function ‘int main(int, char**)’:  
  3. a.cc:22: error: passing ‘const Apple’ as ‘this’ argument of ‘int Apple::GetCount()’ discards qualifiers  

 

clang:

Shell代码
  1. $ clang++ a.cc  
  2. a.cc:22:5: error: member function 'GetCount' not viable: 'this' argument has  
  3.       type 'const Apple', but function is not marked const  
  4.     apple.GetCount();    /* 错误 */  
  5.     ^~~~~  
  6. a.cc:8:13: note: 'GetCount' declared here  
  7.         int GetCount()  /* 非const 成员函数 */  
  8.             ^  
0 0