type_traits: Add the trivial is_union and is_class; add the __is_union_or_class exten...
authorPaolo Carlini <pcarlini@suse.de>
Fri, 25 Feb 2005 18:17:06 +0000 (18:17 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Fri, 25 Feb 2005 18:17:06 +0000 (18:17 +0000)
2005-02-25  Paolo Carlini  <pcarlini@suse.de>

* include/tr1/type_traits: Add the trivial is_union and is_class;
add the __is_union_or_class extension.
(is_enum, is_empty): Use the latter.
* include/tr1/type_traits_fwd.h: Add __is_union_or_class.
* testsuite/testsuite_tr1.h: Add UnionType; trivial formatting
fixes.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_union_or_class/is_union_or_class.cc: New.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_union_or_class/typedefs.cc: Likewise.

From-SVN: r95541

libstdc++-v3/ChangeLog
libstdc++-v3/include/tr1/type_traits
libstdc++-v3/include/tr1/type_traits_fwd.h
libstdc++-v3/testsuite/testsuite_tr1.h
libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/is_union_or_class.cc [new file with mode: 0644]
libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/typedefs.cc [new file with mode: 0644]

index 9d342b40d4f3f954337ba732f9fa0f333c7bff8f..3982be6402d87eaed16428117a553dea25248c86 100644 (file)
@@ -1,3 +1,16 @@
+2005-02-25  Paolo Carlini  <pcarlini@suse.de>
+
+       * include/tr1/type_traits: Add the trivial is_union and is_class;
+       add the __is_union_or_class extension.
+       (is_enum, is_empty): Use the latter.
+       * include/tr1/type_traits_fwd.h: Add __is_union_or_class.
+       * testsuite/testsuite_tr1.h: Add UnionType; trivial formatting
+       fixes.
+       * testsuite/tr1/4_metaprogramming/composite_type_traits/
+       is_union_or_class/is_union_or_class.cc: New.
+       * testsuite/tr1/4_metaprogramming/composite_type_traits/
+       is_union_or_class/typedefs.cc: Likewise.
+
 2005-02-24  Benjamin Kosnik  <bkoz@redhat.com>
 
        * testsuite/tr1/6_containers/unordered/instantiate/hash.cc: Guard
index 0fb5916fc655260cc8b1f3af8c7e253578909c3b..240542151e4a6e4cac9e64c149188c1380e4771a 100644 (file)
@@ -166,47 +166,22 @@ namespace tr1
   _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*,
               is_function<_Tp>::value)
 
-  template<typename _Tp, bool = (is_fundamental<_Tp>::value
-                                || is_array<_Tp>::value
-                                || is_pointer<_Tp>::value
-                                || is_reference<_Tp>::value
-                                || is_member_pointer<_Tp>::value
-                                || is_function<_Tp>::value)>
-    struct __is_enum_helper
-    : public __sfinae_types
-    {
-    private:
-      static __one __test(bool);
-      static __one __test(char);
-      static __one __test(signed char);
-      static __one __test(unsigned char);
-#ifdef _GLIBCXX_USE_WCHAR_T
-      static __one __test(wchar_t);
-#endif
-      static __one __test(short);
-      static __one __test(unsigned short);
-      static __one __test(int);
-      static __one __test(unsigned int);
-      static __one __test(long);
-      static __one __test(unsigned long);
-      static __one __test(long long);
-      static __one __test(unsigned long long);
-      static __two __test(...);
-
-      struct __convert
-      { operator _Tp() const; };
-
-    public:
-      static const bool __value = sizeof(__test(__convert())) == 1;
-    };
-
-  template<typename _Tp>
-    struct __is_enum_helper<_Tp, true>
-    { static const bool __value = false; };
-
   template<typename _Tp>
     struct is_enum
