This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] tr1/type_traits: fix is_function, implement is_abstract


Hi,

the below reimplements is_function and adds is_abstract.

Tested x86-linux, committing to mainline.

Paolo.

///////////////////
2005-01-28  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/type_traits: Implement is_abstract, by exploiting the
	resolution of DR core/337.
	* testsuite/testsuite_tr1.h: Add AbstractClass.
	* testsuite/tr1/4_metaprogramming/type_properties/is_abstract/
	is_abstract.cc: New.
	* testsuite/tr1/4_metaprogramming/type_properties/is_abstract/
	typedefs.cc: Likewise.
	
	* include/tr1/type_traits (is_function): Rewrite, use the conversion
	F& -> F* instead, thus avoiding problems with abstract classes.
	* testsuite/tr1/4_metaprogramming/primary_type_categories/
	is_function/is_function.cc: Add a test for tricky AbstractClass.
diff -urN libstdc++-v3-orig/include/tr1/type_traits libstdc++-v3/include/tr1/type_traits
--- libstdc++-v3-orig/include/tr1/type_traits	2005-01-24 15:06:12.000000000 +0100
+++ libstdc++-v3/include/tr1/type_traits	2005-01-28 14:25:28.000000000 +0100
@@ -33,13 +33,26 @@
 {
 namespace tr1
 {
-  // For use in is_enum, is_function, and elsewhere.
+  // For use in is_enum, is_abstract and elsewhere.
   struct __sfinae_types
   {
     typedef char __one;
     typedef struct { char __arr[2]; } __two;
   };
 
+  template<typename _From, typename _To>
+    struct __conv_helper
+    : public __sfinae_types
+    {
+    private:
+      static __one __test(_To);
+      static __two __test(...);
+      static _From __makeFrom();
+    
+    public:
+      static const bool __value = sizeof(__test(__makeFrom())) == 1;
+    };
+
 #define _DEFINE_SPEC_BODY(_Value)                                    \
     : public integral_constant<bool, _Value> { };
 
@@ -393,28 +406,13 @@
     struct is_enum
     : public 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]);
-    
-    public:
-      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> { };
+    : public integral_constant<bool,
+			       (__conv_helper<typename add_reference<_Tp>::type,
+				typename add_pointer<_Tp>::type>::__value)>
+    { };
+  _DEFINE_SPEC(0, is_function, void, false)
 
   /// @brief  composite type traits [4.5.2].
   template<typename _Tp>
@@ -479,6 +477,31 @@
 				      remove_all_extents<_Tp>::type>::value)>
     { };
 
+  // Exploit the resolution DR core/337.
+  template<typename _Tp, bool = (is_void<_Tp>::value
+				 || is_function<_Tp>::value
+				 || is_reference<_Tp>::value)>
+    struct __is_abstract_helper
+    : public __sfinae_types
+    {
+    private:
+      template<typename>
+        static __one __test(...);
+      template<typename _Up>
+        static __two __test(_Up(*)[1]);
+    
+    public:
+      static const bool __value = sizeof(__test<_Tp>(0)) == 1;
+    };
+  
+  template<typename _Tp>
+    struct __is_abstract_helper<_Tp, true>
+    { static const bool __value = false; };
+
+  template<typename _Tp>
+    struct is_abstract
+    : public integral_constant<bool, __is_abstract_helper<_Tp>::__value> { };
+
   template<typename _Tp>
     struct has_trivial_constructor
     : public integral_constant<bool, is_pod<_Tp>::value> { };
diff -urN libstdc++-v3-orig/testsuite/testsuite_tr1.h libstdc++-v3/testsuite/testsuite_tr1.h
--- libstdc++-v3-orig/testsuite/testsuite_tr1.h	2004-12-31 00:27:27.000000000 +0100
+++ libstdc++-v3/testsuite/testsuite_tr1.h	2005-01-28 14:14:00.000000000 +0100
@@ -1,7 +1,7 @@
 // -*- C++ -*-
 // Testing utilities for the tr1 testsuite.
 //
-// Copyright (C) 2004 Free Software Foundation, Inc.
+// Copyright (C) 2004, 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
@@ -117,6 +117,9 @@
 
   struct ConvType
   { operator int() const; };
+
+  class AbstractClass
+  { virtual void rotate(int) = 0; };
   
 }; // namespace __gnu_test
 
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc	2004-12-16 12:35:56.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc	2005-01-28 14:06:54.000000000 +0100
@@ -1,6 +1,6 @@
 // 2004-12-16  Paolo Carlini  <pcarlini@suse.de>
 //
-// Copyright (C) 2004 Free Software Foundation, Inc.
+// Copyright (C) 2004, 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
@@ -39,6 +39,8 @@
   VERIFY( (test_category<is_function, int&>(false)) );
   VERIFY( (test_category<is_function, void>(false)) );
   VERIFY( (test_category<is_function, const void>(false)) );
+
+  VERIFY( (test_category<is_function, AbstractClass>(false)) );
   
   // Sanity check.
   VERIFY( (test_category<is_function, ClassType>(false)) );
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc	2005-01-28 13:59:46.000000000 +0100
@@ -0,0 +1,49 @@
+// 2005-01-28  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.3 Type properties
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::is_abstract;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_category<is_abstract, AbstractClass>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_category<is_abstract, void>(false)) );
+  VERIFY( (test_category<is_abstract, int (int)>(false)) );
+  VERIFY( (test_category<is_abstract, int&>(false)) );
+  
+  // Sanity check.
+  VERIFY( (test_category<is_abstract, ClassType>(false)) );  
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc	2005-01-28 13:51:42.000000000 +0100
@@ -0,0 +1,36 @@
+// 2005-01-28  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_abstract<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;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]