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: add is_empty


Hi,

tested x86-linux, committed to mainline.

Paolo.

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

	* include/tr1/type_traits: Implement is_empty.
	* testsuite/tr1/4_metaprogramming/type_properties/is_empty/
	is_empty.cc: New.
	* testsuite/tr1/4_metaprogramming/type_properties/is_empty/
	typedefs.cc: Likewise.

	* include/tr1/type_traits (__is_abstract_helper): Simplify a bit.
diff -urN libstdc++-v3-orig/include/tr1/type_traits libstdc++-v3/include/tr1/type_traits
--- libstdc++-v3-orig/include/tr1/type_traits	2005-01-28 14:25:28.000000000 +0100
+++ libstdc++-v3/include/tr1/type_traits	2005-01-28 17:56:43.000000000 +0100
@@ -477,10 +477,38 @@
 				      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)>
+    struct __is_empty_helper
+    { static const bool __value = (sizeof(__is_empty_helper_1<_Tp>)
+				   == sizeof(__is_empty_helper_2<_Tp>)); };
+
+  template<typename _Tp>
+    struct __is_empty_helper<_Tp, true>
+    { static const bool __value = false; };
+
+  template<typename _Tp>
+    struct is_empty
+    : public integral_constant<bool, __is_empty_helper<_Tp>::__value>
+    { };
+
   // Exploit the resolution DR core/337.
-  template<typename _Tp, bool = (is_void<_Tp>::value
-				 || is_function<_Tp>::value
-				 || is_reference<_Tp>::value)>
+  template<typename _Tp, bool = !is_object<_Tp>::value>
     struct __is_abstract_helper
     : public __sfinae_types
     {
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc	2005-01-28 17:41:55.000000000 +0100
@@ -0,0 +1,73 @@
+// 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>
+
+class EmptyClassOne
+{ typedef int type; };
+
+class EmptyClassTwo
+{ static int data; };
+
+class EmptyClassThree
+{ int f(); };
+
+class NonEmptyClassOne
+{ int data; };
+
+class NonEmptyClassTwo
+{ virtual int f(); };
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::is_empty;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_category<is_empty, ClassType>(true)) );
+  VERIFY( (test_category<is_empty, EmptyClassOne>(true)) );
+  VERIFY( (test_category<is_empty, EmptyClassTwo>(true)) );
+  VERIFY( (test_category<is_empty, EmptyClassThree>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_category<is_empty, void>(false)) );
+  VERIFY( (test_category<is_empty, float>(false)) );
+  VERIFY( (test_category<is_empty, int[4]>(false)) );
+  VERIFY( (test_category<is_empty, int*>(false)) );
+  VERIFY( (test_category<is_empty, int&>(false)) );
+  VERIFY( (test_category<is_empty, int (ClassType::*)>(false)) );
+  VERIFY( (test_category<is_empty, EnumType>(false)) );
+  VERIFY( (test_category<is_empty, int (int)>(false)) );
+
+  VERIFY( (test_category<is_empty, AbstractClass>(false)) );
+  VERIFY( (test_category<is_empty, NonEmptyClassOne>(false)) );
+  VERIFY( (test_category<is_empty, NonEmptyClassTwo>(false)) );  
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc	2005-01-28 17:14:35.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_empty<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]