aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-02-24 06:43:14 -0800
committerChris Robinson <[email protected]>2022-02-24 06:43:14 -0800
commit843a5aac6b0be0e9f17bd73b9036d4c0c1e2c227 (patch)
tree319bbf67b2a39a94c3462e5cd6191228c466c0c3
parentb1559227b0bc46408752de52ca0644d1f89d2671 (diff)
Forward the (un)likely expression being cast to bool
-rw-r--r--common/opthelpers.h11
1 files changed, 7 insertions, 4 deletions
diff --git a/common/opthelpers.h b/common/opthelpers.h
index cb901a63..ae14f1d9 100644
--- a/common/opthelpers.h
+++ b/common/opthelpers.h
@@ -1,6 +1,9 @@
#ifndef OPTHELPERS_H
#define OPTHELPERS_H
+#include <utility>
+
+
#ifdef __has_builtin
#define HAS_BUILTIN __has_builtin
#else
@@ -14,20 +17,20 @@
*/
template<typename T>
constexpr bool likely(T&& expr) noexcept
-{ return __builtin_expect(static_cast<bool>(expr), true); }
+{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), true); }
/* The opposite of likely(), optimizing for the case where the condition is
* false.
*/
template<typename T>
constexpr bool unlikely(T&& expr) noexcept
-{ return __builtin_expect(static_cast<bool>(expr), false); }
+{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), false); }
#else
template<typename T>
-constexpr bool likely(T&& expr) noexcept { return static_cast<bool>(expr); }
+constexpr bool likely(T&& expr) noexcept { return static_cast<bool>(std::forward<T>(expr)); }
template<typename T>
-constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(expr); }
+constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(std::forward<T>(expr)); }
#endif
#define LIKELY(x) (likely(x))
#define UNLIKELY(x) (unlikely(x))