Use std::ref instead of copying the function parameter… (#290)

* GHA: revise numerous CI jobs. Fix sanitizer on new kernel

* Use std::ref instead of copying the function parameter in std::initializer_list overloads of insert_{or|and}_[c]visit

* Reimplement detail::is_invocable

* Update docs for std::ref(f) changes

---------

Co-authored-by: sdarwin <samuel.d.darwin@gmail.com>
This commit is contained in:
Braden Ganetsky 2024-10-08 10:35:58 -05:00 committed by GitHub
parent 94ab2f0776
commit e214ecdbd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 65 additions and 36 deletions

View File

@ -19,6 +19,7 @@ visits the element if insertion did _not_ take place).
* Added GDB pretty-printers for all containers and iterators. For a container with an allocator that uses fancy pointers, these only work if the proper pretty-printer is written for the fancy pointer type itself.
* Fixed `std::initializer_list` assignment issues for open-addressing containers
({github-pr-url}/277[PR#277^]).
* Allowed non-copyable callables to be passed to the `std::initializer_list` overloads of `insert_{and|or}_[c]visit` for concurrent containers, by internally passing a `std::reference_wrapper` of the callable to the iterator-pair overloads.
== Release 1.86.0

View File

@ -1148,7 +1148,7 @@ template<class F> size_type insert_or_cvisit(std::initializer_list<value_type> i
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_flat_map_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), f);
this->xref:#concurrent_flat_map_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), std::ref(f));
-----
[horizontal]
@ -1246,15 +1246,15 @@ Returns:;; The number of elements inserted.
==== Insert Initializer List and Visit
```c++
template<class F1, class F2>
size_type insert_or_visit(std::initializer_list<value_type> il, F1 f1, F2 f2);
size_type insert_and_visit(std::initializer_list<value_type> il, F1 f1, F2 f2);
template<class F1, class F2>
size_type insert_or_cvisit(std::initializer_list<value_type> il, F1 f1, F2 f2);
size_type insert_and_cvisit(std::initializer_list<value_type> il, F1 f1, F2 f2);
```
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_flat_map_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), f1, f2);
this->xref:#concurrent_flat_map_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), std::ref(f1), std::ref(f2));
-----
[horizontal]

View File

@ -1106,7 +1106,7 @@ template<class F> size_type insert_or_cvisit(std::initializer_list<value_type> i
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_flat_set_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), f);
this->xref:#concurrent_flat_set_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), std::ref(f));
-----
[horizontal]
@ -1222,7 +1222,7 @@ template<class F1, class F2>
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_flat_set_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), f1, f2);
this->xref:#concurrent_flat_set_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), std::ref(f1), std::ref(f2));
-----
[horizontal]

View File

@ -1207,7 +1207,7 @@ template<class F> size_type insert_or_cvisit(std::initializer_list<value_type> i
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_node_map_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), f);
this->xref:#concurrent_node_map_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), std::ref(f));
-----
[horizontal]
@ -1320,15 +1320,15 @@ Returns:;; The number of elements inserted.
==== Insert Initializer List and Visit
```c++
template<class F1, class F2>
size_type insert_or_visit(std::initializer_list<value_type> il, F1 f1, F2 f2);
size_type insert_and_visit(std::initializer_list<value_type> il, F1 f1, F2 f2);
template<class F1, class F2>
size_type insert_or_cvisit(std::initializer_list<value_type> il, F1 f1, F2 f2);
size_type insert_and_cvisit(std::initializer_list<value_type> il, F1 f1, F2 f2);
```
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_node_map_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), f1, f2);
this->xref:#concurrent_node_map_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), std::ref(f1), std::ref(f2));
-----
[horizontal]

View File

@ -1164,7 +1164,7 @@ template<class F> size_type insert_or_cvisit(std::initializer_list<value_type> i
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_node_set_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), f);
this->xref:#concurrent_node_set_insert_iterator_range_or_visit[insert_or_[c\]visit](il.begin(), il.end(), std::ref(f));
-----
[horizontal]
@ -1295,7 +1295,7 @@ template<class F1, class F2>
Equivalent to
[listing,subs="+macros,+quotes"]
-----
this->xref:#concurrent_node_set_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), f1, f2);
this->xref:#concurrent_node_set_insert_iterator_range_and_visit[insert_and_[c\]visit](il.begin(), il.end(), std::ref(f1), std::ref(f2));
-----
[horizontal]

View File

