[libcxx] r180036 - Somehow aligned_union got dropped through the cracks. This adds it. Did a drive-by fix of alignment_of while I was in the neighborhood.

Howard Hinnant hhinnant at apple.com
Mon Apr 22 12:37:49 PDT 2013


Author: hhinnant
Date: Mon Apr 22 14:37:49 2013
New Revision: 180036

URL: http://llvm.org/viewvc/llvm-project?rev=180036&view=rev
Log:
Somehow aligned_union got dropped through the cracks.  This adds it.  Did a drive-by fix of alignment_of while I was in the neighborhood.

Added:
    libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp
Modified:
    libcxx/trunk/include/type_traits
    libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp
    libcxx/trunk/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=180036&r1=180035&r2=180036&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Mon Apr 22 14:37:49 2013
@@ -129,6 +129,7 @@ namespace std
     template <class T> struct alignment_of;
     template <size_t Len, size_t Align = most_stringent_alignment_requirement>
         struct aligned_storage;
+    template <size_t Len, class... Types> struct aligned_union;
 
     template <class T> struct decay;
     template <class... T> struct common_type;
@@ -828,10 +829,8 @@ template <class _Tp> struct _LIBCPP_TYPE
 
 // alignment_of
 
-template <class _Tp> struct __alignment_of {_Tp __lx;};
-
 template <class _Tp> struct _LIBCPP_TYPE_VIS alignment_of
-    : public integral_constant<size_t, __alignof__(__alignment_of<typename remove_all_extents<_Tp>::type>)> {};
+    : public integral_constant<size_t, __alignof__(_Tp)> {};
 
 // aligned_storage
 
@@ -960,6 +959,38 @@ _CREATE_ALIGNED_STORAGE_SPECIALIZATION(0
 
 #undef _CREATE_ALIGNED_STORAGE_SPECIALIZATION
 
+#ifndef _LIBCPP_HAS_NO_VARIADICS
+
+// aligned_union
+
+template <size_t _I0, size_t ..._In>
+struct __static_max;
+
+template <size_t _I0>
+struct __static_max<_I0>
+{
+    static const size_t value = _I0;
+};
+
+template <size_t _I0, size_t _I1, size_t ..._In>
+struct __static_max<_I0, _I1, _In...>
+{
+    static const size_t value = _I0 >= _I1 ? __static_max<_I0, _In...>::value :
+                                             __static_max<_I1, _In...>::value;
+};
+
+template <size_t _Len, class _Type0, class ..._Types>
+struct aligned_union
+{
+    static const size_t alignment_value = __static_max<__alignof__(_Type0),
+                                                       __alignof__(_Types)...>::value;
+    static const size_t __len = __static_max<_Len, sizeof(_Type0),
+                                             sizeof(_Types)...>::value;
+    typedef typename aligned_storage<__len, alignment_value>::type type;
+};
+
+#endif  // _LIBCPP_HAS_NO_VARIADICS
+
 // __promote
 
 template <class _A1, class _A2 = void, class _A3 = void,

Modified: libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp?rev=180036&r1=180035&r2=180036&view=diff
==============================================================================
--- libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp (original)
+++ libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp Mon Apr 22 14:37:49 2013
@@ -92,17 +92,17 @@ int main()
     }
     {
     typedef std::aligned_storage<8>::type T1;
-    static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), "");
+    static_assert(std::alignment_of<T1>::value == 8, "");
     static_assert(sizeof(T1) == 8, "");
     }
     {
     typedef std::aligned_storage<9>::type T1;
-    static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), "");
-    static_assert(sizeof(T1) == (sizeof(long) == 4 ? 12 : 16), "");
+    static_assert(std::alignment_of<T1>::value == 8, "");
+    static_assert(sizeof(T1) == 16, "");
     }
     {
     typedef std::aligned_storage<15>::type T1;
-    static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), "");
+    static_assert(std::alignment_of<T1>::value == 8, "");
     static_assert(sizeof(T1) == 16, "");
     }
     {
@@ -117,7 +117,7 @@ int main()
     }
     {
     typedef std::aligned_storage<10>::type T1;
-    static_assert(std::alignment_of<T1>::value == (sizeof(long) == 4 ? 4 : 8), "");
-    static_assert(sizeof(T1) == (sizeof(long) == 4 ? 12 : 16), "");
+    static_assert(std::alignment_of<T1>::value == 8, "");
+    static_assert(sizeof(T1) == 16, "");
     }
 }

Added: libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp?rev=180036&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp (added)
+++ libcxx/trunk/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp Mon Apr 22 14:37:49 2013
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// type_traits
+
+// aligned_union<size_t Len, class ...Types>
+
+#include <type_traits>
+
+int main()
+{
+#ifndef _LIBCPP_HAS_NO_VARIADICS
+    {
+    typedef std::aligned_union<10, char >::type T1;
+    static_assert(std::alignment_of<T1>::value == 1, "");
+    static_assert(sizeof(T1) == 10, "");
+    }
+    {
+    typedef std::aligned_union<10, short >::type T1;
+    static_assert(std::alignment_of<T1>::value == 2, "");
+    static_assert(sizeof(T1) == 10, "");
+    }
+    {
+    typedef std::aligned_union<10, int >::type T1;
+    static_assert(std::alignment_of<T1>::value == 4, "");
+    static_assert(sizeof(T1) == 12, "");
+    }
+    {
+    typedef std::aligned_union<10, double >::type T1;
+    static_assert(std::alignment_of<T1>::value == 8, "");
+    static_assert(sizeof(T1) == 16, "");
+    }
+    {
+    typedef std::aligned_union<10, short, char >::type T1;
+    static_assert(std::alignment_of<T1>::value == 2, "");
+    static_assert(sizeof(T1) == 10, "");
+    }
+    {
+    typedef std::aligned_union<10, char, short >::type T1;
+    static_assert(std::alignment_of<T1>::value == 2, "");
+    static_assert(sizeof(T1) == 10, "");
+    }
+    {
+    typedef std::aligned_union<2, int, char, short >::type T1;
+    static_assert(std::alignment_of<T1>::value == 4, "");
+    static_assert(sizeof(T1) == 4, "");
+    }
+    {
+    typedef std::aligned_union<2, char, int, short >::type T1;
+    static_assert(std::alignment_of<T1>::value == 4, "");
+    static_assert(sizeof(T1) == 4, "");
+    }
+    {
+    typedef std::aligned_union<2, char, short, int >::type T1;
+    static_assert(std::alignment_of<T1>::value == 4, "");
+    static_assert(sizeof(T1) == 4, "");
+    }
+#endif
+}

Modified: libcxx/trunk/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp?rev=180036&r1=180035&r2=180036&view=diff
==============================================================================
--- libcxx/trunk/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp (original)
+++ libcxx/trunk/test/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp Mon Apr 22 14:37:49 2013
@@ -30,13 +30,13 @@ public:
 
 int main()
 {
-    test_alignment_of<int&, sizeof(long) == 4 ? 4 : 8>();
+    test_alignment_of<int&, 4>();
     test_alignment_of<Class, 1>();
     test_alignment_of<int*, sizeof(long) == 4 ? 4 : 8>();
     test_alignment_of<const int*, sizeof(long) == 4 ? 4 : 8>();
     test_alignment_of<char[3], 1>();
     test_alignment_of<int, 4>();
-    test_alignment_of<double, sizeof(long) == 4 ? 4 : 8>();
+    test_alignment_of<double, 8>();
     test_alignment_of<bool, 1>();
     test_alignment_of<unsigned, 4>();
 }





More information about the cfe-commits mailing list