This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] tr1/type_traits: is_enum
- From: Paolo Carlini <pcarlini at suse dot de>
- To: "'gcc-patches at gcc dot gnu dot org'" <gcc-patches at gcc dot gnu dot org>
- Date: Sat, 25 Dec 2004 16:28:41 +0100
- Subject: [v3] tr1/type_traits: is_enum
Hi,
"usual" caveats about c++/19076... Tested x86-linux, committed.
M C!
Paolo.
///////////////
2004-12-25 Paolo Carlini <pcarlini@suse.de>
* include/tr1/type_traits: Implement is_enum (usual caveats about
the nasty consequences of c++/19076...).
* testsuite/testsuite_tr1.h: Add ConvType.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_scalar/is_scalar.cc: New.
* testsuite/tr1/4_metaprogramming/composite_type_traits/
is_scalar/typedefs.cc: Likewise.
* testsuite/tr1/4_metaprogramming/primary_type_categories/
is_enum/is_enum.cc: Likewise.
* testsuite/tr1/4_metaprogramming/primary_type_categories/
is_enum/typedefs.cc: Likewise.
diff -urN libstdc++-v3-orig/include/tr1/type_traits libstdc++-v3/include/tr1/type_traits
--- libstdc++-v3-orig/include/tr1/type_traits 2004-12-25 00:19:19.000000000 +0100
+++ libstdc++-v3/include/tr1/type_traits 2004-12-25 12:45:10.000000000 +0100
@@ -33,7 +33,7 @@
{
namespace tr1
{
- // For use in is_function and elsewhere.
+ // For use in is_enum, is_function, and elsewhere.
struct __sfinae_types
{
typedef char __one;
@@ -149,14 +149,57 @@
: public false_type { };
_DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*)
+ 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_function<_Tp>::value)>
+ struct __is_enum_helper
+ : public __sfinae_types
+ {
+ private:
+ static __one __test(bool);
+ static __one __test(char);
+ static __one __test(signed char);
+ static __one __test(unsigned char);
+#ifdef _GLIBCXX_USE_WCHAR_T
+ static __one __test(wchar_t);
+#endif
+ static __one __test(short);
+ static __one __test(unsigned short);
+ static __one __test(int);
+ static __one __test(unsigned int);
+ static __one __test(long);
+ static __one __test(unsigned long);
+ static __one __test(long long);
+ static __one __test(unsigned long long);
+ static __two __test(...);
+
+ template<typename _Up>
+ struct __convert
+ { operator _Up() const; };
+
+ public:
+ static const bool __value = sizeof(__test(__convert<_Tp>())) == 1;
+ };
+
template<typename _Tp>
+ struct __is_enum_helper<_Tp, true>
+ { static const bool __value = false; };
+
+ template<typename _Tp>
+ struct is_enum
+ : 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]);
@@ -165,11 +208,12 @@
};
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
- && !is_reference<_Tp>::value
- && !is_void<_Tp>::value)>
- { };
+ : public integral_constant<bool, __is_function_helper<_Tp>::__value> { };
/// @brief composite type traits [4.5.2].
template<typename _Tp>
diff -urN libstdc++-v3-orig/testsuite/testsuite_tr1.h libstdc++-v3/testsuite/testsuite_tr1.h
--- libstdc++-v3-orig/testsuite/testsuite_tr1.h 2004-12-24 20:59:21.000000000 +0100
+++ libstdc++-v3/testsuite/testsuite_tr1.h 2004-12-25 15:38:17.000000000 +0100
@@ -78,6 +78,12 @@
typedef const ClassType cClassType;
typedef volatile ClassType vClassType;
typedef const volatile ClassType cvClassType;
+
+ enum EnumType { };
+
+ struct ConvType
+ { operator int() const; };
+
}; // namespace __gnu_test
#endif // _GLIBCXX_TESTSUITE_TR1_H
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/is_scalar.cc 2004-12-25 15:05:12.000000000 +0100
@@ -0,0 +1,50 @@
+// 2004-12-25 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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.2 Composite type traits
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+ bool test __attribute__((unused)) = true;
+ using std::tr1::is_scalar;
+ using namespace __gnu_test;
+
+ VERIFY( (test_category<is_scalar, int>(true)) );
+ VERIFY( (test_category<is_scalar, float>(true)) );
+ VERIFY( (test_category<is_scalar, EnumType>(true)) );
+ VERIFY( (test_category<is_scalar, int*>(true)) );
+ VERIFY( (test_category<is_scalar, int(*)(int)>(true)) );
+ VERIFY( (test_category<is_scalar, int (ClassType::*)>(true)) );
+ // Temporarily disabled because of c++/19076 :-(
+ // VERIFY( (test_category<is_scalar, int (ClassType::*) (int)>(true)) );
+
+ // Sanity check.
+ VERIFY( (test_category<is_scalar, ClassType>(false)) );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/composite_type_traits/is_scalar/typedefs.cc 2004-12-25 14:56:54.000000000 +0100
@@ -0,0 +1,36 @@
+// 2004-12-25 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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_scalar<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;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/is_enum.cc 2004-12-25 15:38:53.000000000 +0100
@@ -0,0 +1,60 @@
+// 2004-12-25 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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.1 Primary type categories
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+ bool test __attribute__((unused)) = true;
+ using std::tr1::is_enum;
+ using namespace __gnu_test;
+
+ // Positive tests.
+ VERIFY( (test_category<is_enum, EnumType>(true)) );
+
+ // Negative tests.
+ VERIFY( (test_category<is_enum, void>(false)) );
+ VERIFY( (test_category<is_enum, int>(false)) );
+ VERIFY( (test_category<is_enum, float>(false)) );
+ VERIFY( (test_category<is_enum, int[2]>(false)) );
+ VERIFY( (test_category<is_enum, int*>(false)) );
+ VERIFY( (test_category<is_enum, int(*)(int)>(false)) );
+ VERIFY( (test_category<is_enum, float&>(false)) );
+ VERIFY( (test_category<is_enum, float(&)(float)>(false)) );
+ VERIFY( (test_category<is_enum, int (ClassType::*)>(false)) );
+ // Temporarily disabled because of c++/19076 :-(
+ // VERIFY( (test_category<is_enum, int (ClassType::*) (int)>(false)) );
+ VERIFY( (test_category<is_enum, int (int)>(false)) );
+
+ VERIFY( (test_category<is_enum, ConvType>(false)) );
+
+ // Sanity check.
+ VERIFY( (test_category<is_enum, ClassType>(false)) );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_enum/typedefs.cc 2004-12-25 14:45:30.000000000 +0100
@@ -0,0 +1,36 @@
+// 2004-12-25 Paolo Carlini <pcarlini@suse.de>
+//
+// Copyright (C) 2004 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_enum<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;
+}