Microsoft KB Archive/166721

From BetaArchive Wiki

Article ID: 166721

Article Last Modified on 7/5/2005



APPLIES TO

  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition



This article was previously published under Q166721

SYMPTOMS

When you attempt to instantiate an STL container such as vector or list, you receive compiler error C2784 if the class type of the objects contained do not have operator < and operator == defined to take the class type as both arguments:

xutility(45) : error C2784: 'bool __cdecl std::operator<(const class
std::reverse_iterator<`template-parameter-1',`template-parameter-2
',`template-parameter-3',`template-parameter-4',`template-parameter-5'>
&,const class std::reverse_iterator<`template-parameter-1', `template-
parameter-2',`template-parameter-3',`template-parameter-4',
`template-parameter-5'> &)' : could not deduce template argument for
'const class std::reverse_iterator<`template-parameter-1',
`template-parameter-2',`template-parameter-3',`template-parameter-4',
`template-parameter-5'> &' from 'const struct P'

xutility(45) : error C2676: binary '<' : 'const struct MyClass' does not
define this operator or a conversion to a type acceptable to the
predefined operator

CAUSE

The Visual C++ 5.0 compiler forces the instantiation of inline template member functions regardless of whether they are referenced. The member function generated is later discarded if it is not referenced.

RESOLUTION

Declare a global operator < and a global operator == that take the given class type in both arguments. Note that the operators don't need to have bodies because they aren't actually used and will be discarded by the linker anyway.

   #if (_MSC_VER == 1100)
   #define GLOBAL_OPS(Type) \ 
     bool operator < (const Type& lhs, const Type& rhs);
     bool operator == (const Type& lhs, const Type& rhs);

   GLOBAL_OPS(MyClass)
   GLOBAL_OPS(MyOtherClass)
   #endif
                

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Visual Studio 97 Service Pack 1.

For additional information about the Visual Studio 97 Service Pack 1, please see the following article in the Microsoft Knowledge Base:

170365 INFO: Visual Studio 97 Service Packs - What, Where, and Why


MORE INFORMATION

Note that if you use functions such as find() or sort() or compare containers (using one of the logical operators), you must define less than and equality operators for the class type being contained in the STL container.

Steps to Reproduce Behavior

    /*
    Compile options needed to reproduce:   -GX
    Compile options needed for workaround: -GX -D"WORKAROUND"
    */ 

    #include <vector>
    using namespace std;

    struct MyClass {
            // members here
    };
    struct MyOtherClass {
            // members here
    };

    #ifdef WORKAROUND
    #if (_MSC_VER == 1100)
    #define GLOBAL_OPS(Type) \ 
      bool operator < (const Type& lhs, const Type& rhs) {return true;} \ 
      bool operator == (const Type& lhs, const Type& rhs) {return true;}

    GLOBAL_OPS(MyClass)
    GLOBAL_OPS(MyOtherClass)
    #endif //_MSC_VER
    #endif //WORKAROUND

    void main () {
            vector<MyClass> a;
    }
                

Keywords: kbbug kbfix kblangcpp kbvs97sp1fix kbcpponly kbcode kbcompiler KB166721