-    : public integral_constant<bool, __is_enum_helper<_Tp>::__value> { };
+    : public integral_constant<bool, !(is_fundamental<_Tp>::value
+                                      || is_array<_Tp>::value
+                                      || is_pointer<_Tp>::value
+                                      || is_reference<_Tp>::value
+                                      || is_member_pointer<_Tp>::value
+                                      || is_function<_Tp>::value
+                                      || __is_union_or_class<_Tp>::value)>
+    { };
+
+  template<typename>
+    struct is_union { };
+
+  template<typename>
+    struct is_class { };
 
   template<typename _Tp, bool = (is_void<_Tp>::value
                                 || is_reference<_Tp>::value)>
@@ -264,6 +239,26 @@ namespace tr1
                               (is_member_object_pointer<_Tp>::value
                                || is_member_function_pointer<_Tp>::value)>
     { };
+
+  template<typename _Tp>
+    struct __is_union_or_class_helper
+    : public __sfinae_types
+    {
+    private:
+      template<typename _Up>
+        static __one __test(int _Up::*);
+      template<typename>
+        static __two __test(...);
+    
+    public:
+      static const bool __value = sizeof(__test<_Tp>(0)) == 1;
+    };
+
+  // Extension.
+  template<typename _Tp>
+    struct __is_union_or_class
+    : public integral_constant<bool, __is_union_or_class_helper<_Tp>::__value>
+    { };
   
   /// @brief  type properties [4.5.3].
   template<typename>
@@ -289,26 +284,21 @@ namespace tr1
                                      remove_all_extents<_Tp>::type>::value)>
     { };
 
-  template<typename>
-    struct __is_empty_helper_1
-    { };
-
-  template<typename _Tp>
-    struct __is_empty_helper_2
-    : public _Tp { };
-
-  // Unfortunately, without compiler support we cannot tell union from
-  // class types, and is_empty doesn't work at all with the former. 
-  template<typename _Tp, bool = (is_fundamental<_Tp>::value
-                                || is_array<_Tp>::value
-                                || is_pointer<_Tp>::value
-                                || is_reference<_Tp>::value
-                                || is_member_pointer<_Tp>::value
-                                || is_enum<_Tp>::value
-                                || is_function<_Tp>::value)>
+  // N.B. Without compiler support we cannot tell union from class types,
+  // and is_empty doesn't work at all with the former. 
+  template<typename _Tp, bool = !__is_union_or_class<_Tp>::value>
     struct __is_empty_helper
-    { static const bool __value = (sizeof(__is_empty_helper_1<_Tp>)
-                                  == sizeof(__is_empty_helper_2<_Tp>)); };
+    { 
+    private:
+      template<typename>
+        struct __ebo_1 { };
+      template<typename _Up>
+        struct __ebo_2
+        : public _Up { };
+           
+    public:
+      static const bool __value = sizeof(__ebo_1<_Tp>) == sizeof(__ebo_2<_Tp>);
+    };
 
   template<typename _Tp>
     struct __is_empty_helper<_Tp, true>
index 738e67cd1d693c71db6fdf2ee18040bb811417ab..2d25d9b4c1469bd83fb4130995cec02e739373d7 100644 (file)
@@ -103,6 +103,10 @@ namespace tr1
 
   template<typename _Tp>
     struct is_member_pointer;
+
+  // Extension.
+  template<typename _Tp>
+    struct __is_union_or_class;
    
   /// @brief  type properties [4.5.3].
   template<typename _Tp>
index 2a47483951c3ddde20504a668deeb389754466ee..3e9fafc434c16de5304dce52c9720e0a319c1bfc 100644 (file)
@@ -123,67 +123,69 @@ namespace __gnu_test
   class AbstractClass
   { virtual void rotate(int) = 0; };
 
+  union UnionType { };
 
- int truncate_float(float x) { return (int)x; }
- long truncate_double(double x) { return (long)x; }
 
