一例奇怪的编译错误(VC6)

VC6 编译下段代码会报错.


将出错行 

    if (mmm.end() == itr)        // ERROR 

改写为 

    if (itr == mmm.end())        // OK 

就可通过. 


(g++可以通过.) 
 
  
/**/ 
  /*
     error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'class std::_Tree<int,struct std::pair<int const ,int>,struct std::map<int,int,struct std::less<int>,class std:
:allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)

*/ 
  

 
  #pragma 
   warning(disable:4786) 
  

#include  
  < 
  map 
  > 
  

typedef std::map 
  < 
  int 
  ,  
  int 
  > 
   MYMAP;

 
  int 
   main( 
  int 
   argc,  
  char 
  * 
   argv[])
 
  ... 
  {
    MYMAP mmm;
    MYMAP::const_iterator itr = mmm.find(1234);
    // if (mmm.end() == itr)    ERROR!!! (VC6)
    // if (itr == mmm.end())    OK
    if (mmm.end() == itr)
        return 123;
    return 0;
}

粗略地分析是map.end()返回的是iterator类型,


而iterator::operator==(const_iterator)没有定义.



将map.end()强制为const_iterator也可以通过.