-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libstdc++: Fix conversions to key/value types for hash table insertio…
…n [PR115285] The conversions to key_type and value_type that are performed when inserting into _Hashtable need to be fixed to do any required conversions explicitly. The current code assumes that conversions from the parameter to the key_type or value_type can be done implicitly, which isn't necessarily true. Remove the _S_forward_key function which doesn't handle all cases and either forward the parameter if it already has type cv key_type, or explicitly construct a temporary of type key_type. Similarly, the _ConvertToValueType specialization for maps doesn't handle all cases either, for std::pair arguments only some value categories are handled. Remove _ConvertToValueType and for the _M_insert function for unique keys, either forward the argument unchanged or explicitly construct a temporary of type value_type. For the _M_insert overload for non-unique keys we don't need any conversion at all, we can just forward the argument directly to where we construct a node. libstdc++-v3/ChangeLog: PR libstdc++/115285 * include/bits/hashtable.h (_Hashtable::_S_forward_key): Remove. (_Hashtable::_M_insert_unique_aux): Replace _S_forward_key with a static_cast to a type defined using conditional_t. (_Hashtable::_M_insert): Replace _ConvertToValueType with a static_cast to a type defined using conditional_t. * include/bits/hashtable_policy.h (_ConvertToValueType): Remove. * testsuite/23_containers/unordered_map/insert/115285.cc: New test. * testsuite/23_containers/unordered_set/insert/115285.cc: New test. * testsuite/23_containers/unordered_set/96088.cc: Adjust expected number of allocations.
- Loading branch information
Showing
5 changed files
with
88 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
libstdc++-v3/testsuite/23_containers/unordered_map/insert/115285.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// { dg-do run { target c++11 } } | ||
|
||
// PR libstdc++/115285 | ||
|
||
#include <unordered_map> | ||
#include <iterator> | ||
#include <testsuite_hooks.h> | ||
|
||
struct Pair | ||
{ | ||
explicit operator std::pair<const int, int>() const&& { return {1, 2}; } | ||
}; | ||
|
||
void | ||
test01() | ||
{ | ||
Pair p[2]; | ||
auto mi = std::make_move_iterator(p); | ||
auto me = std::make_move_iterator(p+2); | ||
std::unordered_map<int, int> m(mi, me); | ||
VERIFY( m.size() == 1 ); | ||
} | ||
|
||
struct K { | ||
explicit K(int) noexcept { } | ||
bool operator==(const K&) const { return true; } | ||
}; | ||
|
||
template<> struct std::hash<K> { | ||
std::size_t operator()(const K&) const { return 0ul; } | ||
}; | ||
|
||
void | ||
test02() | ||
{ | ||
const std::pair<int, int> p[2]{{1,2}, {3,4}}; | ||
auto mi = std::make_move_iterator(p); | ||
auto me = std::make_move_iterator(p+2); | ||
std::unordered_map<K, int> m(mi, me); | ||
VERIFY( m.size() == 1 ); | ||
} | ||
|
||
int main() | ||
{ | ||
test01(); | ||
test02(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
libstdc++-v3/testsuite/23_containers/unordered_set/insert/115285.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// { dg-do run { target c++11 } } | ||
|
||
// PR libstdc++/115285 | ||
|
||
#include <unordered_set> | ||
#include <testsuite_hooks.h> | ||
|
||
struct K { | ||
explicit K(int) noexcept { } | ||
bool operator==(const K&) const { return true; } | ||
}; | ||
|
||
template<> struct std::hash<K> { | ||
std::size_t operator()(const K&) const { return 0ul; } | ||
}; | ||
|
||
void | ||
test01() | ||
{ | ||
int i[2]{1, 2}; | ||
std::unordered_set<K> s(i, i+2); | ||
VERIFY( s.size() == 1 ); | ||
} | ||
|
||
int main() | ||
{ | ||
test01(); | ||
} |