Patchwork [v3] Add std::underlying_type

login
register
mail settings
Submitter Paolo Carlini
Date April 25, 2011, 11:04 p.m.
Message ID <4DB5FE12.9000005@oracle.com>
Download mbox | patch
Permalink /patch/92825/
State New
Headers show

Comments

Paolo Carlini - April 25, 2011, 11:04 p.m.
Hi,

tested x86_64-linux, committed to mainline.

Paolo.

//////////////////////
2011-04-25  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/std/type_traits (struct underlying_type): Add.
	* testsuite/20_util/underlying_type/requirements/typedefs-1.cc: New.
	* testsuite/20_util/underlying_type/requirements/typedefs-2.cc:
	Likewise.
	* testsuite/20_util/underlying_type/requirements/
	explicit_instantiation.cc: Likewise.
	* testsuite/20_util/declval/requirements/1_neg.cc: Adjust dg-error
	line number.

Patch

Index: include/std/type_traits
===================================================================
--- include/std/type_traits	(revision 172943)
+++ include/std/type_traits	(working copy)
@@ -1581,7 +1581,12 @@ 
         common_type<typename common_type<_Tp, _Up>::type, _Vp...>::type type;
     };
 
-  /// underlying_type (still unimplemented)
+  /// underlying_type
+  template<typename _Tp>
+    struct underlying_type
+    {
+      typedef __underlying_type(_Tp) type;
+    };
 
   /// declval
   template<typename _Tp>
Index: testsuite/20_util/declval/requirements/1_neg.cc
===================================================================
--- testsuite/20_util/declval/requirements/1_neg.cc	(revision 172943)
+++ testsuite/20_util/declval/requirements/1_neg.cc	(working copy)
@@ -19,7 +19,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 1598 }
+// { dg-error "static assertion failed" "" { target *-*-* } 1603 }
 
 #include <utility>
 
Index: testsuite/20_util/underlying_type/requirements/typedefs-1.cc
===================================================================
--- testsuite/20_util/underlying_type/requirements/typedefs-1.cc	(revision 0)
+++ testsuite/20_util/underlying_type/requirements/typedefs-1.cc	(revision 0)
@@ -0,0 +1,42 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+//
+// Copyright (C) 2011 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()
+{
+  typedef enum E1 : unsigned { }                     test_type1;
+  typedef enum E2 : char { }                         test_type2;
+  typedef enum class E3 { }                          test_type3;
+  typedef enum class E4 : unsigned char { c = 1 }    test_type4;
+  typedef enum class E5 : int { a = -1, b = 1 }      test_type5;
+  typedef enum class E6 : long { c = __LONG_MAX__ }  test_type6;  
+
+  // Check for required typedef
+  typedef std::underlying_type<test_type1>::type  type1;
+  typedef std::underlying_type<test_type2>::type  type2;
+  typedef std::underlying_type<test_type3>::type  type3;
+  typedef std::underlying_type<test_type4>::type  type4;
+  typedef std::underlying_type<test_type5>::type  type5;
+  typedef std::underlying_type<test_type6>::type  type6;
+}
Index: testsuite/20_util/underlying_type/requirements/typedefs-2.cc
===================================================================
--- testsuite/20_util/underlying_type/requirements/typedefs-2.cc	(revision 0)
+++ testsuite/20_util/underlying_type/requirements/typedefs-2.cc	(revision 0)
@@ -0,0 +1,44 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+//
+// Copyright (C) 2011 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()
+{
+  using namespace std;
+
+  enum E1 : unsigned { };
+  enum E2 : char { };
+  enum class E3 { };
+  enum class E4 : unsigned char { c = 1 };
+  enum class E5 : int { a = -1, b = 1 };
+  enum class E6 : long { c = __LONG_MAX__ };  
+
+  static_assert(is_same<underlying_type<E1>::type, unsigned>::value, "Error");
+  static_assert(is_same<underlying_type<E2>::type, char>::value, "Error");
+  static_assert(is_same<underlying_type<E3>::type, int>::value, "Error");
+  static_assert(is_same<underlying_type<E4>::type,
+			unsigned char>::value, "Error");
+  static_assert(is_same<underlying_type<E5>::type, int>::value, "Error");
+  static_assert(is_same<underlying_type<E6>::type, long>::value, "Error");
+}
Index: testsuite/20_util/underlying_type/requirements/explicit_instantiation.cc
===================================================================
--- testsuite/20_util/underlying_type/requirements/explicit_instantiation.cc	(revision 0)
+++ testsuite/20_util/underlying_type/requirements/explicit_instantiation.cc	(revision 0)
@@ -0,0 +1,40 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// Copyright (C) 2011 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 enum E1 : unsigned { }                     test_type1;
+  typedef enum E2 : char { }                         test_type2;
+  typedef enum class E3 { }                          test_type3;
+  typedef enum class E4 : unsigned char { c = 1 }    test_type4;
+  typedef enum class E5 : int { a = -1, b = 1 }      test_type5;
+  typedef enum class E6 : long { c = __LONG_MAX__ }  test_type6;  
+
+  template struct underlying_type<test_type1>;
+  template struct underlying_type<test_type2>;
+  template struct underlying_type<test_type3>;
+  template struct underlying_type<test_type4>;
+  template struct underlying_type<test_type5>;
+  template struct underlying_type<test_type6>;
+}