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
This commit is contained in:
Jonathan Wakely 2018-09-03 15:25:12 +01:00 committed by Jonathan Wakely
parent a6b75a69da
commit bc62e155e4
8 changed files with 523 additions and 42 deletions

View File

@ -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

View File

@ -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
// @}

View File

@ -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
// @}

View File

@ -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)); }
//@}
/**

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}