[gjs: 7/18] jsapi-util: Make SFINAE on constructor to work with clang++




commit 232aeed57253879a6b007fbe312093670198ade0
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Oct 6 16:43:37 2020 +0200

    jsapi-util: Make SFINAE on constructor to work with clang++
    
    As per C++ standard is perfectly legal for the compiler to check
    template definitions both during instantation (so looking at potential
    issues) and after their actual usage, so while g++ is more liberal on this
    and will not try to define functions which are disabled when allocating
    the template, clang++ will do it.
    
    So, in the case of using a void pointer to hold GjsAutoPointer it will
    complain when using void[] type.
    
    To avoid this, let's change the array constructor definition to use an
    implicit template value, while we still check it properly as part of the
    enable_if check, so that we'll eventually have the same result.
    
    Also fix other compilation issues due to clang being a bit more
    stricter.

 gjs/jsapi-util.h              | 9 +++++----
 test/gjs-test-jsapi-utils.cpp | 7 +++++++
 2 files changed, 12 insertions(+), 4 deletions(-)
---
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index c71e8cfc..34ffc987 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -71,9 +71,10 @@ struct GjsAutoPointer {
 
     constexpr GjsAutoPointer(Ptr ptr = nullptr)  // NOLINT(runtime/explicit)
         : m_ptr(ptr) {}
-    template <typename U = T,
-              typename std::enable_if_t<std::is_array_v<U>, int> = 0>
-    explicit constexpr GjsAutoPointer(Tp ptr[]) : m_ptr(ptr) {}
+    template <typename U, typename = std::enable_if_t<std::is_same_v<U, Tp> &&
+                                                      std::is_array_v<T>>>
+    explicit constexpr GjsAutoPointer(U ptr[]) : m_ptr(ptr) {}
+
     constexpr GjsAutoPointer(Ptr ptr, const GjsAutoTakeOwnership&)
         : GjsAutoPointer(ptr) {
         m_ptr = copy();
@@ -194,7 +195,7 @@ using GjsAutoUnref = GjsAutoPointer<T, void, g_object_unref, g_object_ref>;
 template <typename V, typename T>
 constexpr void GjsAutoPointerDeleter(T v) {
     if constexpr (std::is_array_v<V>)
-        delete[] v;
+        delete[] reinterpret_cast<std::remove_extent_t<V>*>(v);
     else
         delete v;
 }
diff --git a/test/gjs-test-jsapi-utils.cpp b/test/gjs-test-jsapi-utils.cpp
index 1974a51a..6d3f0dd6 100644
--- a/test/gjs-test-jsapi-utils.cpp
+++ b/test/gjs-test-jsapi-utils.cpp
@@ -264,7 +264,14 @@ static void test_gjs_autopointer_assign_operator_self_object() {
 
     GjsAutoTestObject autoptr(ptr);
 
+#if defined(__clang__)
+#    pragma clang diagnostic push
+#    pragma clang diagnostic ignored "-Wself-assign"
+#endif
     autoptr = autoptr;
+#if defined(__clang__)
+#    pragma clang diagnostic pop
+#endif
 
     g_assert(autoptr == ptr);
     g_assert_cmpuint(test_gjs_autopointer_refcount(ptr), ==, 1);


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