From 4022db6d7a8128c1853a6d2533753a3e9bfbd8fd Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Wed, 7 Mar 2007 09:26:49 -0500
Subject: [PATCH] ENH: Enabled support for use_auto_ptr(get_auto_ptr()) syntax
 on HP compiler.

---
 auto_ptr.hxx.in | 66 +++++++++++++++++++++++++++++++++++--------------
 testAutoPtr.cxx |  2 --
 2 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/auto_ptr.hxx.in b/auto_ptr.hxx.in
index c3eca75..f9a68c4 100644
--- a/auto_ptr.hxx.in
+++ b/auto_ptr.hxx.in
@@ -16,11 +16,25 @@
 
 #include <@KWSYS_NAMESPACE@/Configure.hxx>
 
+// The HP compiler cannot handle the conversions necessary to use
+// auto_ptr_ref to pass an auto_ptr returned from one function
+// directly to another function as in use_auto_ptr(get_auto_ptr()).
+// We instead use const_cast to achieve the syntax on that platform.
+// We do not use const_cast on other platforms to maintain the C++
+// standard design and guarantee that if an auto_ptr is bound
+// to a reference-to-const then ownership will be maintained.
+#if defined(__HP_aCC)
+# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 0
+#else
+# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
+#endif
+
 namespace @KWSYS_NAMESPACE@
 {
 
 template <class X> class auto_ptr;
 
+#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
 namespace detail
 {
 // The auto_ptr_ref template is supposed to be a private member of
@@ -38,12 +52,24 @@ template <class Y> struct auto_ptr_ref
   auto_ptr_ref(Y* p, int): p_(p) {}
 };
 }
+#endif
 
 /** C++98 Standard Section 20.4.5 - Template class auto_ptr.  */
 template <class X>
 class auto_ptr
 {
+#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
+  typedef auto_ptr auto_ptr_source;
+  static inline auto_ptr& cast(auto_ptr_source& a) { return a; }
+#else
+  typedef auto_ptr const auto_ptr_source;
+  static inline auto_ptr& cast(auto_ptr_source& a)
+    { return const_cast<auto_ptr&>(a); }
+#endif
+
+  /** The pointer to the object held.  */
   X* x_;
+
 public:
   /** The type of object held by the auto_ptr.  */
   typedef X element_type;
@@ -73,19 +99,19 @@ public:
   explicit auto_ptr(X* p=0) throw(): x_(p)
     {
     }
-
+ 
   /** Construct from another auto_ptr holding an object of the same
       type.  This transfers ownership to the newly constructed
       auto_ptr.  */
-  auto_ptr(auto_ptr& a) throw(): x_(a.release())
+  auto_ptr(auto_ptr_source& a) throw(): x_(cast(a).release())
     {
     }
 
   /** Assign from another auto_ptr holding an object of the same type.
       This transfers ownership to the newly constructed auto_ptr.  */
-  auto_ptr& operator=(auto_ptr& a) throw()
+  auto_ptr& operator=(auto_ptr_source& a) throw()
     {
-    this->reset(a.release());
+    this->reset(cast(a).release());
     return *this;
     }
 
@@ -135,21 +161,6 @@ public:
       }
     }
 
-  /** Construct from an auto_ptr_ref.  This is used when the
-      constructor argument is a call to a function returning an
-      auto_ptr.  */
-  auto_ptr(detail::auto_ptr_ref<X> r) throw(): x_(r.p_)
-    {
-    }
-
-  /** Convert to an auto_ptr_ref.  This is used when a function
-      returning an auto_ptr is the argument to the constructor of
-      another auto_ptr.  */
-  template <class Y> operator detail::auto_ptr_ref<Y>() throw()
-    {
-    return detail::auto_ptr_ref<Y>(this->release(), 1);
-    }
-
   /** Convert to an auto_ptr holding an object of a compatible type.
       This transfers ownership to the returned auto_ptr.  */
   template <class Y> operator auto_ptr<Y>() throw()
@@ -157,6 +168,14 @@ public:
     return auto_ptr<Y>(this->release());
     }
 
+#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
+  /** Construct from an auto_ptr_ref.  This is used when the
+      constructor argument is a call to a function returning an
+      auto_ptr.  */
+  auto_ptr(detail::auto_ptr_ref<X> r) throw(): x_(r.p_)
+    {
+    }
+
   /** Assign from an auto_ptr_ref.  This is used when a function
       returning an auto_ptr is passed on the right-hand-side of an
       assignment.  */
@@ -165,6 +184,15 @@ public:
     this->reset(r.p_);
     return *this;
     }
+
+  /** Convert to an auto_ptr_ref.  This is used when a function
+      returning an auto_ptr is the argument to the constructor of
+      another auto_ptr.  */
+  template <class Y> operator detail::auto_ptr_ref<Y>() throw()
+    {
+    return detail::auto_ptr_ref<Y>(this->release(), 1);
+    }
+#endif
 };
 
 } // namespace @KWSYS_NAMESPACE@
diff --git a/testAutoPtr.cxx b/testAutoPtr.cxx
index 5f5c798..c6515d9 100644
--- a/testAutoPtr.cxx
+++ b/testAutoPtr.cxx
@@ -115,14 +115,12 @@ int testAutoPtr(int, char*[])
            "auto_ptr did not release ownership to called function");
     }
 
-#if !defined(__HP_aCC)
     {
     int received = function_call(generate_auto_ptr_A());
     ASSERT(received,
            "auto_ptr in called function did not take ownership "
            "from factory function");
     }
-#endif
 
 #if 0
     // Is this allowed by the standard?
-- 
GitLab