Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in STL's _Debug_lt function?

55 views
Skip to first unread message

Michael Mellor

unread,
Nov 28, 2005, 7:04:10 AM11/28/05
to
The following code when compiled in debug mode in Visual Studio 2005
gives errors about "no operator found which takes a left-hand operand of
type 'const B' (or there is no acceptable conversion)". This happens
because of "_Debug_lt" in xutility which does both Left < Right and
Right < Left to check for strict weak ordering. However, section
25.3.3.1 of the C++ standard on lower_bound states:

"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"

from this I would assume the value parameter will only every appear as
the right-hand operand and never the left-hand one in the call to
operator<. Is this a bug in VCs STL or am I missing something?


#include <vector>
#include <algorithm>

struct A {
int val;
};

struct B {
int val;
};

bool operator<( const A &a, const B &b ) {
return a.val < b.val;
}

void func ( ) {
std::vector<A> vec;
B comp;
std::lower_bound ( vec.begin(), vec.end(), comp );
}

Thanks,

Michael

Tom Widmer [VC++ MVP]

unread,
Nov 28, 2005, 7:55:41 AM11/28/05
to
Michael Mellor wrote:
> The following code when compiled in debug mode in Visual Studio 2005
> gives errors about "no operator found which takes a left-hand operand of
> type 'const B' (or there is no acceptable conversion)". This happens
> because of "_Debug_lt" in xutility which does both Left < Right and
> Right < Left to check for strict weak ordering. However, section
> 25.3.3.1 of the C++ standard on lower_bound states:
>
> "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"

You missed the other bit:
Requires: Type T is LessThanComparable (20.1.2).

> from this I would assume the value parameter will only every appear as
> the right-hand operand and never the left-hand one in the call to
> operator<. Is this a bug in VCs STL or am I missing something?

The requirement says that t<u must compile for two values of type T, in
your case B. However, this requirement has been relaxed for the next
standard:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

The dinkumware debugging version does pre-condition checks (e.g. it
checks the requires clauses of the algorithms) to make sure you haven't
written anything non-standard.

Tom

P.J. Plauger

unread,
Nov 28, 2005, 9:36:47 AM11/28/05
to
"Tom Widmer [VC++ MVP]" <tom_u...@hotmail.com> wrote in message
news:%23avXKsB...@TK2MSFTNGP15.phx.gbl...

Excellent summary, as always. But IIRC, the final version of V8
fixed this problem. (We made _Debug_lt a template with two parameters
instead of one.) To the OP: Are you using a beta or did this fix not
make the cut?

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Carl Daniel [VC++ MVP]

unread,
Nov 28, 2005, 10:59:12 AM11/28/05
to
P.J. Plauger wrote:
> "Tom Widmer [VC++ MVP]" <tom_u...@hotmail.com> wrote in message
>> The dinkumware debugging version does pre-condition checks (e.g. it
>> checks the requires clauses of the algorithms) to make sure you
>> haven't written anything non-standard.
>
> Excellent summary, as always. But IIRC, the final version of V8
> fixed this problem. (We made _Debug_lt a template with two parameters
> instead of one.) To the OP: Are you using a beta or did this fix not
> make the cut?

The two-parameter form of _Debug_lt is in the RTM version of VC 2005. The
OP's code still does not compile using that version, however. The following
operators must be added to support all of the necessary comparisons:

bool operator<( const A &a, const A &a2 ) {
return a.val < a2.val;
}

bool operator<( const B &b, const A &a ) {
return b.val < b.val;
}

-cd


Carl Daniel [VC++ MVP]

unread,
Nov 28, 2005, 11:30:30 AM11/28/05
to
Carl Daniel [VC++ MVP] wrote:
> bool operator<( const B &b, const A &a ) {
> return b.val < b.val;

return b.val < a.val;

-cd


0 new messages