Skip to content

Commit 23504b1

Browse files
committed
fix: rule of 3 for xtimit
C++ types should not implement move constructors without also implementing move assignment. This also ensures that m_fn is correctly emptied after a move
1 parent 4077bf2 commit 23504b1

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/util/timeit.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,25 @@ class xtimeit {
5555
}
5656
xtimeit(std::function<void(second_duration)> const & fn) : xtimeit(second_duration(0), fn) {} // NOLINT
5757
xtimeit(xtimeit const &) = delete;
58-
xtimeit(xtimeit &&) = default;
58+
xtimeit& operator=(xtimeit const &) = delete;
59+
xtimeit(xtimeit && other) noexcept
60+
: m_threshold(std::move(other.m_threshold)),
61+
m_excluded(std::move(other.m_excluded)),
62+
m_start(std::move(other.m_start)),
63+
m_fn(std::move(other.m_fn)) { // TODO: use `std::exchange(_, nullptr)` in C++14
64+
other.m_fn = nullptr;
65+
}
66+
xtimeit& operator=(xtimeit && other) noexcept {
67+
m_threshold = std::move(other.m_threshold);
68+
m_excluded = std::move(other.m_excluded);
69+
m_start = std::move(other.m_start);
70+
m_fn = std::move(other.m_fn); // TODO: use `std::exchange(_, nullptr)` in C++14
71+
other.m_fn = nullptr;
72+
}
5973
~xtimeit() {
74+
if (!m_fn) return;
6075
auto diff = get_elapsed();
61-
if (diff >= m_threshold && m_fn) {
76+
if (diff >= m_threshold) {
6277
m_fn(diff);
6378
}
6479
}

0 commit comments

Comments
 (0)