@ -487,7 +487,7 @@ namespace boost {
size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
return this->insert_or_visit(ilist.begin(), ilist.end(), f);
return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class Ty, class F>
@ -520,7 +520,7 @@ namespace boost {
size_type insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_cvisit(ilist.begin(), ilist.end(), f);
return this->insert_or_cvisit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class Ty, class F1, class F2>
@ -559,7 +559,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
return this->insert_and_visit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_visit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class Ty, class F1, class F2>
@ -598,7 +599,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_cvisit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_cvisit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class... Args> BOOST_FORCEINLINE bool emplace(Args&&... args)

View File

@ -478,7 +478,7 @@ namespace boost {
size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_visit(ilist.begin(), ilist.end(), f);
return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F>
@ -520,7 +520,7 @@ namespace boost {
size_type insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_cvisit(ilist.begin(), ilist.end(), f);
return this->insert_or_cvisit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F1, class F2>
@ -569,7 +569,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_visit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_visit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class F1, class F2>
@ -619,7 +620,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_cvisit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_cvisit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class... Args> BOOST_FORCEINLINE bool emplace(Args&&... args)

View File

@ -513,7 +513,7 @@ namespace boost {
size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F)
return this->insert_or_visit(ilist.begin(), ilist.end(), f);
return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F>
@ -567,7 +567,7 @@ namespace boost {
size_type insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_cvisit(ilist.begin(), ilist.end(), f);
return this->insert_or_cvisit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F>
@ -627,7 +627,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F2)
return this->insert_and_visit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_visit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class F1, class F2>
@ -688,7 +689,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_cvisit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_cvisit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class F1, class F2>

View File

@ -504,7 +504,7 @@ namespace boost {
size_type insert_or_visit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_visit(ilist.begin(), ilist.end(), f);
return this->insert_or_visit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F>
@ -567,7 +567,7 @@ namespace boost {
size_type insert_or_cvisit(std::initializer_list<value_type> ilist, F f)
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F)
return this->insert_or_cvisit(ilist.begin(), ilist.end(), f);
return this->insert_or_cvisit(ilist.begin(), ilist.end(), std::ref(f));
}
template <class F>
@ -638,7 +638,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_visit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_visit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class F1, class F2>
@ -710,7 +711,8 @@ namespace boost {
{
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F1)
BOOST_UNORDERED_STATIC_ASSERT_CONST_INVOCABLE(F2)
return this->insert_and_cvisit(ilist.begin(), ilist.end(), f1, f2);
return this->insert_and_cvisit(
ilist.begin(), ilist.end(), std::ref(f1), std::ref(f2));
}
template <class F1, class F2>

View File

@ -13,10 +13,7 @@
#include <boost/config.hpp>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/list.hpp>
#include <functional>
#include <iterator>
#include <type_traits>
#include <boost/unordered/detail/type_traits.hpp>
#define BOOST_UNORDERED_STATIC_ASSERT_INVOCABLE(F) \
static_assert(boost::unordered::detail::is_invocable<F, value_type&>::value, \
@ -79,13 +76,20 @@
namespace boost {
namespace unordered {
namespace detail {
template <class F, class... Args>
struct is_invocable
: std::is_constructible<std::function<void(Args...)>,
std::reference_wrapper<typename std::remove_reference<F>::type> >
template <class...> struct is_invocable_helper : std::false_type
{
};
template <class F, class... Args>
struct is_invocable_helper<
void_t<decltype(std::declval<F>()(std::declval<Args>()...))>, F,
Args...> : std::true_type
{
};
template <class F, class... Args>
using is_invocable = is_invocable_helper<void, F, Args...>;
} // namespace detail
} // namespace unordered

View File

@ -947,6 +947,14 @@ namespace {
}
} iterator_range_insert_and_visit;
struct non_copyable_function
{
non_copyable_function() = default;
non_copyable_function(const non_copyable_function&) = delete;
non_copyable_function(non_copyable_function&&) = default;
template <class... Args> void operator()(Args&&...) const {}
};
template <class X, class GF, class F>
void insert(X*, GF gen_factory, F inserter, test::random_generator rg)
{
@ -1047,6 +1055,9 @@ namespace {
++num_invokes;
}),
init_list.size());
x.insert_or_visit(init_list, non_copyable_function{});
x.insert_or_cvisit(init_list, non_copyable_function{});
});
BOOST_TEST_EQ(num_invokes, (init_list.size() - x.size()) +
@ -1105,6 +1116,11 @@ namespace {
++num_invokes;
}),
init_list.size());
x.insert_and_visit(
init_list, non_copyable_function{}, non_copyable_function{});
x.insert_and_cvisit(
init_list, non_copyable_function{}, non_copyable_function{});
});
BOOST_TEST_EQ(num_inserts, x.size());