PR libstdc++/82522 overload map insert functions for rvalues (LWG 2354)

Backport from mainline
2017-10-13  Jonathan Wakely  <jwakely@redhat.com>

	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: r255330
This commit is contained in:
Jonathan Wakely 2017-12-01 17:50:59 +00:00 committed by Jonathan Wakely
parent 2a8f244b91
commit 50748fad7c
9 changed files with 208 additions and 1 deletions

View File

@ -1,5 +1,25 @@
2017-12-01 Jonathan Wakely <jwakely@redhat.com> 2017-12-01 Jonathan Wakely <jwakely@redhat.com>
Backport from mainline
2017-10-13 Jonathan Wakely <jwakely@redhat.com>
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 Backport from mainline
2017-11-23 Jonathan Wakely <jwakely@redhat.com> 2017-11-23 Jonathan Wakely <jwakely@redhat.com>

View File

@ -986,6 +986,12 @@ requirements of the license of GCC.
<listitem><para>Add deleted constructors. <listitem><para>Add deleted constructors.
</para></listitem></varlistentry> </para></listitem></varlistentry>
<varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&DR;#2354">2332</link>:
<emphasis>Unnecessary copying when inserting into maps with braced-init syntax</emphasis>
</term>
<listitem><para>Add overloads of <code>insert</code> taking <code>value_type&amp;&amp;</code> rvalues.
</para></listitem></varlistentry>
<varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&DR;#2399">2399</link>: <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&DR;#2399">2399</link>:
<emphasis><code>shared_ptr</code>'s constructor from <code>unique_ptr</code> should be constrained</emphasis> <emphasis><code>shared_ptr</code>'s constructor from <code>unique_ptr</code> should be constrained</emphasis>
</term> </term>

View File

@ -777,7 +777,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
/** /**
* @brief Attempts to insert a std::pair into the %map. * @brief Attempts to insert a std::pair into the %map.
* @param __x Pair to be inserted (see std::make_pair for easy * @param __x Pair to be inserted (see std::make_pair for easy
* creation of pairs). * creation of pairs).
* *
@ -790,12 +789,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
* first element (the key) is not already present in the %map. * first element (the key) is not already present in the %map.
* *
* Insertion requires logarithmic time. * Insertion requires logarithmic time.
* @{
*/ */
std::pair<iterator, bool> std::pair<iterator, bool>
insert(const value_type& __x) insert(const value_type& __x)
{ return _M_t._M_insert_unique(__x); } { return _M_t._M_insert_unique(__x); }
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2354. Unnecessary copying when inserting into maps with braced-init
std::pair<iterator, bool>
insert(value_type&& __x)
{ return _M_t._M_insert_unique(std::move(__x)); }
template<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -803,6 +809,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(_Pair&& __x) insert(_Pair&& __x)
{ return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); }
#endif #endif
// @}
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
/** /**
@ -839,6 +846,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
* for more on @a hinting. * for more on @a hinting.
* *
* Insertion requires logarithmic time (if the hint is not taken). * Insertion requires logarithmic time (if the hint is not taken).
* @{
*/ */
iterator iterator
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
@ -849,6 +857,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{ return _M_t._M_insert_unique_(__position, __x); } { return _M_t._M_insert_unique_(__position, __x); }
#if __cplusplus >= 201103L #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<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -857,6 +871,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{ return _M_t._M_insert_unique_(__position, { return _M_t._M_insert_unique_(__position,
std::forward<_Pair>(__x)); } std::forward<_Pair>(__x)); }
#endif #endif
// @}
/** /**
* @brief Template function that attempts to insert a range of elements. * @brief Template function that attempts to insert a range of elements.

View File

@ -525,12 +525,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
* thus multiple pairs with the same key can be inserted. * thus multiple pairs with the same key can be inserted.
* *
* Insertion requires logarithmic time. * Insertion requires logarithmic time.
* @{
*/ */
iterator iterator
insert(const value_type& __x) insert(const value_type& __x)
{ return _M_t._M_insert_equal(__x); } { return _M_t._M_insert_equal(__x); }
#if __cplusplus >= 201103L #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<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -538,6 +545,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(_Pair&& __x) insert(_Pair&& __x)
{ return _M_t._M_insert_equal(std::forward<_Pair>(__x)); } { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
#endif #endif
// @}
/** /**
* @brief Inserts a std::pair into the %multimap. * @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 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
* *
* Insertion requires logarithmic time (if the hint is not taken). * Insertion requires logarithmic time (if the hint is not taken).
* @{
*/ */
iterator iterator
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
@ -568,6 +577,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{ return _M_t._M_insert_equal_(__position, __x); } { return _M_t._M_insert_equal_(__position, __x); }
#if __cplusplus >= 201103L #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<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -576,6 +591,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{ return _M_t._M_insert_equal_(__position, { return _M_t._M_insert_equal_(__position,
std::forward<_Pair>(__x)); } std::forward<_Pair>(__x)); }
#endif #endif
// @}
/** /**
* @brief A template function that attempts to insert a range * @brief A template function that attempts to insert a range

View File

@ -578,6 +578,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(const value_type& __x) insert(const value_type& __x)
{ return _M_h.insert(__x); } { return _M_h.insert(__x); }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2354. Unnecessary copying when inserting into maps with braced-init
std::pair<iterator, bool>
insert(value_type&& __x)
{ return _M_h.insert(std::move(__x)); }
template<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -612,6 +618,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(const_iterator __hint, const value_type& __x) insert(const_iterator __hint, const value_type& __x)
{ return _M_h.insert(__hint, __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<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -1467,6 +1479,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(const value_type& __x) insert(const value_type& __x)
{ return _M_h.insert(__x); } { return _M_h.insert(__x); }
iterator
insert(value_type&& __x)
{ return _M_h.insert(std::move(__x)); }
template<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>
@ -1499,6 +1515,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
insert(const_iterator __hint, const value_type& __x) insert(const_iterator __hint, const value_type& __x)
{ return _M_h.insert(__hint, __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<typename _Pair, typename = typename template<typename _Pair, typename = typename
std::enable_if<std::is_constructible<value_type, std::enable_if<std::is_constructible<value_type,
_Pair&&>::value>::type> _Pair&&>::value>::type>

View File

@ -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
// <http://www.gnu.org/licenses/>.
// { dg-do compile { target c++11 } }
#include <map>
struct MoveOnly {
MoveOnly(int) { }
MoveOnly(MoveOnly&&) = default;
};
void
test01()
{
std::map<int, MoveOnly> m;
m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// { dg-do compile { target c++11 } }
#include <map>
struct MoveOnly {
MoveOnly(int) { }
MoveOnly(MoveOnly&&) = default;
};
void
test01()
{
std::multimap<int, MoveOnly> m;
m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// { dg-do compile { target c++11 } }
#include <unordered_map>
struct MoveOnly {
MoveOnly(int) { }
MoveOnly(MoveOnly&&) = default;
};
void
test01()
{
std::unordered_map<int, MoveOnly> m;
m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// { dg-do compile { target c++11 } }
#include <unordered_map>
struct MoveOnly {
MoveOnly(int) { }
MoveOnly(MoveOnly&&) = default;
};
void
test01()
{
std::unordered_multimap<int, MoveOnly> m;
m.insert({1, 2}); // PR libstdc++/82522 - LWG 2354
}