Skip to content

Commit

Permalink
Enable noexcept for special member functions
Browse files Browse the repository at this point in the history
The hopscotch_hash container already was nothrow move constructible,
but was missing the conditional noexcept for the default constructor,
swap, and move assignment.
  • Loading branch information
pfent committed Aug 18, 2024
1 parent 72d947e commit 3981e98
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions include/tsl/bhopscotch_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class bhopscotch_map {
/*
* Constructors
*/
bhopscotch_map() : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
bhopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit bhopscotch_map(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down Expand Up @@ -414,7 +414,7 @@ class bhopscotch_map {
return m_ht.erase(key, precalculated_hash);
}

void swap(bhopscotch_map& other) { other.m_ht.swap(m_ht); }
void swap(bhopscotch_map& other) noexcept(noexcept(other.m_ht.swap(m_ht))) { other.m_ht.swap(m_ht); }

/*
* Lookup
Expand Down Expand Up @@ -785,7 +785,7 @@ class bhopscotch_map {
return !operator==(lhs, rhs);
}

friend void swap(bhopscotch_map& lhs, bhopscotch_map& rhs) { lhs.swap(rhs); }
friend void swap(bhopscotch_map& lhs, bhopscotch_map& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }

private:
ht m_ht;
Expand Down
6 changes: 3 additions & 3 deletions include/tsl/bhopscotch_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class bhopscotch_set {
/*
* Constructors
*/
bhopscotch_set() : bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
bhopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit bhopscotch_set(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down Expand Up @@ -309,7 +309,7 @@ class bhopscotch_set {
return m_ht.erase(key, precalculated_hash);
}

void swap(bhopscotch_set& other) { other.m_ht.swap(m_ht); }
void swap(bhopscotch_set& other) noexcept(noexcept(other.m_ht.swap(m_ht))) { other.m_ht.swap(m_ht); }

/*
* Lookup
Expand Down Expand Up @@ -597,7 +597,7 @@ class bhopscotch_set {
return !operator==(lhs, rhs);
}

friend void swap(bhopscotch_set& lhs, bhopscotch_set& rhs) { lhs.swap(rhs); }
friend void swap(bhopscotch_set& lhs, bhopscotch_set& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }

private:
ht m_ht;
Expand Down
8 changes: 6 additions & 2 deletions include/tsl/hopscotch_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ class hopscotch_hash : private Hash, private KeyEqual, private GrowthPolicy {
return *this;
}

hopscotch_hash& operator=(hopscotch_hash&& other) {
hopscotch_hash& operator=(hopscotch_hash&& other) noexcept(noexcept(other.swap(*this))) {
other.swap(*this);
other.clear();

Expand Down Expand Up @@ -1035,7 +1035,11 @@ class hopscotch_hash : private Hash, private KeyEqual, private GrowthPolicy {
return 0;
}

void swap(hopscotch_hash& other) {
void swap(hopscotch_hash& other) noexcept(std::is_nothrow_swappable<Hash>::value &&
std::is_nothrow_swappable<KeyEqual>::value &&
std::is_nothrow_swappable<GrowthPolicy>::value &&
std::is_nothrow_swappable<buckets_container_type>::value &&
std::is_nothrow_swappable<overflow_container_type>::value) {
using std::swap;

swap(static_cast<Hash&>(*this), static_cast<Hash&>(other));
Expand Down
6 changes: 3 additions & 3 deletions include/tsl/hopscotch_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class hopscotch_map {
/*
* Constructors
*/
hopscotch_map() : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
hopscotch_map() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit hopscotch_map(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down Expand Up @@ -424,7 +424,7 @@ class hopscotch_map {
return m_ht.erase(key, precalculated_hash);
}

void swap(hopscotch_map& other) { other.m_ht.swap(m_ht); }
void swap(hopscotch_map& other) noexcept(noexcept(other.m_ht.swap(m_ht))) { other.m_ht.swap(m_ht); }

/*
* Lookup
Expand Down Expand Up @@ -783,7 +783,7 @@ class hopscotch_map {
return !operator==(lhs, rhs);
}

friend void swap(hopscotch_map& lhs, hopscotch_map& rhs) { lhs.swap(rhs); }
friend void swap(hopscotch_map& lhs, hopscotch_map& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }

private:
ht m_ht;
Expand Down
6 changes: 3 additions & 3 deletions include/tsl/hopscotch_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class hopscotch_set {
/*
* Constructors
*/
hopscotch_set() : hopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}
hopscotch_set() noexcept(ht::DEFAULT_INIT_BUCKETS_SIZE == 0) : hopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {}

explicit hopscotch_set(size_type bucket_count, const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual(),
Expand Down Expand Up @@ -323,7 +323,7 @@ class hopscotch_set {
return m_ht.erase(key, precalculated_hash);
}

void swap(hopscotch_set& other) { other.m_ht.swap(m_ht); }
void swap(hopscotch_set& other) noexcept(noexcept(other.m_ht.swap(m_ht))) { other.m_ht.swap(m_ht); }

/*
* Lookup
Expand Down Expand Up @@ -600,7 +600,7 @@ class hopscotch_set {
return !operator==(lhs, rhs);
}

friend void swap(hopscotch_set& lhs, hopscotch_set& rhs) { lhs.swap(rhs); }
friend void swap(hopscotch_set& lhs, hopscotch_set& rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }

private:
ht m_ht;
Expand Down
7 changes: 7 additions & 0 deletions tests/hopscotch_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,4 +1510,11 @@ BOOST_AUTO_TEST_CASE(test_precalculated_hash) {
BOOST_CHECK_EQUAL(map.erase(4, map.hash_function()(2)), 0);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_noexcept, HSet, test_types) {
static_assert(std::is_nothrow_default_constructible<HSet>::value, "");
static_assert(std::is_nothrow_move_constructible<HSet>::value, "");
static_assert(std::is_nothrow_move_assignable<HSet>::value, "");
static_assert(std::is_nothrow_swappable<HSet>::value, "");
}

BOOST_AUTO_TEST_SUITE_END()
7 changes: 7 additions & 0 deletions tests/hopscotch_set_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert_transparent_hint, HSet,
BOOST_CHECK_EQUAL(*otherIt, 2);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_noexcept, HSet, test_types) {
static_assert(std::is_nothrow_default_constructible<HSet>::value, "");
static_assert(std::is_nothrow_move_constructible<HSet>::value, "");
static_assert(std::is_nothrow_move_assignable<HSet>::value, "");
static_assert(std::is_nothrow_swappable<HSet>::value, "");
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 3981e98

Please sign in to comment.