[gjs] utils-inl: Use constexpr checks for int-to-pointer conversions
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] utils-inl: Use constexpr checks for int-to-pointer conversions
- Date: Sun, 9 Aug 2020 17:58:04 +0000 (UTC)
commit ce48c3044f72f137dc172eb811050ef546aabc2f
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Sun Aug 9 01:23:40 2020 +0200
utils-inl: Use constexpr checks for int-to-pointer conversions
gi/utils-inl.h | 30 +++++++++++++-----------------
1 file changed, 13 insertions(+), 17 deletions(-)
---
diff --git a/gi/utils-inl.h b/gi/utils-inl.h
index a258cef6..4e060c76 100644
--- a/gi/utils-inl.h
+++ b/gi/utils-inl.h
@@ -9,30 +9,26 @@
#include <stdint.h>
-#include <type_traits>
+#include <type_traits> // IWYU pragma: keep
template <typename T>
-inline std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>, void*>
-gjs_int_to_pointer(T v) {
- return reinterpret_cast<void*>(static_cast<intptr_t>(v));
-}
+constexpr void* gjs_int_to_pointer(T v) {
+ static_assert(std::is_integral_v<T>, "Need integer value");
-template <typename T>
-inline std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>, void*>
-gjs_int_to_pointer(T v) {
- return reinterpret_cast<void*>(static_cast<uintptr_t>(v));
+ if constexpr (std::is_signed_v<T>)
+ return reinterpret_cast<void*>(static_cast<intptr_t>(v));
+ else
+ return reinterpret_cast<void*>(static_cast<uintptr_t>(v));
}
template <typename T>
-inline std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>, T>
-gjs_pointer_to_int(void* p) {
- return static_cast<T>(reinterpret_cast<intptr_t>(p));
-}
+constexpr T gjs_pointer_to_int(void* p) {
+ static_assert(std::is_integral_v<T>, "Need integer value");
-template <typename T>
-inline std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>, T>
-gjs_pointer_to_int(void* p) {
- return static_cast<T>(reinterpret_cast<uintptr_t>(p));
+ if constexpr (std::is_signed_v<T>)
+ return static_cast<T>(reinterpret_cast<intptr_t>(p));
+ else
+ return static_cast<T>(reinterpret_cast<uintptr_t>(p));
}
template <>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]