c++visual-c++stlvisual-c++-2005

STL lower_bound not spec compliant


The following code doesn't compile in C++Builder 2009 or in Visual C++ 2005 when the macro _HAS_ITERATOR_DEBUGGING equals 1 but if commented out it will. It appears the lower_bound function isn't spec compliant. The library is swapping the arguments. Here's an excerpt form the spec. value should always be the second argument. Am I wrong?

NOTE: The test code wasn't designed to run. It was designed to illustrate the library bug.

C++ Spec excerpt

template<class ForwardIterator, class T, class Compare>
ForwardIterator
lower_bound(ForwardIterator first, 
            ForwardIterator last, 
            const T& value, 
            Compare comp);

25.3.3.1.3

Returns: The furthermost iterator i in the range [first, last] such that for any iterator j in the range [first, i) the following corresponding conditions hold: *j < value or comp(*j, value) != false

Visual Studio error message

Msg: error C2664: 'double mike::operator ()(const double,const char *) const' : cannot convert parameter 1 from 'const char [1]' to 'const double'

File: c:\program files\microsoft visual studio 8\vc\include\xutility

Line No: 314

Test Code

#define _HAS_ITERATOR_DEBUGGING 1  // needs to be in the stdafx.h file for Visual Studio
#include "stdafx.h"
#include <algorithm>
#include <functional>

struct mike : public std::binary_function<double, char*, double> {
   double operator() (const double i, const char*) const {
      return i;
   }
};

int main()
{
   double r[] = {0};
   std::lower_bound(r, r, "", mike());
   return 0;
}

Solution

  • This is a known issue in the Visual C++ 2005 C++ Standard Library implementation (see "Binary predicate paramter to lower_bound assumes that both parameters are the same type when compiling in debug mode" on Microsoft Connect).

    The bug was fixed in Visual C++ 2008.