<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 31, 2014 at 3:33 PM, Michael Gottesman <span dir="ltr"><<a href="mailto:mgottesman@apple.com" target="_blank">mgottesman@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: mgottesman<br>
Date: Wed Dec 31 17:33:18 2014<br>
New Revision: 225053<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=225053&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=225053&view=rev</a><br>
Log:<br>
Add an ArrayRef upcasting constructor from ArrayRef<U*> -> ArrayRef<T*> where T is a base of U.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ADT/ArrayRef.h<br>
    llvm/trunk/unittests/ADT/ArrayRefTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/ArrayRef.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=225053&r1=225052&r2=225053&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=225053&r1=225052&r2=225053&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/ArrayRef.h Wed Dec 31 17:33:18 2014<br>
@@ -112,6 +112,16 @@ namespace llvm {<br>
                  std::is_convertible<U *const *, T const *>::value>::type* = 0)<br>
       : Data(A.data()), Length(A.size()) {}<br>
<br>
+    /// Construct an ArrayRef<T*> from an ArrayRef<U*> where T is a super class<br>
+    /// of U. This uses SFINAE to ensure that only ArrayRefs with this property<br>
+    /// can be converted. This is an upcasting constructor.<br>
+    template <typename U><br>
+    ArrayRef(const ArrayRef<U> &A,<br>
+             typename std::enable_if<std::is_base_of<<br>
+                 typename std::remove_pointer<T>::type,<br>
+                 typename std::remove_pointer<U>::type>::value>::type * = 0)<br>
+        : Data(reinterpret_cast<T const *>(A.data())), Length(A.size()) {}<br>
+<br>
     /// @}<br>
     /// @name Simple Operations<br>
     /// @{<br>
<br>
Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=225053&r1=225052&r2=225053&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=225053&r1=225052&r2=225053&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)<br>
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Wed Dec 31 17:33:18 2014<br>
@@ -90,4 +90,39 @@ TEST(ArrayRefTest, ConstConvert) {<br>
   a = ArrayRef<int *>(A);<br>
 }<br>
<br>
+struct A {<br>
+  int data;<br>
+<br>
+  A() : data(0) {}<br>
+};<br>
+<br>
+struct B : A {<br>
+  int data2;<br>
+<br>
+  B() : A(), data2(0) {}<br>
+};<br>
+<br>
+TEST(ArrayRefTest, UpcastConvert) {<br>
+  B Data[5];<br>
+<br>
+  for (unsigned i = 0, e = 5; i != e; ++i) {<br>
+    Data[i].data = i + 5;<br>
+    Data[i].data2 = i + 30;<br>
+  }<br>
+<br>
+  B *DataPtrs[5];<br>
+  for (unsigned i = 0, e = 5; i != e; ++i) {<br>
+    DataPtrs[i] = &Data[i];<br>
+  }<br>
+<br>
+  ArrayRef<B *> BArray(DataPtrs, 5);<br>
+  ArrayRef<A *> AArray(BArray);<br>
+<br>
+  EXPECT_TRUE(AArray.size() == 5);<br>
+  for (unsigned i = 0, e = 5; i != e; ++i) {<br>
+    A *a = AArray[i];<br>
+    EXPECT_TRUE(a->data == int(i + 5));<br>
+  }<br>
+}<br>
+<br></blockquote><div><br></div><div>Maybe some static_assert+!is_convertible checks to demonstrate that the SFINAE is behaving correctly and disallowing other conversions?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 } // end anonymous namespace<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>