From 73f050310177d90d001d90a1e639d59309efd40e Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 4 Oct 2011 20:34:54 +0000 Subject: [PATCH] alloc_traits.h (__alloc_traits::max_size): Define. 2011-10-04 Jonathan Wakely * include/ext/alloc_traits.h (__alloc_traits::max_size): Define. (__alloc_traits::rebind): Define. * include/bits/stl_vector.h: Use them. * testsuite/util/testsuite_allocator.h (SimpleAllocator): Define. * testsuite/23_containers/vector/allocator/minimal.cc: New. * testsuite/23_containers/vector/requirements/dr438/assign_neg.cc: Adjust dg-error line numbers. * testsuite/23_containers/vector/requirements/dr438/insert_neg.cc: Likewise. * testsuite/23_containers/vector/requirements/dr438/ constructor_1_neg.cc: Likewise. * testsuite/23_containers/vector/requirements/dr438/ constructor_2_neg.cc: Likewise. From-SVN: r179523 --- libstdc++-v3/ChangeLog | 16 +++++++ libstdc++-v3/include/bits/stl_vector.h | 5 ++- libstdc++-v3/include/ext/alloc_traits.h | 13 ++++++ .../23_containers/vector/allocator/minimal.cc | 45 +++++++++++++++++++ .../vector/requirements/dr438/assign_neg.cc | 2 +- .../requirements/dr438/constructor_1_neg.cc | 2 +- .../requirements/dr438/constructor_2_neg.cc | 2 +- .../vector/requirements/dr438/insert_neg.cc | 2 +- .../testsuite/util/testsuite_allocator.h | 28 ++++++++++++ 9 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/vector/allocator/minimal.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 6e74692c361..10cfaaf70bf 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,19 @@ +2011-10-04 Jonathan Wakely + + * include/ext/alloc_traits.h (__alloc_traits::max_size): Define. + (__alloc_traits::rebind): Define. + * include/bits/stl_vector.h: Use them. + * testsuite/util/testsuite_allocator.h (SimpleAllocator): Define. + * testsuite/23_containers/vector/allocator/minimal.cc: New. + * testsuite/23_containers/vector/requirements/dr438/assign_neg.cc: + Adjust dg-error line numbers. + * testsuite/23_containers/vector/requirements/dr438/insert_neg.cc: + Likewise. + * testsuite/23_containers/vector/requirements/dr438/ + constructor_1_neg.cc: Likewise. + * testsuite/23_containers/vector/requirements/dr438/ + constructor_2_neg.cc: Likewise. + 2011-10-03 Jonathan Wakely * testsuite/20_util/pointer_traits/pointer_to.cc: Define equality diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 40664209fab..869bcf74489 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -70,7 +70,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template struct _Vector_base { - typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type; + typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template + rebind<_Tp>::other _Tp_alloc_type; typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer pointer; @@ -643,7 +644,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER /** Returns the size() of the largest possible %vector. */ size_type max_size() const _GLIBCXX_NOEXCEPT - { return _M_get_Tp_allocator().max_size(); } + { return _Alloc_traits::max_size(_M_get_Tp_allocator()); } #ifdef __GXX_EXPERIMENTAL_CXX0X__ /** diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h index dbeb95f313c..de13b652521 100644 --- a/libstdc++-v3/include/ext/alloc_traits.h +++ b/libstdc++-v3/include/ext/alloc_traits.h @@ -106,6 +106,7 @@ template using _Base_type::deallocate; using _Base_type::construct; using _Base_type::destroy; + using _Base_type::max_size; private: template @@ -115,6 +116,7 @@ template { }; public: + // overload construct for non-standard pointer types template static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type construct(_Alloc& __a, _Ptr __p, _Args&&... __args) @@ -123,6 +125,7 @@ template std::forward<_Args>(__args)...); } + // overload destroy for non-standard pointer types template static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type destroy(_Alloc& __a, _Ptr __p) @@ -156,6 +159,9 @@ template || noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>())); } + template + struct rebind + { typedef typename _Base_type::template __rebind_alloc<_Tp>::__type other; }; #else typedef typename _Alloc::pointer pointer; @@ -179,6 +185,9 @@ template static void destroy(_Alloc& __a, pointer __p) { __a.destroy(__p); } + static size_type max_size(const _Alloc& __a) + { return __a.max_size(); } + static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; } static void _S_on_swap(_Alloc& __a, _Alloc& __b) @@ -187,6 +196,10 @@ template // 431. Swapping containers with unequal allocators. std::__alloc_swap<_Alloc>::_S_do_it(__a, __b); } + + template + struct rebind + { typedef typename _Alloc::template rebind<_Tp>::other other; }; #endif }; diff --git a/libstdc++-v3/testsuite/23_containers/vector/allocator/minimal.cc b/libstdc++-v3/testsuite/23_containers/vector/allocator/minimal.cc new file mode 100644 index 00000000000..2d56884a436 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/allocator/minimal.cc @@ -0,0 +1,45 @@ +// Copyright (C) 2011 Free Software Foundation +// +// 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 +// . + +// { dg-options "-std=gnu++0x" } + +#include +#include +#include +#include + +struct T { int i; }; + +using __gnu_test::SimpleAllocator; + +template class std::vector>; + +void test01() +{ + bool test __attribute__((unused)) = true; + typedef SimpleAllocator alloc_type; + typedef std::allocator_traits traits_type; + typedef std::vector test_type; + test_type v(alloc_type{}); + VERIFY( v.max_size() == traits_type::max_size(v.get_allocator()) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc index 55b9096d768..00d18b74fd5 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/assign_neg.cc @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1222 } +// { dg-error "no matching" "" { target *-*-* } 1223 } #include diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc index 6ea2c6f1145..34c2ed1c6a8 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_1_neg.cc @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1152 } +// { dg-error "no matching" "" { target *-*-* } 1153 } #include diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc index 75b5bd6d218..563def0e666 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/constructor_2_neg.cc @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1152 } +// { dg-error "no matching" "" { target *-*-* } 1153 } #include #include diff --git a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc index d93de19d486..fec41a39fdb 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/requirements/dr438/insert_neg.cc @@ -18,7 +18,7 @@ // . // { dg-do compile } -// { dg-error "no matching" "" { target *-*-* } 1263 } +// { dg-error "no matching" "" { target *-*-* } 1264 } #include diff --git a/libstdc++-v3/testsuite/util/testsuite_allocator.h b/libstdc++-v3/testsuite/util/testsuite_allocator.h index 57a5a8ae896..454108c6e8e 100644 --- a/libstdc++-v3/testsuite/util/testsuite_allocator.h +++ b/libstdc++-v3/testsuite/util/testsuite_allocator.h @@ -386,6 +386,8 @@ namespace __gnu_test typedef std::integral_constant trait_type; public: + // default allocator_traits::rebind_alloc would select + // uneq_allocator::rebind so we must define rebind here template struct rebind { typedef propagating_allocator other; }; @@ -433,6 +435,32 @@ namespace __gnu_test { return Propagate ? *this : propagating_allocator(); } }; + // Class template supporting the minimal interface that satisfies the + // Allocator requirements, from example in [allocator.requirements] + template + struct SimpleAllocator + { + typedef Tp value_type; + + SimpleAllocator() { } + + template + SimpleAllocator(const SimpleAllocator& other) { } + + Tp *allocate(std::size_t n) + { return std::allocator().allocate(n); } + + void deallocate(Tp *p, std::size_t n) + { std::allocator().deallocate(p, n); } + }; + + template + bool operator==(const SimpleAllocator&, const SimpleAllocator&) + { return true; } + template + bool operator!=(const SimpleAllocator&, const SimpleAllocator&) + { return false; } + #endif template