浏览代码

cmUVHandlePtr: Fix conversion to bool on Oracle Studio compiler

The operator added by commit 17690558c3 (cmUVHandlePtr: Add explicit
conversion to bool, 2023-10-26) works in direct expressions like
`if(foo)` but not compound expressions like `if(foo && ...)`.
Drop the `explicit` mark when compiling with Oracle Studio so we
can at least compile valid code.
Brad King 1 年之前
父节点
当前提交
47fbb29ad7
共有 2 个文件被更改,包括 24 次插入0 次删除
  1. 8 0
      Source/cmUVHandlePtr.h
  2. 16 0
      Tests/CMakeLib/testUVHandlePtr.cxx

+ 8 - 0
Source/cmUVHandlePtr.h

@@ -132,7 +132,15 @@ public:
   uv_handle_ptr_base_(std::nullptr_t) {}
   ~uv_handle_ptr_base_() { this->reset(); }
 
+#if defined(__SUNPRO_CC)
+  // The Oracle Studio compiler recognizes 'explicit operator bool()' in
+  // 'if(foo)' but not 'if(foo && ...)'.  The purpose of 'explicit' here
+  // is to avoid accidental conversion in non-boolean contexts.  Just
+  // leave it out on this compiler so we can compile valid code.
+  operator bool() const;
+#else
   explicit operator bool() const;
+#endif
 
   /**
    * Properly close the handle if needed and sets the inner handle to nullptr

+ 16 - 0
Tests/CMakeLib/testUVHandlePtr.cxx

@@ -7,6 +7,21 @@
 #include "cmGetPipes.h"
 #include "cmUVHandlePtr.h"
 
+static bool testBool()
+{
+  cm::uv_async_ptr async;
+  cm::uv_handle_ptr handle;
+  cm::uv_idle_ptr idle;
+  cm::uv_pipe_ptr pipe;
+  cm::uv_process_ptr process;
+  cm::uv_signal_ptr signal;
+  cm::uv_stream_ptr stream;
+  cm::uv_timer_ptr timer;
+  cm::uv_tty_ptr tty;
+  return !async && !handle && !idle && !pipe && !process && !signal &&
+    !stream && !timer && !tty;
+}
+
 static bool testIdle()
 {
   bool idled = false;
@@ -130,6 +145,7 @@ static bool testWriteCallback()
 int testUVHandlePtr(int, char** const)
 {
   bool passed = true;
+  passed = testBool() && passed;
   passed = testIdle() && passed;
   passed = testTimer() && passed;
   passed = testWriteCallback() && passed;