From 01b2b7a573c086f3e0ea820f78c56a99b73228b3 Mon Sep 17 00:00:00 2001 From: redi Date: Mon, 3 Sep 2018 14:25:12 +0000 Subject: [PATCH] PR libstdc++/78595 implement insertion into maps in terms of emplace C++14 simplified the specification of the generic insert function templates to be equivalent to calling emplace (or emplace_hint). Defining them in terms of emplace takes care of the problems described in PR 78595, ensuring a single conversion to value_type is done at the right time. PR libstdc++/78595 * include/bits/stl_map.h (map::insert(_Pair&&)) (map::insert(const_iterator, _Pair&&)): Do emplace instead of insert. * include/bits/stl_multimap.h (multimap::insert(_Pair&&)) (multimap::insert(const_iterator, _Pair&&)): Likewise. * include/bits/unordered_map.h (unordered_map::insert(_Pair&&)) (unordered_map::insert(const_iterator, _Pair&&)) (unordered_multimap::insert(_Pair&&)) (unordered_multimap::insert(const_iterator, _Pair&&)): Likewise. * testsuite/23_containers/map/modifiers/insert/78595.cc: New test. * testsuite/23_containers/multimap/modifiers/insert/78595.cc: New test. * testsuite/23_containers/unordered_map/modifiers/78595.cc: New test. * testsuite/23_containers/unordered_multimap/modifiers/78595.cc: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@264059 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 17 +++ libstdc++-v3/include/bits/stl_map.h | 21 ++-- libstdc++-v3/include/bits/stl_multimap.h | 20 ++-- libstdc++-v3/include/bits/unordered_map.h | 33 +++--- .../23_containers/map/modifiers/insert/78595.cc | 115 +++++++++++++++++++ .../multimap/modifiers/insert/78595.cc | 115 +++++++++++++++++++ .../23_containers/unordered_map/modifiers/78595.cc | 122 +++++++++++++++++++++ .../unordered_multimap/modifiers/78595.cc | 122 +++++++++++++++++++++ 8 files changed, 523 insertions(+), 42 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc create mode 100644 libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0c6f4084ee3..763da9ea778 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,20 @@ +2018-09-03 Jonathan Wakely + + PR libstdc++/78595 + * include/bits/stl_map.h (map::insert(_Pair&&)) + (map::insert(const_iterator, _Pair&&)): Do emplace instead of insert. + * include/bits/stl_multimap.h (multimap::insert(_Pair&&)) + (multimap::insert(const_iterator, _Pair&&)): Likewise. + * include/bits/unordered_map.h (unordered_map::insert(_Pair&&)) + (unordered_map::insert(const_iterator, _Pair&&)) + (unordered_multimap::insert(_Pair&&)) + (unordered_multimap::insert(const_iterator, _Pair&&)): Likewise. + * testsuite/23_containers/map/modifiers/insert/78595.cc: New test. + * testsuite/23_containers/multimap/modifiers/insert/78595.cc: New test. + * testsuite/23_containers/unordered_map/modifiers/78595.cc: New test. + * testsuite/23_containers/unordered_multimap/modifiers/78595.cc: New + test. + 2018-09-02 François Dumont * include/debug/safe_iterator.h diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index fdd058b060d..6ce9c3e1f60 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -808,12 +808,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_t._M_insert_unique(std::move(__x)); } - template::value>::type> - std::pair + template + __enable_if_t::value, + pair> insert(_Pair&& __x) - { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } + { return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); } #endif // @} @@ -869,13 +868,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __position, value_type&& __x) { return _M_t._M_insert_unique_(__position, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __position, _Pair&& __x) - { return _M_t._M_insert_unique_(__position, - std::forward<_Pair>(__x)); } + { + return _M_t._M_emplace_hint_unique(__position, + std::forward<_Pair>(__x)); + } #endif // @} diff --git a/libstdc++-v3/include/bits/stl_multimap.h b/libstdc++-v3/include/bits/stl_multimap.h index 9357d1db6aa..d4973870763 100644 --- a/libstdc++-v3/include/bits/stl_multimap.h +++ b/libstdc++-v3/include/bits/stl_multimap.h @@ -544,12 +544,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_t._M_insert_equal(std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(_Pair&& __x) - { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); } + { return _M_t._M_emplace_equal(std::forward<_Pair>(__x)); } #endif // @} @@ -589,13 +587,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __position, value_type&& __x) { return _M_t._M_insert_equal_(__position, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __position, _Pair&& __x) - { return _M_t._M_insert_equal_(__position, - std::forward<_Pair>(__x)); } + { + return _M_t._M_emplace_hint_equal(__position, + std::forward<_Pair>(__x)); + } #endif // @} diff --git a/libstdc++-v3/include/bits/unordered_map.h b/libstdc++-v3/include/bits/unordered_map.h index 9a9332f0305..94ce8cdb482 100644 --- a/libstdc++-v3/include/bits/unordered_map.h +++ b/libstdc++-v3/include/bits/unordered_map.h @@ -585,12 +585,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_h.insert(std::move(__x)); } - template::value>::type> - std::pair + template + __enable_if_t::value, + pair> insert(_Pair&& __x) - { return _M_h.insert(std::forward<_Pair>(__x)); } + { return _M_h.emplace(std::forward<_Pair>(__x)); } //@} //@{ @@ -625,12 +624,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, value_type&& __x) { return _M_h.insert(__hint, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __hint, _Pair&& __x) - { return _M_h.insert(__hint, std::forward<_Pair>(__x)); } + { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); } //@} /** @@ -1571,12 +1568,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(value_type&& __x) { return _M_h.insert(std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(_Pair&& __x) - { return _M_h.insert(std::forward<_Pair>(__x)); } + { return _M_h.emplace(std::forward<_Pair>(__x)); } //@} //@{ @@ -1609,12 +1604,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, value_type&& __x) { return _M_h.insert(__hint, std::move(__x)); } - template::value>::type> - iterator + template + __enable_if_t::value, iterator> insert(const_iterator __hint, _Pair&& __x) - { return _M_h.insert(__hint, std::forward<_Pair>(__x)); } + { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); } //@} /** diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc new file mode 100644 index 00000000000..187d165b083 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2018 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 run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::map m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 1 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::map m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 1 ); +} + +struct Key { + int key; + bool operator<(const Key& r) const { return key < r.key; } +}; + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::map, Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 1 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 2 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc new file mode 100644 index 00000000000..2f357fcae95 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2018 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 run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::multimap m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 2 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::multimap m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 2 ); +} + +struct Key { + int key; + bool operator<(const Key& r) const { return key < r.key; } +}; + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::multimap, Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 2 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 4 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc new file mode 100644 index 00000000000..47cd67b86d8 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc @@ -0,0 +1,122 @@ +// Copyright (C) 2018 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 run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_map m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 1 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_map m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 1 ); +} + +struct Key { + int key; + bool operator==(const Key& r) const { return key == r.key; } +}; + +namespace std { + template<> struct hash { + size_t operator()(const Key& k) const { return std::hash()(k.key); } + }; +} + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::unordered_map, std::equal_to, + Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 1 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 2 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc new file mode 100644 index 00000000000..e47c20cad81 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc @@ -0,0 +1,122 @@ +// Copyright (C) 2018 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 run { target c++11 } } + +#include +#include + +void +test01() +{ + struct X { + mutable int conversions = 0; + + operator std::pair() const { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_multimap m; + m.insert(X()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), X()); + VERIFY( m.size() == 2 ); + +} +void +test02() +{ + struct Y { + int conversions = 0; + + operator std::pair() && { + if (++conversions > 1) + throw 1; + return {}; + } + }; + + std::unordered_multimap m; + m.insert(Y()); + VERIFY( m.size() == 1 ); + m.insert(m.begin(), Y()); + VERIFY( m.size() == 2 ); +} + +struct Key { + int key; + bool operator==(const Key& r) const { return key == r.key; } +}; + +namespace std { + template<> struct hash { + size_t operator()(const Key& k) const { return std::hash()(k.key); } + }; +} + +struct Z { + operator std::pair() const { return { { z }, 0 }; } + int z; +}; + +template +struct Alloc +{ + Alloc() = default; + + template + Alloc(const Alloc&) { } + + using value_type = T; + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } + + template + void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } + + template + bool operator==(const Alloc&) { return true; } + + template + bool operator!=(const Alloc&) { return false; } +}; + +void +test03() +{ + std::unordered_multimap, std::equal_to, + Alloc>> m; + m.insert(Z{}); + m.insert(Z{}); + VERIFY( m.size() == 2 ); + m.insert(Z{}); + m.insert(Z{1}); + VERIFY( m.size() == 4 ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} -- 2.11.4.GIT