[gjs/mozjs78: 27/50] maint: Use 'if constexpr' in templates where possible




commit 1683f4be6098b1fb3b8ab86eb614ae20ef2de28e
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Aug 1 14:12:19 2020 -0700

    maint: Use 'if constexpr' in templates where possible
    
    This is a new C++ feature that helps the compiler generate better code
    for branches that can be computed at compile time.

 gi/arg.cpp       |  3 +--
 gjs/jsapi-util.h | 17 +++++++++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 220cc8b8..ccc6cc80 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -514,8 +514,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool hashtable_int_key(
          i < static_cast<Container>(std::numeric_limits<IntType>::min())))
         *out_of_range = true;
 
-    /* Use if constexpr with c++17 */
-    if (std::is_signed<IntType>())
+    if constexpr (std::is_signed_v<IntType>)
         *pointer_out = GINT_TO_POINTER(i);
     else
         *pointer_out = GUINT_TO_POINTER(i);
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index c43085d0..b2ff920a 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -60,8 +60,21 @@ struct GjsAutoPointer : std::unique_ptr<T, decltype(free_func)> {
         : GjsAutoPointer::unique_ptr(ptr, *free_func) {}
     GjsAutoPointer(T* ptr, const GjsAutoTakeOwnership&)
         : GjsAutoPointer(nullptr) {
-        auto ref = ref_func;  // use if constexpr ... once we're on C++17
-        this->reset(ptr && ref ? reinterpret_cast<T*>(ref(ptr)) : ptr);
+// clang-format off
+#if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+_Pragma("GCC diagnostic push")
+_Pragma("GCC diagnostic ignored \"-Waddress\"")
+#endif
+        // Bogus warning, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554
+        if constexpr (ref_func != nullptr) {
+#if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+_Pragma("GCC diagnostic pop")
+#endif
+            if (ptr)
+                ptr = reinterpret_cast<T*>(ref_func(ptr));
+        }
+        // clang-format on
+        this->reset(ptr);
     }
 
     operator T*() const { return this->get(); }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]