This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: PR 58148 patch
- From: François Dumont <frs dot dumont at gmail dot com>
- To: Paolo Carlini <paolo dot carlini at oracle dot com>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Tue, 27 Aug 2013 22:34:34 +0200
- Subject: Re: PR 58148 patch
- Authentication-results: sourceware.org; auth=none
- References: <521BB025 dot 3040700 at gmail dot com> <521BBD4F dot 1040300 at oracle dot com>
On 08/26/2013 10:40 PM, Paolo Carlini wrote:
On 08/26/2013 09:44 PM, François Dumont wrote:
Hi
The problem exposed in PR 58148 is quite trivial,
std::common_type cannot find a result when instantiated with char*
and const wchar_t*. The attached patch will fix this problem by
dealing with a std::common_type resolution failure even if the
drawback is that following call won't be detected as an invalid one
anymore:
std::vector<int> v;
....
v.insert(v.begin(), reinterpret_cast<char*>(v.data() + 1),
reinterpret_cast<char*>(v.data() + 5)); // Expected failure
For the time being I think we should simply live with this. To be
honest, I think that at this level even the very idea of using
common_type (and relying on the very recent sfinae-friendly feature)
is a bit too much, I would just return true if the type of
addressof(*__other) != container::pointer (modulo const). Thus have a
__foreign_iterator_aux4 taking two container::const_pointer and a fall
back always returning true.
Paolo.
As you seem to be more concern by the usage of experimental
std::common_type than by the usage of const volatile void*I would like
to propose the attached patch. No more usage of common_type but still
able to detect weird issues.
Tested under Linux x86_64 debug mode.
2013-08-27 François Dumont <fdumont@gcc.gnu.org>
PR libstdc++/58148
* include/bits/functions.h: Use const volatile void* as common
type rather than result of std::common_type. Add a fallback method
if conversion to const volatile void* is not possible.
* testsuite/23_containers/vector/modifiers/insert/58148.cc: New.
Ok to commit ?
François
Index: include/debug/functions.h
===================================================================
--- include/debug/functions.h (revision 201966)
+++ include/debug/functions.h (working copy)
@@ -36,7 +36,7 @@
#include <bits/move.h> // for __addressof and addressof
#if __cplusplus >= 201103L
# include <bits/stl_function.h> // for less and greater_equal
-# include <type_traits> // for common_type
+# include <type_traits> // for is_lvalue_reference and __and_
#endif
#include <debug/formatter.h>
@@ -172,19 +172,16 @@
}
#if __cplusplus >= 201103L
+ // Default implementation.
template<typename _Iterator, typename _Sequence,
- typename _InputIterator,
- typename _PointerType1,
- typename _PointerType2>
+ typename _InputIterator>
inline bool
__foreign_iterator_aux4(const _Safe_iterator<_Iterator, _Sequence>& __it,
_InputIterator __other,
- _PointerType1, _PointerType2)
+ const volatile void*, const volatile void*)
{
- typedef typename std::common_type<_PointerType1,
- _PointerType2>::type _PointerType;
- constexpr std::less<_PointerType> __l{};
- constexpr std::greater_equal<_PointerType> __ge{};
+ constexpr std::less<const volatile void*> __l{};
+ constexpr std::greater_equal<const volatile void*> __ge{};
return (__l(std::addressof(*__other),
std::addressof(*(__it._M_get_sequence()->_M_base().begin())))
@@ -192,7 +189,16 @@
std::addressof(*(__it._M_get_sequence()->_M_base().end()
- 1)) + 1));
}
-
+
+ // Fallback when one of the address type cannot be implicitely casted to const
+ // volatile void*.
+ template<typename _Iterator, typename _Sequence,
+ typename _InputIterator>
+ inline bool
+ __foreign_iterator_aux4(const _Safe_iterator<_Iterator, _Sequence>&,
+ _InputIterator, ...)
+ { return true; }
+
template<typename _Iterator, typename _Sequence, typename _InputIterator>
inline bool
__foreign_iterator_aux3(const _Safe_iterator<_Iterator, _Sequence>& __it,
@@ -223,7 +229,7 @@
std::false_type)
{ return true; }
#endif
-
+
/** Checks that iterators do not belong to the same sequence. */
template<typename _Iterator, typename _Sequence, typename _OtherIterator>
inline bool
Index: testsuite/23_containers/vector/modifiers/insert/58148.cc
===================================================================
--- testsuite/23_containers/vector/modifiers/insert/58148.cc (revision 0)
+++ testsuite/23_containers/vector/modifiers/insert/58148.cc (revision 0)
@@ -0,0 +1,35 @@
+// Copyright (C) 2013 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 3, 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 COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+#include <vector>
+
+void
+test01()
+{
+ std::vector<wchar_t> v;
+ char c = 'a';
+ v.insert(v.begin(), &c, &c);
+}
+
+int main()
+{
+ test01();
+ return 0;
+}