diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f001b9a9111..937e20517a5 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,25 @@ 2017-12-01 Jonathan Wakely + Backport from mainline + 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. + Backport from mainline 2017-11-23 Jonathan Wakely diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 4ec74949bfb..157b2845463 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -986,6 +986,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 30339536f57..ea0e3277738 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -777,7 +777,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). * @@ -790,12 +789,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> @@ -803,6 +809,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER insert(_Pair&& __x) { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } #endif + // @} #if __cplusplus >= 201103L /** @@ -839,6 +846,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * for more on @a hinting. * * Insertion requires logarithmic time (if the hint is not taken). + * @{ */ iterator #if __cplusplus >= 201103L @@ -849,6 +857,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> @@ -857,6 +871,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 7dc22a96a59..68ffea3adba 100644 --- a/libstdc++-v3/include/bits/stl_multimap.h +++ b/libstdc++-v3/include/bits/stl_multimap.h @@ -525,12 +525,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> @@ -538,6 +545,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. @@ -558,6 +566,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 @@ -568,6 +577,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> @@ -576,6 +591,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 4ef30ccccd6..dd881d46213 100644 --- a/libstdc++-v3/include/bits/unordered_map.h +++ b/libstdc++-v3/include/bits/unordered_map.h @@ -578,6 +578,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> @@ -612,6 +618,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> @@ -1467,6 +1479,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> @@ -1499,6 +1515,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 +}