aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2018-09-03 15:25:12 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2018-09-03 15:25:12 +0100
commitbc62e155e412fd2230ec0538e2bef86211d26301 (patch)
treed1599d3dc442dce5d10470f835ebc341956a59ae /libstdc++-v3
parenta6b75a69dae0cfaa9085708073ebd1853e0e6102 (diff)
downloadgcc-bc62e155e412fd2230ec0538e2bef86211d26301.zip
gcc-bc62e155e412fd2230ec0538e2bef86211d26301.tar.gz
gcc-bc62e155e412fd2230ec0538e2bef86211d26301.tar.bz2
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. From-SVN: r264059
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/ChangeLog17
-rw-r--r--libstdc++-v3/include/bits/stl_map.h21
-rw-r--r--libstdc++-v3/include/bits/stl_multimap.h20
-rw-r--r--libstdc++-v3/include/bits/unordered_map.h33
-rw-r--r--libstdc++-v3/testsuite/23_containers/map/modifiers/insert/78595.cc115
-rw-r--r--libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/78595.cc115
-rw-r--r--libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/78595.cc122
-rw-r--r--libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/78595.cc122
8 files changed, 523 insertions, 42 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 0c6f408..763da9e 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,20 @@
+2018-09-03 Jonathan Wakely <jwakely@redhat.com>
+
+ 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 <fdumont@gcc.gnu.org>
* include/debug/safe_iterator.h
diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h
index fdd058b..6ce9c3e 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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- std::pair<iterator, bool>
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair>::value,
+ pair<iterator, bool>>
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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair>::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 9357d1d..d497387 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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair>::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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair&&>::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 9a9332f..94ce8cd 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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- std::pair<iterator, bool>
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair&&>::value,
+ pair<iterator, bool>>
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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair&&>::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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair&&>::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<typename _Pair, typename = typename
- std::enable_if<std::is_constructible<value_type,
- _Pair&&>::value>::type>
- iterator
+ template<typename _Pair>
+ __enable_if_t<is_constructible<value_type, _Pair&&>::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 0000000..187d165
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <map>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ struct X {
+ mutable int conversions = 0;
+
+ operator std::pair<const int, int>() const {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::map<int, int> 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<const int, int>() && {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::map<int, int> 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 Key, int>() const { return { { z }, 0 }; }
+ int z;
+};
+
+template<typename T>
+struct Alloc
+{
+ Alloc() = default;
+
+ template<typename U>
+ Alloc(const Alloc<U>&) { }
+
+ using value_type = T;
+
+ T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+
+ void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
+
+ template<typename U>
+ void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; }
+
+ template<typename U>
+ bool operator==(const Alloc<U>&) { return true; }
+
+ template<typename U>
+ bool operator!=(const Alloc<U>&) { return false; }
+};
+
+void
+test03()
+{
+ std::map<Key, int, std::less<Key>, Alloc<std::pair<const Key, int>>> 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 0000000..2f357fc
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <map>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ struct X {
+ mutable int conversions = 0;
+
+ operator std::pair<const int, int>() const {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::multimap<int, int> 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<const int, int>() && {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::multimap<int, int> 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 Key, int>() const { return { { z }, 0 }; }
+ int z;
+};
+
+template<typename T>
+struct Alloc
+{
+ Alloc() = default;
+
+ template<typename U>
+ Alloc(const Alloc<U>&) { }
+
+ using value_type = T;
+
+ T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+
+ void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
+
+ template<typename U>
+ void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; }
+
+ template<typename U>
+ bool operator==(const Alloc<U>&) { return true; }
+
+ template<typename U>
+ bool operator!=(const Alloc<U>&) { return false; }
+};
+
+void
+test03()
+{
+ std::multimap<Key, int, std::less<Key>, Alloc<std::pair<const Key, int>>> 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 0000000..47cd67b
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <unordered_map>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ struct X {
+ mutable int conversions = 0;
+
+ operator std::pair<const int, int>() const {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::unordered_map<int, int> 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<const int, int>() && {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::unordered_map<int, int> 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<Key> {
+ size_t operator()(const Key& k) const { return std::hash<int>()(k.key); }
+ };
+}
+
+struct Z {
+ operator std::pair<const Key, int>() const { return { { z }, 0 }; }
+ int z;
+};
+
+template<typename T>
+struct Alloc
+{
+ Alloc() = default;
+
+ template<typename U>
+ Alloc(const Alloc<U>&) { }
+
+ using value_type = T;
+
+ T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+
+ void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
+
+ template<typename U>
+ void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; }
+
+ template<typename U>
+ bool operator==(const Alloc<U>&) { return true; }
+
+ template<typename U>
+ bool operator!=(const Alloc<U>&) { return false; }
+};
+
+void
+test03()
+{
+ std::unordered_map<Key, int, std::hash<Key>, std::equal_to<Key>,
+ Alloc<std::pair<const Key, int>>> 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 0000000..e47c20c
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <unordered_map>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ struct X {
+ mutable int conversions = 0;
+
+ operator std::pair<const int, int>() const {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::unordered_multimap<int, int> 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<const int, int>() && {
+ if (++conversions > 1)
+ throw 1;
+ return {};
+ }
+ };
+
+ std::unordered_multimap<int, int> 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<Key> {
+ size_t operator()(const Key& k) const { return std::hash<int>()(k.key); }
+ };
+}
+
+struct Z {
+ operator std::pair<const Key, int>() const { return { { z }, 0 }; }
+ int z;
+};
+
+template<typename T>
+struct Alloc
+{
+ Alloc() = default;
+
+ template<typename U>
+ Alloc(const Alloc<U>&) { }
+
+ using value_type = T;
+
+ T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
+
+ void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
+
+ template<typename U>
+ void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; }
+
+ template<typename U>
+ bool operator==(const Alloc<U>&) { return true; }
+
+ template<typename U>
+ bool operator!=(const Alloc<U>&) { return false; }
+};
+
+void
+test03()
+{
+ std::unordered_multimap<Key, int, std::hash<Key>, std::equal_to<Key>,
+ Alloc<std::pair<const Key, int>>> 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();
+}