type_traits: Implement is_enum (usual caveats about the nasty consequences of c+...
authorPaolo Carlini <pcarlini@suse.de>
Sat, 25 Dec 2004 15:24:36 +0000 (15:24 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sat, 25 Dec 2004 15:24:36 +0000 (15:24 +0000)
2004-12-25  Paolo Carlini  <pcarlini@suse.de>

* include/tr1/type_traits: Implement is_enum (usual caveats about
the nasty consequences of c++/19076...).
* testsuite/testsuite_tr1.h: Add ConvType.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_scalar/is_scalar.cc: New.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_scalar/typedefs.cc: Likewise.
* testsuite/tr1/4_metaprogramming/primary_type_categories/
is_enum/is_enum.cc: Likewise.
* testsuite/tr1/4_metaprogramming/primary_type_categories/
is_enum/typedefs.cc: Likewise.

From-SVN: r92604

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

index 6e817284d361d352c6589b4480d0724e3e07605d..b644e008c2ffc71cb999b4d94f3dbf0a01f6d164 100644 (file)
@@ -1,3 +1,17 @@
+2004-12-25  Paolo Carlini  <pcarlini@suse.de>
+
+       * include/tr1/type_traits: Implement is_enum (usual caveats about
+       the nasty consequences of c++/19076...).
+       * testsuite/testsuite_tr1.h: Add ConvType.
+       * testsuite/tr1/4_metaprogramming/composite_type_traits/
+       is_scalar/is_scalar.cc: New.
+       * testsuite/tr1/4_metaprogramming/composite_type_traits/
+       is_scalar/typedefs.cc: Likewise.
+       * testsuite/tr1/4_metaprogramming/primary_type_categories/
+       is_enum/is_enum.cc: Likewise.
+       * testsuite/tr1/4_metaprogramming/primary_type_categories/
+       is_enum/typedefs.cc: Likewise.
+
 2004-12-24  Paolo Carlini  <pcarlini@suse.de>
 
        * include/tr1/type_traits: Add missing undef.
index 42f9bac6af414474955e491beaed9417c785b4a5..9816adafa5206f2e0a83fe790d669a84ab31863d 100644 (file)
@@ -33,7 +33,7 @@ namespace std
 {
 namespace tr1
 {
-  // For use in is_function and elsewhere.
+  // For use in is_enum, is_function, and elsewhere.
   struct __sfinae_types
   {
     typedef char __one;
@@ -149,14 +149,57 @@ namespace tr1
     : public false_type { };
   _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*)
 
+  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(...);
+
+      template<typename _Up>
+        struct __convert
+       { operator _Up() const; };
+
+    public:
+      static const bool __value = sizeof(__test(__convert<_Tp>())) == 1;
+    };
+
   template<typename _Tp>
+    struct __is_enum_helper<_Tp, true>
+    { static const bool __value = false; };
+
+  template<typename _Tp>
+    struct is_enum
+    : integral_constant<bool, __is_enum_helper<_Tp>::__value> { };
+
+  template<typename _Tp, bool = (is_reference<_Tp>::value
+                                || is_void<_Tp>::value)>
     struct __is_function_helper
     : public __sfinae_types
     {
     private:
       template<typename>
         static __one __test(...);
-
       template<typename _Up>
         static __two __test(_Up(*)[1]);
     
@@ -164,12 +207,13 @@ namespace tr1
       static const bool __value = sizeof(__test<_Tp>(0)) == 1;
     };
   
+  template<typename _Tp>
+    struct __is_function_helper<_Tp, true>
+    { static const bool __value = false; };
+
   template<typename _Tp>
     struct is_function
-    : public integral_constant<bool, (__is_function_helper<_Tp>::__value
-                                     && !is_reference<_Tp>::value
-                                     && !is_void<_Tp>::value)>
-    { };
+    : public integral_constant<bool, __is_function_helper<_Tp>::__value> { };
 
   /// @brief  composite type traits [4.5.2].
   template<typename _Tp>
index c9d8d235781db9acc7833b5d46b6b3af520d2309..0ad8ac14313cc7c51006505544138c84f8107a74 100644 (file)
@@ -78,6 +78,12 @@ namespace __gnu_test
   typedef const ClassType           cClassType;
   typedef volatile ClassType        vClassType;
   typedef const volatile ClassType  cvClassType;
+
+  enum EnumType { };
+
+  struct ConvType
+  { operator int() const; };
+  
 }; // namespace __gnu_test
 
 #endif // _GLIBCXX_TESTSUITE_TR1_H
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc
new file mode 100644 (file)
index 0000000..37d6592
--- /dev/null
@@ -0,0 +1,50 @@
+// 2004-12-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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_scalar;
+  using namespace __gnu_test;
+
+  VERIFY( (test_category<is_scalar, int>(true)) );
+  VERIFY( (test_category<is_scalar, float>(true)) );
+  VERIFY( (test_category<is_scalar, EnumType>(true)) );
+  VERIFY( (test_category<is_scalar, int*>(true)) );
+  VERIFY( (test_category<is_scalar, int(*)(int)>(true)) );
+  VERIFY( (test_category<is_scalar, int (ClassType::*)>(true)) );
+  // Temporarily disabled because of c++/19076 :-(
+  // VERIFY( (test_category<is_scalar, int (ClassType::*) (int)>(true)) );
+
+  // Sanity check.
+  VERIFY( (test_category<is_scalar, ClassType>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc
new file mode 100644 (file)
index 0000000..5a1740f
--- /dev/null
@@ -0,0 +1,36 @@
+// 2004-12-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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_scalar<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;
+}
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc
new file mode 100644 (file)
index 0000000..3de48c4
--- /dev/null
@@ -0,0 +1,60 @@
+// 2004-12-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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.1 Primary type categories
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::is_enum;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_category<is_enum, EnumType>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_category<is_enum, void>(false)) );
+  VERIFY( (test_category<is_enum, int>(false)) );
+  VERIFY( (test_category<is_enum, float>(false)) );
+  VERIFY( (test_category<is_enum, int[2]>(false)) );
+  VERIFY( (test_category<is_enum, int*>(false)) );
+  VERIFY( (test_category<is_enum, int(*)(int)>(false)) );
+  VERIFY( (test_category<is_enum, float&>(false)) );
+  VERIFY( (test_category<is_enum, float(&)(float)>(false)) );
+  VERIFY( (test_category<is_enum, int (ClassType::*)>(false)) );
+  // Temporarily disabled because of c++/19076 :-(
+  // VERIFY( (test_category<is_enum, int (ClassType::*) (int)>(false)) );
+  VERIFY( (test_category<is_enum, int (int)>(false)) );
+
+  VERIFY( (test_category<is_enum, ConvType>(false)) );
+
+  // Sanity check.
+  VERIFY( (test_category<is_enum, ClassType>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc
new file mode 100644 (file)
index 0000000..eeb1093
--- /dev/null
@@ -0,0 +1,36 @@
+// 2004-12-25  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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_enum<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;
+}