This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

rel_ops issues


I've seen Nathan's answer on the FAQ.  I think that it is not correct,
and that libstdc++-v3 needs more work to be a usable library.  That
is, rel_ops is not broken, __normal_iterator needs repair.

I think that the fundamental quote

 * Short summary:  the rel_ops operators cannot be made to play nice.
 * Don't use them.

is wrong.  The rel_ops operators *must* be made to play nice or the
libstdc++-v3 project will have failed to deliver a conforming library.
And they *can* be made to play nice.

The reason that it is correct to have a separate rel_ops namespace is
to permit a programmer to design classes where == and != return something
other than a bool (e.g. a lazy-evaluation expression library where the
result is an expression object).  It is not a license to have the world
break when the ordinary user who is not being tricky adds a using directive.

Please understand what I'm saying: NOT that the rel_ops operators should
be promoted to std, but rather that programs that say

using std::rel_ops::operator!=;

and also use STL classes must work.  Otherwise we have a release-critical
bug: I have tons of C++ that works on about six different compilers that
won't work with gcc 3.0.

Currently many algorithms don't work as they should, e.g. operator== on
vector<T> does not work unless there is an explicit operator!= for T (a
requirement that isn't in the standard).

First, the conflicts between the rel_ops operators and __normal_iterator can
be fixed, by adding more specializations.  See

http://gcc.gnu.org/ml/gcc-bugs/2001-03/msg00822.html

for a suggestion on how to do this.  I have tested it and it works.

Second, STL algorithms that use the != operator on objects other than
"normal iterators" should either include

	using std::rel_ops::operator!=;

or
	using namespace std::relops;

in the function body, so that the algorithms will work right (see bug
c++/2406), or alternatively, all STL algorithms currently using a != b
on objects other than iterators (where we know we have != defined)
should replace it with !(a == b).  For example,

template <class _InputIter1, class _InputIter2>
inline bool equal(_InputIter1 __first1, _InputIter1 __last1,
                  _InputIter2 __first2) {
  __STL_REQUIRES(_InputIter1, _InputIterator);
  __STL_REQUIRES(_InputIter2, _InputIterator);
  __STL_REQUIRES(typename iterator_traits<_InputIter1>::value_type,
                 _EqualityComparable);
  __STL_REQUIRES(typename iterator_traits<_InputIter2>::value_type,
                 _EqualityComparable);
  for ( ; __first1 != __last1; ++__first1, ++__first2)
    if (!(*__first1 == *__first2))
      return false;
  return true;
}

In the original we had

    if (*__first1 != *__first2)
      return false;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]