- struct do_truncate_float_t
- {
-   do_truncate_float_t()
-   {
-     ++live_objects;
-   }
-
-   do_truncate_float_t(const do_truncate_float_t&)
-   {
-     ++live_objects;
-   }
+  int truncate_float(float x) { return (int)x; }
+  long truncate_double(double x) { return (long)x; }
 
-   ~do_truncate_float_t()
-   {
-     --live_objects;
-   }
-
-   int operator()(float x) { return (int)x; }
+  struct do_truncate_float_t
+  {
+    do_truncate_float_t()
+    {
+      ++live_objects;
+    }
 
-   static int live_objects;
- };
+    do_truncate_float_t(const do_truncate_float_t&)
+    {
+      ++live_objects;
+    }
+    
+    ~do_truncate_float_t()
+    {
+      --live_objects;
+    }
 
- int do_truncate_float_t::live_objects = 0;
+    int operator()(float x) { return (int)x; }
+    
+    static int live_objects;
+  };
 
- struct do_truncate_double_t
- {
-   do_truncate_double_t()
-   {
-     ++live_objects;
-   }
+  int do_truncate_float_t::live_objects = 0;
 
-   do_truncate_double_t(const do_truncate_double_t&)
-   {
+  struct do_truncate_double_t
+  {
+    do_truncate_double_t()
+    {
      ++live_objects;
-   }
-
-   ~do_truncate_double_t()
-   {
-     --live_objects;
-   }
-
-   long operator()(double x) { return (long)x; }
-
-   static int live_objects;
- };
+    }
 
- int do_truncate_double_t::live_objects = 0;
+    do_truncate_double_t(const do_truncate_double_t&)
+    {
+      ++live_objects;
+    }
 
- struct X
- {
-   int bar;
+    ~do_truncate_double_t()
+    {
+      --live_objects;
+    }
 
-   int foo()                   { return 1; }
-   int foo_c() const           { return 2; }
-   int foo_v()  volatile       { return 3; }
-   int foo_cv() const volatile { return 4; }
- };
+    long operator()(double x) { return (long)x; }
+    
+    static int live_objects;
+  };
+  
+  int do_truncate_double_t::live_objects = 0;
+  
+  struct X
+  {
+    int bar;
+    
+    int foo()                   { return 1; }
+    int foo_c() const           { return 2; }
+    int foo_v()  volatile       { return 3; }
+    int foo_cv() const volatile { return 4; }
+  };
 }; // namespace __gnu_test
 
 #endif // _GLIBCXX_TESTSUITE_TR1_H
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/is_union_or_class.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/is_union_or_class.cc
new file mode 100644 (file)
index 0000000..6027eb9
--- /dev/null
@@ -0,0 +1,60 @@
+// 2005-02-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 4.5.2 Composite type traits
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::__is_union_or_class;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_category<__is_union_or_class, UnionType>(true)) );
+  VERIFY( (test_category<__is_union_or_class, ClassType>(true)) );
+  VERIFY( (test_category<__is_union_or_class, DerivedType>(true)) );
+  VERIFY( (test_category<__is_union_or_class, ConvType>(true)) );
+  VERIFY( (test_category<__is_union_or_class, AbstractClass>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_category<__is_union_or_class, void>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int>(false)) );
+  VERIFY( (test_category<__is_union_or_class, float>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int[2]>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int*>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int(*)(int)>(false)) );
+  VERIFY( (test_category<__is_union_or_class, float&>(false)) );
+  VERIFY( (test_category<__is_union_or_class, float(&)(float)>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int (ClassType::*)>(false)) );
+  VERIFY( (test_category<__is_union_or_class,
+          int (ClassType::*) (int)>(false)) );
+  VERIFY( (test_category<__is_union_or_class, int (int)>(false)) );
+  VERIFY( (test_category<__is_union_or_class, EnumType>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_union_or_class/typedefs.cc
new file mode 100644 (file)
index 0000000..d62829a
--- /dev/null
@@ -0,0 +1,36 @@
+// 2005-02-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 
+// NB: This file is for testing tr1/type_traits with NO OTHER INCLUDES.
+
+#include <tr1/type_traits>
+
+// { dg-do compile }
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::tr1::__is_union_or_class<int>  test_type;
+  typedef test_type::value_type               value_type;
+  typedef test_type::type                     type;
+  typedef test_type::type::value_type         type_value_type;
+  typedef test_type::type::type               type_type;
+}