[v3] Improve std::is_convertible

View: New views
1 Messages — Rating Filter:   Alert me  

[v3] Improve std::is_convertible

by Paolo Carlini-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

should bring us very close to the best we can do without front-end
support. Tested x86_64-linux, committed to mainline.

Paolo.

///////////////////////

2009-10-29  Paolo Carlini  <paolo.carlini@...>

        * include/std/type_traits (__is_int_or_cref): Remove.
        (__is_convertible_helper): Fix per C++0x and simplify (the hack to
        suppress warnings isn't necessary anymore).
        * testsuite/20_util/is_convertible/requirements/typedefs.cc: New.
        * testsuite/20_util/is_convertible/requirements/
        explicit_instantiation.cc: Likewise.
        * testsuite/20_util/is_convertible/value.cc: Likewise.
        * testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust
        dg-error line numbers.
        * testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
        Likewise.

Index: include/std/type_traits
===================================================================
--- include/std/type_traits (revision 153724)
+++ include/std/type_traits (working copy)
@@ -253,39 +253,23 @@
       static const bool __value = sizeof(__test(__makeFrom())) == 1;
     };
 
-  template<typename _Tp>
-    struct __is_int_or_cref
-    {
-      typedef typename remove_reference<_Tp>::type __rr_Tp;
-      static const bool __value = (is_integral<_Tp>::value
-   || (is_integral<__rr_Tp>::value
-       && is_const<__rr_Tp>::value
-       && !is_volatile<__rr_Tp>::value));
-    };
-
   template<typename _From, typename _To,
    bool = (is_void<_From>::value || is_void<_To>::value
-   || is_function<_To>::value || is_array<_To>::value
-   // This special case is here only to avoid warnings.
-   || (is_floating_point<typename
-       remove_reference<_From>::type>::value
-       && __is_int_or_cref<_To>::__value))>
+   || is_function<_To>::value || is_array<_To>::value)>
     struct __is_convertible_helper
     {
-      // "An imaginary lvalue of type From...".
       static const bool __value = (__is_convertible_simple<typename
-   add_lvalue_reference<_From>::type,
+   add_rvalue_reference<_From>::type,
    _To>::__value);
     };
 
   template<typename _From, typename _To>
     struct __is_convertible_helper<_From, _To, true>
-    { static const bool __value = (is_void<_To>::value
-   || (__is_int_or_cref<_To>::__value
-       && !is_void<_From>::value)); };
+    { static const bool __value = (is_void<_From>::value
+   && is_void<_To>::value); };
 
   // XXX FIXME
-  // The C++0x specifications are different, see N2255.
+  // The C++0x specifications require front-end support, see N2255.
   /// is_convertible
   template<typename _From, typename _To>
     struct is_convertible
Index: testsuite/20_util/make_signed/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_signed/requirements/typedefs_neg.cc (revision 153724)
+++ testsuite/20_util/make_signed/requirements/typedefs_neg.cc (working copy)
@@ -3,7 +3,7 @@
 
 // 2007-05-03  Benjamin Kosnik  <bkoz@...>
 //
-// Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+// Copyright (C) 2007, 2008, 2009 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
@@ -48,8 +48,8 @@
 // { dg-error "instantiated from here" "" { target *-*-* } 40 }
 // { dg-error "instantiated from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 570 }
-// { dg-error "declaration of" "" { target *-*-* } 532 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 554 }
+// { dg-error "declaration of" "" { target *-*-* } 516 }
 
 // { dg-excess-errors "At global scope" }
 // { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/is_convertible/value.cc
