Skip to content

Commit 08d4a65

Browse files
Reporter: use constexpr if over SFINAE
1 parent a200fdc commit 08d4a65

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

src/runner.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//
1818

1919
// C++ headers
20-
#include <type_traits> // for enable_if_t
20+
#include <type_traits> // for std::is_same_v
2121

2222
// libsemigroups headers
2323
#include <libsemigroups/runner.hpp>
@@ -39,28 +39,32 @@ namespace libsemigroups {
3939
using std::chrono::high_resolution_clock;
4040
using std::chrono::system_clock;
4141
using std::chrono::time_point_cast;
42-
template <
43-
typename TimePoint,
44-
std::enable_if_t<std::is_same_v<TimePoint, high_resolution_clock>, bool>
45-
= true>
46-
system_clock::time_point to_system(TimePoint tp) {
47-
return tp;
48-
}
4942

50-
template <
51-
typename TimePoint,
52-
std::enable_if_t<!std::is_same_v<TimePoint, high_resolution_clock>,
53-
bool>
54-
= true>
55-
system_clock::time_point to_system(TimePoint tp) {
56-
// Account for the difference between system_clock and
57-
// high_resolution_clock
58-
auto sys_now = system_clock::now();
59-
auto high_res_now = high_resolution_clock::now();
60-
return time_point_cast<system_clock::duration>(tp - high_res_now
61-
+ sys_now);
43+
// The template is required here, rather than just specifying
44+
// high_resolution_clock::time_point as the parameter type, so that the
45+
// discarded branch of the constexpr if does not get fully checked by the
46+
// compiler.
47+
template <typename TimePoint>
48+
system_clock::time_point to_system_impl(TimePoint const& tp) {
49+
static_assert(
50+
std::is_same_v<TimePoint, high_resolution_clock::time_point>);
51+
52+
if constexpr (std::is_same_v<high_resolution_clock, system_clock>) {
53+
return time_point_cast<system_clock::duration>(tp);
54+
} else {
55+
// Account for the difference between system_clock and
56+
// high_resolution_clock
57+
auto sys_now = system_clock::now();
58+
auto high_res_now = high_resolution_clock::now();
59+
return time_point_cast<system_clock::duration>(tp - high_res_now
60+
+ sys_now);
61+
}
6262
}
6363

64+
system_clock::time_point
65+
to_system(high_resolution_clock::time_point const& tp) {
66+
return to_system_impl(tp);
67+
}
6468
} // namespace
6569

6670
void init_reporter(py::module& m) {

0 commit comments

Comments
 (0)