From 937ec71aba5b3c633bd5979d6346254e83f46d1c Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 6 Oct 2016 22:22:35 +0100 Subject: [PATCH] Make std::scoped_allocator_adaptor's OUTERMOST recursive * doc/xml/manual/status_cxx2011.xml: Update status. * include/std/scoped_allocator (__outer_allocator_t, __outermost_type): New helpers for recursive OUTERMOST. (__outermost): Use __outermost_type::_S_outermost. (__do_outermost, scoped_allocator_adaptor::__outermost_type): Remove. (scoped_allocator_adaptor::__outermost_alloc_traits): Use new __outermost_type helper. * testsuite/20_util/scoped_allocator/outermost.cc: New test. From-SVN: r240844 --- libstdc++-v3/ChangeLog | 15 +++ .../doc/xml/manual/status_cxx2011.xml | 5 +- libstdc++-v3/include/std/scoped_allocator | 52 +++++++---- .../testsuite/20_util/scoped_allocator/3.cc | 66 ++++++++++++++ .../20_util/scoped_allocator/outermost.cc | 91 +++++++++++++++++++ 5 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/scoped_allocator/3.cc create mode 100644 libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b3116fc785c..d9fa88af6e9 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2016-10-06 Jonathan Wakely + + * doc/xml/manual/status_cxx2011.xml: Update status. + * include/std/scoped_allocator (__outer_allocator_t, __outermost_type): + New helpers for recursive OUTERMOST. + (__outermost): Use __outermost_type::_S_outermost. + (__do_outermost, scoped_allocator_adaptor::__outermost_type): Remove. + (scoped_allocator_adaptor::__outermost_alloc_traits): Use new + __outermost_type helper. + (scoped_allocator_adaptor::_Constructible): New alias template. + (scoped_allocator_adaptor::scoped_allocator_adaptor<_Outer2>): + Constrain template constructors. + * testsuite/20_util/scoped_allocator/3.cc: New test. + * testsuite/20_util/scoped_allocator/outermost.cc: New test. + 2016-10-05 Jonathan Wakely PR libstdc++/70564 diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml index cf99956b7eb..e1b372d643f 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml @@ -1007,11 +1007,10 @@ particular release. - 20.12.4 Scoped allocator adaptor members - Partial - OUTERMOST is not recursive. + Y + 20.12.5 diff --git a/libstdc++-v3/include/std/scoped_allocator b/libstdc++-v3/include/std/scoped_allocator index 310c85c6507..39762fe444d 100644 --- a/libstdc++-v3/include/std/scoped_allocator +++ b/libstdc++-v3/include/std/scoped_allocator @@ -49,21 +49,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template - inline auto - __do_outermost(_Alloc& __a, int) -> decltype(__a.outer_allocator()) - { return __a.outer_allocator(); } + using __outer_allocator_t + = decltype(std::declval<_Alloc>().outer_allocator()); + + template + struct __outermost_type + { + using type = _Alloc; + static type& _S_outermost(_Alloc& __a) { return __a; } + }; template - inline _Alloc& - __do_outermost(_Alloc& __a, ...) - { return __a; } + struct __outermost_type<_Alloc, __void_t<__outer_allocator_t<_Alloc>>> + : __outermost_type< + typename remove_reference<__outer_allocator_t<_Alloc>>::type + > + { + using __base = __outermost_type< + typename remove_reference<__outer_allocator_t<_Alloc>>::type + >; + + static typename __base::type& + _S_outermost(_Alloc& __a) + { return __base::_S_outermost(__a.outer_allocator()); } + }; - // TODO: make recursive (see note in 20.12.4/1) template - inline auto + inline typename __outermost_type<_Alloc>::type& __outermost(_Alloc& __a) - -> decltype(__do_outermost(__a, 0)) - { return __do_outermost(__a, 0); } + { return __outermost_type<_Alloc>::_S_outermost(__a); } template class scoped_allocator_adaptor; @@ -169,13 +183,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_tie() const noexcept { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); } - template - using __outermost_type = typename - std::decay()))>::type; - template using __outermost_alloc_traits - = allocator_traits<__outermost_type<_Alloc>>; + = allocator_traits::type>; template void @@ -225,6 +235,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...) { } + // Used to constrain constructors to disallow invalid conversions. + template + using _Constructible = typename enable_if< + is_constructible<_OuterAlloc, _Alloc>::value + >::type; + public: typedef _OuterAlloc outer_allocator_type; typedef typename __inner_type::__type inner_allocator_type; @@ -270,7 +286,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { } - template + template> scoped_allocator_adaptor(_Outer2&& __outer, const _InnerAllocs&... __inner) : _OuterAlloc(std::forward<_Outer2>(__outer)), @@ -287,14 +303,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_inner(std::move(__other._M_inner)) { } - template + template> scoped_allocator_adaptor( const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other) : _OuterAlloc(__other.outer_allocator()), _M_inner(__other._M_inner) { } - template + template> scoped_allocator_adaptor( scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other) : _OuterAlloc(std::move(__other.outer_allocator())), diff --git a/libstdc++-v3/testsuite/20_util/scoped_allocator/3.cc b/libstdc++-v3/testsuite/20_util/scoped_allocator/3.cc new file mode 100644 index 00000000000..246122048d8 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/scoped_allocator/3.cc @@ -0,0 +1,66 @@ +// Copyright (C) 2016 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 +// . + +// { dg-do compile { target c++11 } } + +#include +#include + +template +struct alloc +{ + using value_type = T; + alloc() = default; + template + alloc(alloc) { } + T* allocate(std::size_t); + void deallocate(T*, std::size_t); +}; + +template + bool operator==(alloc, alloc) { return true; } + +template + bool operator!=(alloc, alloc) { return false; } + +using scoped = std::scoped_allocator_adaptor>; +using other_alloc = __gnu_test::SimpleAllocator; +using other_scoped = std::scoped_allocator_adaptor; + +using std::is_constructible; + +static_assert( is_constructible::value, + "is_constructible"); +static_assert( is_constructible::value, + "is_constructible"); +static_assert( is_constructible&>::value, + "is_constructible"); +static_assert( is_constructible>::value, + "is_constructible"); +static_assert( is_constructible&>::value, + "is_constructible::type&>"); +static_assert( is_constructible>::value, + "is_constructible::type>"); + +static_assert( !is_constructible::value, + "!is_constructible"); +static_assert( !is_constructible::value, + "!is_constructible"); +static_assert( !is_constructible::value, + "!is_constructible"); +static_assert( !is_constructible::value, + "!is_constructible"); diff --git a/libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc b/libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc new file mode 100644 index 00000000000..af313484def --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc @@ -0,0 +1,91 @@ +// Copyright (C) 2016 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 +// . + +// { dg-do compile { target c++11 } } + +#include + +template +struct alloc +{ + using value_type = T; + alloc() = default; + template + alloc(alloc) { } + T* allocate(std::size_t); + void deallocate(T*, std::size_t); +}; + +template + bool operator==(alloc, alloc) { return true; } + +template + bool operator!=(alloc, alloc) { return false; } + +struct X +{ + using allocator_type = alloc; + X(const allocator_type&); +}; + +template +struct nested_alloc : A +{ + nested_alloc() = default; + template + nested_alloc(nested_alloc) { } + + A& outer_allocator() { return *this; } + + template + void construct(U*, Args&&...) + { + static_assert(!std::is_same::value, + "OUTERMOST should recurse and use alloc to construct X"); + } +}; + +template + bool operator==(nested_alloc l, nested_alloc r) + { return l.outer_allocator() == r.outer_allocator(); } + +template + bool operator!=(nested_alloc l, nested_alloc r) + { return !(l == r); } + +template + using scoped_alloc = std::scoped_allocator_adaptor; + +void +test01() +{ + scoped_alloc>> a; + alignas(X) char buf[sizeof(X)]; + X* p = (X*)buf; + // Test that OUTERMOST is recursive and doesn't just unwrap one level: + a.construct(p); +} + +void +test02() +{ + scoped_alloc>>> a; + alignas(X) char buf[sizeof(X)]; + X* p = (X*)buf; + // Test that OUTERMOST is recursive and doesn't just unwrap one level: + a.construct(p); +} -- 2.30.2