type_traits: Add is_base_of.
authorPaolo Carlini <pcarlini@suse.de>
Sat, 5 Mar 2005 00:22:48 +0000 (00:22 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sat, 5 Mar 2005 00:22:48 +0000 (00:22 +0000)
2005-03-04  Paolo Carlini  <pcarlini@suse.de>

* include/tr1/type_traits: Add is_base_of.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_base_of/is_base_of.cc: New.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_base_of/typedefs.cc: Likewise.

From-SVN: r95911

libstdc++-v3/ChangeLog
libstdc++-v3/include/tr1/type_traits
libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc [new file with mode: 0644]
libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc [new file with mode: 0644]

index 8acc3271c8d2760298764cf7e5ed047537c201fb..4ec4808e56d07f3f8398dba5f84043fb9a08e6a3 100644 (file)
@@ -1,3 +1,11 @@
+2005-03-04  Paolo Carlini  <pcarlini@suse.de>
+
+       * include/tr1/type_traits: Add is_base_of.
+       * testsuite/tr1/4_metaprogramming/relationships_between_types/
+       is_base_of/is_base_of.cc: New.
+       * testsuite/tr1/4_metaprogramming/relationships_between_types/
+       is_base_of/typedefs.cc: Likewise.
+
 2005-03-03  Benjamin Kosnik  <bkoz@redhat.com>
 
        * include/tr1/functional: Convert relative path.
index 37415881c7a4392b3583683e88b70d8c96f61d21..afdea0c2ad2f5d32ec6d93864c1300edba640a3d 100644 (file)
@@ -42,7 +42,7 @@ namespace std
 {
 namespace tr1
 {
-  // For use in is_enum, is_abstract and elsewhere.
+  // For use in __conv_helper, is_abstract and elsewhere.
   struct __sfinae_types
   {
     typedef char __one;
@@ -456,6 +456,43 @@ namespace tr1
     struct is_same<_Tp, _Tp>
     : public true_type { };
 
+  // See Daveed Vandevoorde explanation in http://tinyurl.com/502f.
+  // Also see Rani Sharoni in http://tinyurl.com/6jvyq.
+  template<typename _Base, typename _Derived,
+          bool = (!__is_union_or_class<_Base>::value
+                  || !__is_union_or_class<_Derived>::value
+                  || is_same<_Base, _Derived>::value)>
+    struct __is_base_of_helper
+    : public __sfinae_types
+    {
+    private:
+      typedef typename remove_cv<_Base>::type     _NoCv_Base;      
+      typedef typename remove_cv<_Derived>::type  _NoCv_Derived;
+      
+      template<typename _Up>
+        static __one __test(_NoCv_Derived&, _Up);
+      static __two __test(_NoCv_Base&, int);
+   
+      struct _Conv
+      {
+       operator _NoCv_Derived&();
+       operator _NoCv_Base&() const;
+      };
+   
+    public:
+      static const bool __value = sizeof(__test(_Conv(), 0)) == 1;
+    };
+
+  template<typename _Base, typename _Derived>
+    struct __is_base_of_helper<_Base, _Derived, true>
+    { static const bool __value = is_same<_Base, _Derived>::value; };
+
+  template<typename _Base, typename _Derived>
+    struct is_base_of
+    : public integral_constant<bool,
+                              __is_base_of_helper<_Base, _Derived>::__value>
+    { };
+
   template<typename _Tp>
     struct __is_int_or_cref
     {
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc
new file mode 100644 (file)
index 0000000..cac05b2
--- /dev/null
@@ -0,0 +1,94 @@
+// 2005-03-04  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.6 Relationships between types
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+class HiddenCons
+{
+  HiddenCons();
+  HiddenCons(const HiddenCons&);
+};
+
+class DerivedHiddenCons
+: private HiddenCons
+{
+  DerivedHiddenCons();
+  DerivedHiddenCons(const DerivedHiddenCons&);
+};
+
+class MultiDerivedHiddenCons
+: private HiddenCons, private __gnu_test::ClassType 
+{
+  MultiDerivedHiddenCons();
+  MultiDerivedHiddenCons(const MultiDerivedHiddenCons&);
+};
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::is_base_of;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_relationship<is_base_of, int, int>(true)) );
+  VERIFY( (test_relationship<is_base_of, EnumType, EnumType>(true)) );
+  VERIFY( (test_relationship<is_base_of, UnionType, UnionType>(true)) );
+  VERIFY( (test_relationship<is_base_of, AbstractClass,
+          AbstractClass>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, DerivedType>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, const DerivedType>(true)) );
+  VERIFY( (test_relationship<is_base_of, volatile ClassType,
+          volatile DerivedType>(true)) );  
+  VERIFY( (test_relationship<is_base_of, PolymorphicClass,
+          DerivedPolymorphic>(true)) );
+  VERIFY( (test_relationship<is_base_of, HiddenCons,
+          DerivedHiddenCons>(true)) );
+  VERIFY( (test_relationship<is_base_of, HiddenCons,
+          MultiDerivedHiddenCons>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType,
+          MultiDerivedHiddenCons>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_relationship<is_base_of, int, const int>(false)) );
+  VERIFY( (test_relationship<is_base_of, volatile UnionType,
+          UnionType>(false)) );
+  VERIFY( (test_relationship<is_base_of, int&, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, AbstractClass, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, AbstractClass>(false)) );  
+  VERIFY( (test_relationship<is_base_of, DerivedType, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, DerivedPolymorphic,
+          PolymorphicClass>(false)) );
+  VERIFY( (test_relationship<is_base_of, DerivedHiddenCons,
+          HiddenCons>(false)) );
+  VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
+          HiddenCons>(false)) );
+  VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
+          ClassType>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc
new file mode 100644 (file)
index 0000000..0223f4d
--- /dev/null
@@ -0,0 +1,36 @@
+// 2005-03-04  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_base_of<int, 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;
+}