From f8023b78462af97c3fe7a6b7786a9c19c82107d9 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 28 Jan 2005 13:29:41 +0000 Subject: [PATCH] type_traits: Implement is_abstract, by exploiting the resolution of DR core/337. 2005-01-28 Paolo Carlini * include/tr1/type_traits: Implement is_abstract, by exploiting the resolution of DR core/337. * testsuite/testsuite_tr1.h: Add AbstractClass. * testsuite/tr1/4_metaprogramming/type_properties/is_abstract/ is_abstract.cc: New. * testsuite/tr1/4_metaprogramming/type_properties/is_abstract/ typedefs.cc: Likewise. * include/tr1/type_traits (is_function): Rewrite, use the conversion F& -> F* instead, thus avoiding problems with abstract classes. * testsuite/tr1/4_metaprogramming/primary_type_categories/ is_function/is_function.cc: Add a test for tricky AbstractClass. From-SVN: r94370 --- libstdc++-v3/ChangeLog | 15 +++++ libstdc++-v3/include/tr1/type_traits | 65 +++++++++++++------ libstdc++-v3/testsuite/testsuite_tr1.h | 5 +- .../is_function/is_function.cc | 4 +- .../is_abstract/is_abstract.cc | 49 ++++++++++++++ .../type_properties/is_abstract/typedefs.cc | 36 ++++++++++ 6 files changed, 151 insertions(+), 23 deletions(-) create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2742f60f91d0..f9b1de548ba6 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2005-01-28 Paolo Carlini + + * include/tr1/type_traits: Implement is_abstract, by exploiting the + resolution of DR core/337. + * testsuite/testsuite_tr1.h: Add AbstractClass. + * testsuite/tr1/4_metaprogramming/type_properties/is_abstract/ + is_abstract.cc: New. + * testsuite/tr1/4_metaprogramming/type_properties/is_abstract/ + typedefs.cc: Likewise. + + * include/tr1/type_traits (is_function): Rewrite, use the conversion + F& -> F* instead, thus avoiding problems with abstract classes. + * testsuite/tr1/4_metaprogramming/primary_type_categories/ + is_function/is_function.cc: Add a test for tricky AbstractClass. + 2005-01-26 Paolo Carlini * include/ext/mt_allocator.h diff --git a/libstdc++-v3/include/tr1/type_traits b/libstdc++-v3/include/tr1/type_traits index 34c1c9fd076e..d75e5dffb103 100644 --- a/libstdc++-v3/include/tr1/type_traits +++ b/libstdc++-v3/include/tr1/type_traits @@ -33,13 +33,26 @@ namespace std { namespace tr1 { - // For use in is_enum, is_function, and elsewhere. + // For use in is_enum, is_abstract and elsewhere. struct __sfinae_types { typedef char __one; typedef struct { char __arr[2]; } __two; }; + template + struct __conv_helper + : public __sfinae_types + { + private: + static __one __test(_To); + static __two __test(...); + static _From __makeFrom(); + + public: + static const bool __value = sizeof(__test(__makeFrom())) == 1; + }; + #define _DEFINE_SPEC_BODY(_Value) \ : public integral_constant { }; @@ -393,28 +406,13 @@ namespace tr1 struct is_enum : public integral_constant::__value> { }; - template::value - || is_void<_Tp>::value)> - struct __is_function_helper - : public __sfinae_types - { - private: - template - static __one __test(...); - template - static __two __test(_Up(*)[1]); - - public: - static const bool __value = sizeof(__test<_Tp>(0)) == 1; - }; - - template - struct __is_function_helper<_Tp, true> - { static const bool __value = false; }; - template struct is_function - : public integral_constant::__value> { }; + : public integral_constant::type, + typename add_pointer<_Tp>::type>::__value)> + { }; + _DEFINE_SPEC(0, is_function, void, false) /// @brief composite type traits [4.5.2]. template @@ -479,6 +477,31 @@ namespace tr1 remove_all_extents<_Tp>::type>::value)> { }; + // Exploit the resolution DR core/337. + template::value + || is_function<_Tp>::value + || is_reference<_Tp>::value)> + struct __is_abstract_helper + : public __sfinae_types + { + private: + template + static __one __test(...); + template + static __two __test(_Up(*)[1]); + + public: + static const bool __value = sizeof(__test<_Tp>(0)) == 1; + }; + + template + struct __is_abstract_helper<_Tp, true> + { static const bool __value = false; }; + + template + struct is_abstract + : public integral_constant::__value> { }; + template struct has_trivial_constructor : public integral_constant::value> { }; diff --git a/libstdc++-v3/testsuite/testsuite_tr1.h b/libstdc++-v3/testsuite/testsuite_tr1.h index aa7855c7e43d..74a187666fd1 100644 --- a/libstdc++-v3/testsuite/testsuite_tr1.h +++ b/libstdc++-v3/testsuite/testsuite_tr1.h @@ -1,7 +1,7 @@ // -*- C++ -*- // Testing utilities for the tr1 testsuite. // -// Copyright (C) 2004 Free Software Foundation, Inc. +// Copyright (C) 2004, 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 @@ -117,6 +117,9 @@ namespace __gnu_test struct ConvType { operator int() const; }; + + class AbstractClass + { virtual void rotate(int) = 0; }; }; // namespace __gnu_test diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc index 0f79758b58d5..4bbda75280b7 100644 --- a/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/primary_type_categories/is_function/is_function.cc @@ -1,6 +1,6 @@ // 2004-12-16 Paolo Carlini // -// Copyright (C) 2004 Free Software Foundation, Inc. +// Copyright (C) 2004, 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 @@ -39,6 +39,8 @@ void test01() VERIFY( (test_category(false)) ); VERIFY( (test_category(false)) ); VERIFY( (test_category(false)) ); + + VERIFY( (test_category(false)) ); // Sanity check. VERIFY( (test_category(false)) ); diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc new file mode 100644 index 000000000000..70e3ac8277a2 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/is_abstract.cc @@ -0,0 +1,49 @@ +// 2005-01-28 Paolo Carlini +// +// 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 +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + using std::tr1::is_abstract; + using namespace __gnu_test; + + // Positive tests. + VERIFY( (test_category(true)) ); + + // Negative tests. + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + + // Sanity check. + VERIFY( (test_category(false)) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc new file mode 100644 index 000000000000..c781c550ce0e --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_abstract/typedefs.cc @@ -0,0 +1,36 @@ +// 2005-01-28 Paolo Carlini +// +// 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 + +// { dg-do compile } + +void test01() +{ + // Check for required typedefs + typedef std::tr1::is_abstract 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; +}