===================================================================
--- testsuite/20_util/is_convertible/value.cc (revision 0)
+++ testsuite/20_util/is_convertible/value.cc (revision 0)
@@ -0,0 +1,102 @@
+// { dg-options "-std=gnu++0x" }
+
+// 2009-10-29  Paolo Carlini  <paolo.carlini@...>
+//
+// Copyright (C) 2009 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::is_convertible;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_relationship<is_convertible, int, int>(true)) );
+  VERIFY( (test_relationship<is_convertible, int, const int>(true)) );
+  VERIFY( (test_relationship<is_convertible, volatile int, const int>(true)) );
+  VERIFY( (test_relationship<is_convertible, int, float>(true)) );
+  VERIFY( (test_relationship<is_convertible, double, float>(true)) );
+  VERIFY( (test_relationship<is_convertible, float, int>(true)) );
+  VERIFY( (test_relationship<is_convertible, int*, const int*>(true)) );
+  VERIFY( (test_relationship<is_convertible, int*, void*>(true)) );
+  VERIFY( (test_relationship<is_convertible, int[4], int*>(true)) );
+  VERIFY( (test_relationship<is_convertible, float&, int>(true)) );
+  VERIFY( (test_relationship<is_convertible, int, const int&>(true)) );
+  VERIFY( (test_relationship<is_convertible, const int&, int>(true)) );
+  VERIFY( (test_relationship<is_convertible, float, const int&>(true)) );
+  VERIFY( (test_relationship<is_convertible, int(int), int(*)(int)>(true)) );
+  VERIFY( (test_relationship<is_convertible, int(&)(int), int(*)(int)>(true)) );
+  VERIFY( (test_relationship<is_convertible, EnumType, int>(true)) );
+  VERIFY( (test_relationship<is_convertible, ClassType, ClassType>(true)) );
+  VERIFY( (test_relationship<is_convertible, DerivedType, ClassType>(true)) );
+  VERIFY( (test_relationship<is_convertible, DerivedType*, ClassType*>(true)) );
+  VERIFY( (test_relationship<is_convertible, DerivedType&, ClassType&>(true)) );
+
+  VERIFY( (test_relationship<is_convertible, const int, const int&>(true)) );
+
+  VERIFY( (test_relationship<is_convertible, void, void>(true)) );
+  VERIFY( (test_relationship<is_convertible, const void, void>(true)) );
+  VERIFY( (test_relationship<is_convertible, void, volatile void>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_relationship<is_convertible, const int*, int*>(false)) );
+  VERIFY( (test_relationship<is_convertible, int*, float*>(false)) );
+  VERIFY( (test_relationship<is_convertible, const int[4], int*>(false)) );
+  VERIFY( (test_relationship<is_convertible, int[4], int[4]>(false)) );
+  VERIFY( (test_relationship<is_convertible, const int&, int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, float&, int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, float, volatile int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, int(int), int(int)>(false)) );
+  VERIFY( (test_relationship<is_convertible, int(int), int(*)(void)>(false)) );
+  VERIFY( (test_relationship<is_convertible, int(*)(int),
+                                     int(&)(int)>(false)) );
+  VERIFY( (test_relationship<is_convertible, int, EnumType>(false)) );
+  VERIFY( (test_relationship<is_convertible, int, ClassType>(false)) );
+  VERIFY( (test_relationship<is_convertible, ClassType, DerivedType>(false)) );
+  VERIFY( (test_relationship<is_convertible, ClassType*,
+                                     DerivedType*>(false)) );
+  VERIFY( (test_relationship<is_convertible, ClassType&,
+                                     DerivedType&>(false)) );
+
+  VERIFY( (test_relationship<is_convertible, void, int>(false)) );
+  VERIFY( (test_relationship<is_convertible, void, float>(false)) );  
+  VERIFY( (test_relationship<is_convertible, void, int(*)(int)>(false)) );
+
+  // C++0x
+  VERIFY( (test_relationship<is_convertible, int, void>(false)) );
+  VERIFY( (test_relationship<is_convertible, int[4], void>(false)) );
+
+  VERIFY( (test_relationship<is_convertible, int, int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, float,
+                                     volatile float&>(false)) );
+  VERIFY( (test_relationship<is_convertible, const volatile int,
+                                     const volatile int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, volatile int,
+                                     volatile int&>(false)) );
+  VERIFY( (test_relationship<is_convertible, int(int), int(&)(int)>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/20_util/is_convertible/requirements/typedefs.cc
===================================================================
--- testsuite/20_util/is_convertible/requirements/typedefs.cc (revision 0)
+++ testsuite/20_util/is_convertible/requirements/typedefs.cc (revision 0)
@@ -0,0 +1,36 @@
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// 2009-10-29  Paolo Carlini  <paolo.carlini@...>
+//
+// Copyright (C) 2009 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+//
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::is_convertible<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;
+}
Index: testsuite/20_util/is_convertible/requirements/explicit_instantiation.cc
===================================================================
--- testsuite/20_util/is_convertible/requirements/explicit_instantiation.cc (revision 0)
+++ testsuite/20_util/is_convertible/requirements/explicit_instantiation.cc (revision 0)
@@ -0,0 +1,31 @@
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// 2009-10-29  Paolo Carlini  <paolo.carlini@...>
+
+// Copyright (C) 2009 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+namespace std
+{
+  typedef short test_type;
+  template struct is_convertible<test_type, test_type>;
+}
Index: testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc (revision 153724)
+++ testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc (working copy)
@@ -3,7 +3,7 @@
 
 // 2007-05-03  Benjamin Kosnik  <bkoz@...>
 //
-// Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+// Copyright (C) 2007, 2008, 2009 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
@@ -48,8 +48,8 @@
 // { dg-error "instantiated from here" "" { target *-*-* } 40 }
 // { dg-error "instantiated from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 491 }
-// { dg-error "declaration of" "" { target *-*-* } 453 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 475 }
+// { dg-error "declaration of" "" { target *-*-* } 437 }
 
 // { dg-excess-errors "At global scope" }
 // { dg-excess-errors "In instantiation of" }