This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: LessThanComparable requirement overspecified?
- To: scott snyder <snyder at fnal dot gov>, austern at research dot att dot com
- Subject: Re: LessThanComparable requirement overspecified?
- From: Benjamin Kosnik <bkoz at redhat dot com>
- Date: Thu, 29 Jun 2000 16:10:04 -0700 (PDT)
- cc: libstdc++ at sourceware dot cygnus dot com
Matt I'm cc'ing you directly on this. Sorry if it's overkill.
> I noticed that the latest update of the `STL' part of libstdc++
> contains code to enforce the requirements on template parameters.
> This is quite useful --- i've already found several instances in my
> code where i was violating the spec.
Yeah it seemed like a good idea to me too. I'm not quite sure the work to
get clean error messages is worth it, but enforcing invariants and
requirements is a great idea.
Hey Gaby, any luck on getting human-readable error messages outta g++?
> However, there are some places where the new code seems to be overspecifying
> the requirements --- making demands not required by the C++ standard ---
> thus causing code that i believe to be legal to fail to compile.
>
> Here is one example:
>
> -----------------------------------------------------------------------
> #include <algorithm>
>
> struct foo {};
>
> bool operator== (const foo&, const foo&);
> bool operator< (const foo&, const foo&);
>
> void bar (foo* a, foo* b, foo& x)
> {
> foo* c = std::lower_bound (a, b, x);
> }
>
> -----------------------------------------------------------------------
>
> When i try to compile this, i get:
>
> /usr/local/egcs/include/g++-v3/bits/concept_checks.h: In function `_Type
> _STL_ERROR::__less_than_comparable_requirement_violation (_Type, _Type) [with _Type = foo]':
> /usr/local/egcs/include/g++-v3/bits/concept_checks.h:560: instantiated from `_LessThanComparable_concept_specification<_Type>::_LessThanComparable_requirement_violation (_Type) [with _Type = foo]'
> /usr/local/egcs/include/g++-v3/bits/stl_algo.h:1945: instantiated from `std::lower_bound (_ForwardIter, _ForwardIter, const _Tp &) [with _ForwardIter = foo *, _Tp = foo]'
> x.cc:10: instantiated from here
> /usr/local/egcs/include/g++-v3/bits/concept_checks.h:396: no match for `foo & >= foo &'
> /usr/local/egcs/include/g++-v3/bits/concept_checks.h:396: no match for `foo & <= foo &'
> /usr/local/egcs/include/g++-v3/bits/concept_checks.h:396: no match for `foo & > foo &'
>
>
> If i look at what __less_than_comparable_requirement is actually requiring:
>
> if (__a < __b || __a > __b || __a <= __b || __a >= __b) return __a;
>
> i.e., it requires the full suite of four inequality comparisons to be
> present, not just `<'.
>
> The requirement in the standard is that the type be LessThanComparable.
> According to 20.1.2, this requires only a `<' relation --- so i think
> that the code above is correct.
Hmm. Matt?
>
> This patch gets rid of the extra requirements, and allows the above
> example to compile.
>
> 2000-06-29 scott snyder <snyder@fnal.gov>
>
> * bits/concept_checks.h
> (__less_then_comparable_requirement_violation): Only check for <.
>
> Index: concept_checks.h
> ===================================================================
> RCS file: /cvs/gcc/egcs/libstdc++-v3/bits/concept_checks.h,v
> retrieving revision 1.1
> diff -u -p -c -p -r1.1 concept_checks.h
> *** concept_checks.h 2000/06/27 23:08:37 1.1
> --- concept_checks.h 2000/06/29 21:58:32
> *************** struct _STL_ERROR {
> *** 393,399 ****
> template <class _Type>
> static _Type
> __less_than_comparable_requirement_violation(_Type __a, _Type __b) {
> ! if (__a < __b || __a > __b || __a <= __b || __a >= __b) return __a;
> return __b;
> }
> template <class _Type>
> --- 393,399 ----
> template <class _Type>
> static _Type
> __less_than_comparable_requirement_violation(_Type __a, _Type __b) {
> ! if (__a < __b) return __a;
> return __b;
> }
> template <class _Type>
>