[libc++] Implement LWG 2911 - add an is_aggregate type-trait
authorEric Fiselier <eric@efcs.ca>
Wed, 12 Apr 2017 23:08:46 +0000 (23:08 +0000)
committerEric Fiselier <eric@efcs.ca>
Wed, 12 Apr 2017 23:08:46 +0000 (23:08 +0000)
Summary:
This patch implements http://cplusplus.github.io/LWG/lwg-defects.html#2911.

I'm putting this up for review until __is_aggregate is added to clang (See D31513)

Reviewers: mclow.lists

Reviewed By: mclow.lists

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D31515

llvm-svn: 300126

libcxx/include/__config
libcxx/include/type_traits
libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/missing_is_aggregate_trait.fail.cpp [new file with mode: 0644]
libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_aggregate.pass.cpp [new file with mode: 0644]
libcxx/www/cxx1z_status.html

index b9d47ac..6f5fc3c 100644 (file)
 
 #ifdef __GNUC__
 #define _GNUC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
+// The _GNUC_VER_NEW macro better represents the new GCC versioning scheme
+// introduced in GCC 5.0.
+#define _GNUC_VER_NEW (_GNUC_VER * 10 + __GNUC_PATCHLEVEL__)
 #else
 #define _GNUC_VER 0
+#define _GNUC_VER_NEW 0
 #endif
 
 #define _LIBCPP_VERSION 5000
