This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] ADL issues: string
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 01 Dec 2005 03:45:11 +0100
- Subject: [Patch] ADL issues: string
Hi,
the below (versus v7, but clearly a candidate for mainline in a few
days), is what I need to solve the issues affecting basic_string. A
separate one will deal with vector, probably more complex...
Anyway, besides avoiding operator+ on __normal_iterator, trivial, I also
need to add an overload of operator- for left and right of the same type
(see also the testcase), besides the one for unequal types already
present (useful for mixes of iterator and const_iterator). The issue
dates back to the implementation of DR 179.
Can you see any problem with this? Otherwise I will commit it tomorrow
and go ahead with vector.
Tested x86-linux.
Paolo.
///////////////////
2005-12-01 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (insert(iterator, _CharT),
erase(iterator), erase(iterator, iterator)): Avoid troubles
with ADL, user defined operators and __normal_iterator.
* include/bits/stl_iterator.h (operator-(const __normal_iterator
<_Iterator, _Container>&, const __normal_iterator<_Iterator,
_Container>&)): Add overload for left and right iterators of
the same type.
* include/debug/safe_iterator.h (operator-(const _Safe_iterator
<_Iterator, _Sequence>&, const _Safe_iterator<_Iterator,
_Sequence>&)): Likewise.
* testsuite/21_strings/basic_string/3.cc: New.
Index: include/bits/basic_string.h
===================================================================
--- include/bits/basic_string.h (revision 107718)
+++ include/bits/basic_string.h (working copy)
@@ -890,7 +890,7 @@
const size_type __pos = __p - _M_ibegin();
_M_replace_aux(__pos, size_type(0), size_type(1), __c);
this->_M_set_leaked();
- return _M_ibegin() + __pos;
+ return iterator(this->_M_data() + __pos);
}
/**
@@ -931,7 +931,7 @@
const size_type __pos = __position - _M_ibegin();
this->_M_erase(__pos, size_type(1));
this->_M_set_leaked();
- return _M_ibegin() + __pos;
+ return iterator(this->_M_data() + __pos);
}
/**
@@ -951,7 +951,7 @@
const size_type __pos = __first - _M_ibegin();
this->_M_erase(__pos, __last - __first);
this->_M_set_leaked();
- return _M_ibegin() + __pos;
+ return iterator(this->_M_data() + __pos);
}
/**
Index: include/bits/stl_iterator.h
===================================================================
--- include/bits/stl_iterator.h (revision 106945)
+++ include/bits/stl_iterator.h (working copy)
@@ -808,6 +808,12 @@
{ return __lhs.base() - __rhs.base(); }
template<typename _Iterator, typename _Container>
+ inline typename __normal_iterator<_Iterator, _Container>::difference_type
+ operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
+ const __normal_iterator<_Iterator, _Container>& __rhs)
+ { return __lhs.base() - __rhs.base(); }
+
+ template<typename _Iterator, typename _Container>
inline __normal_iterator<_Iterator, _Container>
operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
__n, const __normal_iterator<_Iterator, _Container>& __i)
Index: include/debug/safe_iterator.h
===================================================================
--- include/debug/safe_iterator.h (revision 106945)
+++ include/debug/safe_iterator.h (working copy)
@@ -608,6 +608,22 @@
}
template<typename _Iterator, typename _Sequence>
+ inline typename _Safe_iterator<_Iterator, _Sequence>::difference_type
+ operator-(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
+ const _Safe_iterator<_Iterator, _Sequence>& __rhs)
+ {
+ _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
+ _M_message(__msg_distance_bad)
+ ._M_iterator(__lhs, "lhs")
+ ._M_iterator(__rhs, "rhs"));
+ _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
+ _M_message(__msg_distance_different)
+ ._M_iterator(__lhs, "lhs")
+ ._M_iterator(__rhs, "rhs"));
+ return __lhs.base() - __rhs.base();
+ }
+
+ template<typename _Iterator, typename _Sequence>
inline _Safe_iterator<_Iterator, _Sequence>
operator+(typename _Safe_iterator<_Iterator,_Sequence>::difference_type __n,
const _Safe_iterator<_Iterator, _Sequence>& __i)
Index: testsuite/21_strings/basic_string/3.cc
===================================================================
--- testsuite/21_strings/basic_string/3.cc (revision 0)
+++ testsuite/21_strings/basic_string/3.cc (revision 0)
@@ -0,0 +1,46 @@
+// 2005-12-01 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2005 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING. If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// { dg-do compile }
+
+#include <string>
+
+namespace N
+{
+ struct X { };
+
+ template<typename T>
+ X operator+(T, std::size_t)
+ { return X(); }
+
+ template<typename T>
+ X operator-(T, T)
+ { return X(); }
+}
+
+int main()
+{
+ std::basic_string<N::X> s(5, N::X());
+
+ s.erase(s.begin());
+ s.erase(s.begin(), s.end());
+ s.insert(s.begin(), N::X());
+ s.replace(s.begin(), s.end(), s.begin(), s.end());
+}