Skip to content

Commit

Permalink
Consider template args for default constructor noexceptness
Browse files Browse the repository at this point in the history
This adds a helper to detect the default growth policies that are
noexcept when default initialized with 0.
  • Loading branch information
pfent committed Aug 27, 2024
1 parent 2b7dd4a commit 33f41b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
8 changes: 7 additions & 1 deletion include/tsl/bhopscotch_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ class bhopscotch_map {
/*
* Constructors
*/
bhopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
bhopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0 &&
std::is_nothrow_default_constructible<Hash>::value &&
std::is_nothrow_default_constructible<KeyEqual>::value &&
std::is_nothrow_default_constructible<Allocator>::value &&
(std::is_nothrow_constructible<GrowthPolicy, std::size_t&>::value ||
hh::is_noexcept_on_zero_init<GrowthPolicy>::value))
: bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit bhopscotch_map(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down
8 changes: 7 additions & 1 deletion include/tsl/bhopscotch_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ class bhopscotch_set {
/*
* Constructors
*/
bhopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
bhopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0 &&
std::is_nothrow_default_constructible<Hash>::value &&
std::is_nothrow_default_constructible<KeyEqual>::value &&
std::is_nothrow_default_constructible<Allocator>::value &&
(std::is_nothrow_constructible<GrowthPolicy, std::size_t&>::value ||
hh::is_noexcept_on_zero_init<GrowthPolicy>::value))
: bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit bhopscotch_set(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down
13 changes: 13 additions & 0 deletions include/tsl/hopscotch_growth_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ class prime_growth_policy {
"The type of m_iprime is not big enough.");
};

/**
* SFINAE helper to detect growth policies which can be noexcept-initialized
* with a zero min bucket count.
*/
template<typename>
struct is_noexcept_on_zero_init : std::false_type {};
template<std::size_t GrowthFactor>
struct is_noexcept_on_zero_init<power_of_two_growth_policy<GrowthFactor>> : std::true_type {};
template<class GrowthFactor>
struct is_noexcept_on_zero_init<mod_growth_policy<GrowthFactor>> : std::true_type {};
template<>
struct is_noexcept_on_zero_init<prime_growth_policy> : std::true_type {};

} // namespace hh
} // namespace tsl

Expand Down
8 changes: 7 additions & 1 deletion include/tsl/hopscotch_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ class hopscotch_map {
/*
* Constructors
*/
hopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
hopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0 &&
std::is_nothrow_default_constructible<Hash>::value &&
std::is_nothrow_default_constructible<KeyEqual>::value &&
std::is_nothrow_default_constructible<Allocator>::value &&
(std::is_nothrow_constructible<GrowthPolicy, std::size_t&>::value ||
hh::is_noexcept_on_zero_init<GrowthPolicy>::value))
: hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit hopscotch_map(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down
8 changes: 7 additions & 1 deletion include/tsl/hopscotch_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ class hopscotch_set {
/*
* Constructors
*/
hopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : hopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
hopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0 &&
std::is_nothrow_default_constructible<Hash>::value &&
std::is_nothrow_default_constructible<KeyEqual>::value &&
std::is_nothrow_default_constructible<Allocator>::value &&
(std::is_nothrow_constructible<GrowthPolicy, std::size_t&>::value ||
hh::is_noexcept_on_zero_init<GrowthPolicy>::value))
: hopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit hopscotch_set(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down

0 comments on commit 33f41b2

Please sign in to comment.