@@ -1061,7 +1065,7 @@ _LIBCPP_FUNC_VIS extern "C" void __sanitizer_annotate_contiguous_container(
 #endif
 
 #if !__has_builtin(__builtin_addressof) && _GNUC_VER < 700
-#define _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
+# define _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
 #endif
 
 #if !defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
@@ -1097,6 +1101,10 @@ _LIBCPP_FUNC_VIS extern "C" void __sanitizer_annotate_contiguous_container(
 # define _LIBCPP_HAS_NO_DEDUCTION_GUIDES
 #endif
 
+#if !__has_keyword(__is_aggregate) && (_GNUC_VER_NEW < 7001)
+# define _LIBCPP_HAS_NO_IS_AGGREGATE
+#endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP_CONFIG
index c092239..9db4d66 100644 (file)
@@ -97,6 +97,7 @@ namespace std
     template <class T> struct is_polymorphic;
     template <class T> struct is_abstract;
     template <class T> struct is_final; // C++14
+    template <class T> struct is_aggregate; // C++17
 
     template <class T, class... Args> struct is_constructible;
     template <class T>                struct is_default_constructible;
@@ -286,6 +287,8 @@ namespace std
         = is_abstract<T>::value;                                         // C++17
       template <class T> constexpr bool is_final_v
         = is_final<T>::value;                                            // C++17
+      template <class T> constexpr bool is_aggregate_v
+        = is_aggregate<T>::value;                                        // C++17
       template <class T> constexpr bool is_signed_v
         = is_signed<T>::value;                                           // C++17
       template <class T> constexpr bool is_unsigned_v
@@ -1336,6 +1339,19 @@ template <class _Tp> _LIBCPP_CONSTEXPR bool is_final_v
     = is_final<_Tp>::value;
 #endif
 
+// is_aggregate
+#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_IS_AGGREGATE)
+
+template <class _Tp> struct _LIBCPP_TEMPLATE_VIS
+is_aggregate : public integral_constant<bool, __is_aggregate(_Tp)> {};
+
+#if !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
+template <class _Tp>
+constexpr bool is_aggregate_v = is_aggregate<_Tp>::value;
+#endif
+
+#endif // _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_IS_AGGREGATE)
+
 // is_base_of
 
 #ifdef _LIBCPP_HAS_IS_BASE_OF
diff --git a/libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/missing_is_aggregate_trait.fail.cpp b/libcxx/test/libcxx/utilities/meta/meta.unary/meta.unary.prop/missing_is_aggregate_trait.fail.cpp
new file mode 100644 (file)
index 0000000..e3e083b
--- /dev/null
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <type_traits>
+
+// template <class T> struct is_aggregate;
+// template <class T> constexpr bool is_aggregate_v = is_aggregate<T>::value;
+
+#include <type_traits>
+
+int main ()
+{
+#ifdef _LIBCPP_HAS_NO_IS_AGGREGATE
+  // This should not compile when _LIBCPP_HAS_NO_IS_AGGREGATE is defined.
+  bool b = __is_aggregate(void);
+  ((void)b);
+#else
+#error Forcing failure...
+#endif
+}
diff --git a/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_aggregate.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary/meta.unary.prop/is_aggregate.pass.cpp
new file mode 100644 (file)
index 0000000..9c72d4d
--- /dev/null
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <type_traits>
+
+// template <class T> struct is_aggregate;
+// template <class T> constexpr bool is_aggregate_v = is_aggregate<T>::value;
+
+#include <type_traits>
+#include "test_macros.h"
+
+template <class T>
+void test_true()
+{
+#if !defined(_LIBCPP_HAS_NO_IS_AGGREGATE)
+    static_assert( std::is_aggregate<T>::value, "");
+    static_assert( std::is_aggregate<const T>::value, "");
+    static_assert( std::is_aggregate<volatile T>::value, "");
+    static_assert( std::is_aggregate<const volatile T>::value, "");
+    static_assert( std::is_aggregate_v<T>, "");
+    static_assert( std::is_aggregate_v<const T>, "");
+    static_assert( std::is_aggregate_v<volatile T>, "");
+    static_assert( std::is_aggregate_v<const volatile T>, "");
+#endif
+}
+
+template <class T>
+void test_false()
+{
+#if !defined(_LIBCPP_HAS_NO_IS_AGGREGATE)
+    static_assert(!std::is_aggregate<T>::value, "");
+    static_assert(!std::is_aggregate<const T>::value, "");
+    static_assert(!std::is_aggregate<volatile T>::value, "");
+    static_assert(!std::is_aggregate<const volatile T>::value, "");
+    static_assert(!std::is_aggregate_v<T>, "");
+    static_assert(!std::is_aggregate_v<const T>, "");
+    static_assert(!std::is_aggregate_v<volatile T>, "");
+    static_assert(!std::is_aggregate_v<const volatile T>, "");
+#endif
+}
+
+struct Aggregate {};
+struct HasCons { HasCons(int); };
+struct HasPriv {
+  void PreventUnusedPrivateMemberWarning();
+private:
+  int x;
+};
+struct Union { int x; void* y; };
+
+
+int main ()
+{
+  {
+    test_false<void>();
+    test_false<int>();
+    test_false<void*>();
+    test_false<void()>();
+    test_false<void() const>();
+    test_false<void(Aggregate::*)(int) const>();
+    test_false<Aggregate&>();
+    test_false<HasCons>();
+    test_false<HasPriv>();
+  }
+  {
+    test_true<Aggregate>();
+    test_true<Aggregate[]>();
+    test_true<Aggregate[42][101]>();
+    test_true<Union>();
+  }
+}
index 1dc5174..36c2250 100644 (file)
        <tr><td><a href="http://wg21.link/LWG2904">2904</a></td><td>Make variant move-assignment more exception safe</td><td>Kona</td><td></td></tr>
        <tr><td><a href="http://wg21.link/LWG2905">2905</a></td><td>is_constructible_v&lt;unique_ptr&lt;P, D&gt;, P, D const &amp;&gt; should be false when D is not copy constructible</td><td>Kona</td><td></td></tr>
        <tr><td><a href="http://wg21.link/LWG2908">2908</a></td><td>The less-than operator for shared pointers could do more</td><td>Kona</td><td></td></tr>
-       <tr><td><a href="http://wg21.link/LWG2911">2911</a></td><td>An is_aggregate type trait is needed</td><td>Kona</td><td></td></tr>
+       <tr><td><a href="http://wg21.link/LWG2911">2911</a></td><td>An is_aggregate type trait is needed</td><td>Kona</td><td>Complete</td></tr>
        <tr><td><a href="http://wg21.link/LWG2921">2921</a></td><td>packaged_task and type-erased allocators</td><td>Kona</td><td></td></tr>
        <tr><td><a href="http://wg21.link/LWG2934">2934</a></td><td>optional&lt;const T&gt; doesn't compare with T</td><td>Kona</td><td>Complete</td></tr>
 <!--