From 3b0dd4fea23a695df1fb9a7e5c7017f9d1f1ea84 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 13 Oct 2017 13:39:32 +0100 Subject: [PATCH] PR libstdc++/82522 overload map insert functions for rvalues (LWG 2354) PR libstdc++/82522 * doc/xml/manual/intro.xml: Document LWG 2354 changes. * include/bits/stl_map.h (map::insert(value_type&&)) (map::insert(const_iterator, value_type&&)): Add overload for rvalues. * include/bits/stl_multimap.h (multimap::insert(value_type&&)) (multimap::insert(const_iterator, value_type&&)): Likewise. * include/bits/unordered_map.h (unordered_map::insert(value_type&&)) (unordered_map::insert(const_iterator, value_type&&)) (unordered_multimap::insert(value_type&&)) (unordered_multimap::insert(const_iterator, value_type&&)): Likewise. * testsuite/23_containers/map/modifiers/insert/dr2354.cc: New test. * testsuite/23_containers/multimap/modifiers/insert/dr2354.cc: New test. * testsuite/23_containers/unordered_map/insert/dr2354.cc: New test. * testsuite/23_containers/unordered_multimap/insert/dr2354.cc: New test. From-SVN: r253725 --- libstdc++-v3/ChangeLog | 17 ++++++++++ libstdc++-v3/doc/xml/manual/intro.xml | 6 ++++ libstdc++-v3/include/bits/stl_map.h | 17 +++++++++- libstdc++-v3/include/bits/stl_multimap.h | 16 ++++++++++ libstdc++-v3/include/bits/unordered_map.h | 22 +++++++++++++ .../map/modifiers/insert/dr2354.cc | 32 +++++++++++++++++++ .../multimap/modifiers/insert/dr2354.cc | 32 +++++++++++++++++++ .../unordered_map/insert/dr2354.cc | 32 +++++++++++++++++++ .../unordered_multimap/insert/dr2354.cc | 32 +++++++++++++++++++ 9 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/23_containers/map/modifiers/insert/dr2354.cc create mode 100644 libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/dr2354.cc create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_map/insert/dr2354.cc create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_multimap/insert/dr2354.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1b8432da660..64196884160 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,22 @@ 2017-10-13 Jonathan Wakely + PR libstdc++/82522 + * doc/xml/manual/intro.xml: Document LWG 2354 changes. + * include/bits/stl_map.h (map::insert(value_type&&)) + (map::insert(const_iterator, value_type&&)): Add overload for rvalues. + * include/bits/stl_multimap.h (multimap::insert(value_type&&)) + (multimap::insert(const_iterator, value_type&&)): Likewise. + * include/bits/unordered_map.h (unordered_map::insert(value_type&&)) + (unordered_map::insert(const_iterator, value_type&&)) + (unordered_multimap::insert(value_type&&)) + (unordered_multimap::insert(const_iterator, value_type&&)): Likewise. + * testsuite/23_containers/map/modifiers/insert/dr2354.cc: New test. + * testsuite/23_containers/multimap/modifiers/insert/dr2354.cc: New + test. + * testsuite/23_containers/unordered_map/insert/dr2354.cc: New test. + * testsuite/23_containers/unordered_multimap/insert/dr2354.cc: New + test. + PR libstdc++/82481 * include/std/mutex (call_once): Suppress clang-tidy warnings about dangling references. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 782817e0698..3b243e57c8b 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -988,6 +988,12 @@ requirements of the license of GCC. Add deleted constructors. + 2332: + Unnecessary copying when inserting into maps with braced-init syntax + + Add overloads of insert taking value_type&& rvalues. + + 2399: shared_ptr's constructor from unique_ptr should be constrained diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index 0e8a98a96c1..bad6020ef47 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -778,7 +778,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER /** * @brief Attempts to insert a std::pair into the %map. - * @param __x Pair to be inserted (see std::make_pair for easy * creation of pairs). * @@ -791,12 +790,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * first element (the key) is not already present in the %map. * * Insertion requires logarithmic time. + * @{ */ std::pair insert(const value_type& __x) { return _M_t._M_insert_unique(__x); } #if __cplusplus >= 201103L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + std::pair + insert(value_type&& __x) + { return _M_t._M_insert_unique(std::move(__x)); } + template::value>::type> @@ -804,6 +810,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(_Pair&& __x) { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } #endif + // @} #if __cplusplus >= 201103L /** @@ -840,6 +847,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * for more on @a hinting. * * Insertion requires logarithmic time (if the hint is not taken). + * @{ */ iterator #if __cplusplus >= 201103L @@ -850,6 +858,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { return _M_t._M_insert_unique_(__position, __x); } #if __cplusplus >= 201103L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + iterator + insert(const_iterator __position, value_type&& __x) + { return _M_t._M_insert_unique_(__position, std::move(__x)); } + template::value>::type> @@ -858,6 +872,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { return _M_t._M_insert_unique_(__position, std::forward<_Pair>(__x)); } #endif + // @} /** * @brief Template function that attempts to insert a range of elements. diff --git a/libstdc++-v3/include/bits/stl_multimap.h b/libstdc++-v3/include/bits/stl_multimap.h index 7e3cea48a47..6f5cb7a47db 100644 --- a/libstdc++-v3/include/bits/stl_multimap.h +++ b/libstdc++-v3/include/bits/stl_multimap.h @@ -526,12 +526,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * thus multiple pairs with the same key can be inserted. * * Insertion requires logarithmic time. + * @{ */ iterator insert(const value_type& __x) { return _M_t._M_insert_equal(__x); } #if __cplusplus >= 201103L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + iterator + insert(value_type&& __x) + { return _M_t._M_insert_equal(std::move(__x)); } + template::value>::type> @@ -539,6 +546,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(_Pair&& __x) { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); } #endif + // @} /** * @brief Inserts a std::pair into the %multimap. @@ -559,6 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints * * Insertion requires logarithmic time (if the hint is not taken). + * @{ */ iterator #if __cplusplus >= 201103L @@ -569,6 +578,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { return _M_t._M_insert_equal_(__position, __x); } #if __cplusplus >= 201103L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + iterator + insert(const_iterator __position, value_type&& __x) + { return _M_t._M_insert_equal_(__position, std::move(__x)); } + template::value>::type> @@ -577,6 +592,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { return _M_t._M_insert_equal_(__position, std::forward<_Pair>(__x)); } #endif + // @} /** * @brief A template function that attempts to insert a range diff --git a/libstdc++-v3/include/bits/unordered_map.h b/libstdc++-v3/include/bits/unordered_map.h index df1302c80c0..2fd4cd54962 100644 --- a/libstdc++-v3/include/bits/unordered_map.h +++ b/libstdc++-v3/include/bits/unordered_map.h @@ -579,6 +579,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const value_type& __x) { return _M_h.insert(__x); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + std::pair + insert(value_type&& __x) + { return _M_h.insert(std::move(__x)); } + template::value>::type> @@ -613,6 +619,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, const value_type& __x) { return _M_h.insert(__hint, __x); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + iterator + insert(const_iterator __hint, value_type&& __x) + { return _M_h.insert(__hint, std::move(__x)); } + template::value>::type> @@ -1468,6 +1480,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const value_type& __x) { return _M_h.insert(__x); } + iterator + insert(value_type&& __x) + { return _M_h.insert(std::move(__x)); } + template::value>::type> @@ -1500,6 +1516,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(const_iterator __hint, const value_type& __x) { return _M_h.insert(__hint, __x); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2354. Unnecessary copying when inserting into maps with braced-init + iterator + insert(const_iterator __hint, value_type&& __x) + { return _M_h.insert(__hint, std::move(__x)); } + template::value>::type> diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/dr2354.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/dr2354.cc new file mode 100644 index 00000000000..338d9fd3f1e --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/dr2354.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2017 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 + +struct MoveOnly { + MoveOnly(int) { } + MoveOnly(MoveOnly&&) = default; +}; + +void +test01() +{ + std::map m; + m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354 +} diff --git a/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/dr2354.cc b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/dr2354.cc new file mode 100644 index 00000000000..ca743ec4ce9 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multimap/modifiers/insert/dr2354.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2017 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 + +struct MoveOnly { + MoveOnly(int) { } + MoveOnly(MoveOnly&&) = default; +}; + +void +test01() +{ + std::multimap m; + m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354 +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/insert/dr2354.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/insert/dr2354.cc new file mode 100644 index 00000000000..fe5356594c3 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/insert/dr2354.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2017 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 + +struct MoveOnly { + MoveOnly(int) { } + MoveOnly(MoveOnly&&) = default; +}; + +void +test01() +{ + std::unordered_map m; + m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354 +} diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/insert/dr2354.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/insert/dr2354.cc new file mode 100644 index 00000000000..5a27242c4e0 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/insert/dr2354.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2017 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 + +struct MoveOnly { + MoveOnly(int) { } + MoveOnly(MoveOnly&&) = default; +}; + +void +test01() +{ + std::unordered_multimap m; + m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354 +} -- 2.30.2