[gjs: 4/9] maint: Use 'if constexpr' in templates where possible




commit ee84746eabe9c0207fbc7462e5e4884339ad400d
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 | 6 +++++-
 2 files changed, 6 insertions(+), 3 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..9bcc436d 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -60,7 +60,11 @@ 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
+        // FIXME: should use if constexpr (...), but that doesn't work with
+        // ubsan, which generates a null pointer check making it not a constexpr
+        // anymore: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71962 - Also a
+        // bogus warning, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554
+        auto ref = ref_func;
         this->reset(ptr && ref ? reinterpret_cast<T*>(ref(ptr)) : ptr);
     }
 


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