From 91e390fe3dd2e1ec29b6742c69a83d687f135657 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 28 Jan 2005 17:20:43 +0000 Subject: [PATCH] type_traits: Implement is_empty. 2005-01-28 Paolo Carlini * include/tr1/type_traits: Implement is_empty. * testsuite/tr1/4_metaprogramming/type_properties/is_empty/ is_empty.cc: New. * testsuite/tr1/4_metaprogramming/type_properties/is_empty/ typedefs.cc: Likewise. * include/tr1/type_traits (__is_abstract_helper): Simplify a bit. From-SVN: r94379 --- libstdc++-v3/ChangeLog | 10 +++ libstdc++-v3/include/tr1/type_traits | 34 ++++++++- .../type_properties/is_empty/is_empty.cc | 73 +++++++++++++++++++ .../type_properties/is_empty/typedefs.cc | 36 +++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f9b1de548ba6..eda22a8d5675 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2005-01-28 Paolo Carlini + + * include/tr1/type_traits: Implement is_empty. + * testsuite/tr1/4_metaprogramming/type_properties/is_empty/ + is_empty.cc: New. + * testsuite/tr1/4_metaprogramming/type_properties/is_empty/ + typedefs.cc: Likewise. + + * include/tr1/type_traits (__is_abstract_helper): Simplify a bit. + 2005-01-28 Paolo Carlini * include/tr1/type_traits: Implement is_abstract, by exploiting the diff --git a/libstdc++-v3/include/tr1/type_traits b/libstdc++-v3/include/tr1/type_traits index d75e5dffb103..753722941f9d 100644 --- a/libstdc++-v3/include/tr1/type_traits +++ b/libstdc++-v3/include/tr1/type_traits @@ -477,10 +477,38 @@ namespace tr1 remove_all_extents<_Tp>::type>::value)> { }; + template + struct __is_empty_helper_1 + { }; + + template + struct __is_empty_helper_2 + : public _Tp { }; + + // Unfortunately, without compiler support we cannot tell union from + // class types, and is_empty doesn't work at all with the former. + template::value + || is_array<_Tp>::value + || is_pointer<_Tp>::value + || is_reference<_Tp>::value + || is_member_pointer<_Tp>::value + || is_enum<_Tp>::value + || is_function<_Tp>::value)> + struct __is_empty_helper + { static const bool __value = (sizeof(__is_empty_helper_1<_Tp>) + == sizeof(__is_empty_helper_2<_Tp>)); }; + + template + struct __is_empty_helper<_Tp, true> + { static const bool __value = false; }; + + template + struct is_empty + : public integral_constant::__value> + { }; + // Exploit the resolution DR core/337. - template::value - || is_function<_Tp>::value - || is_reference<_Tp>::value)> + template::value> struct __is_abstract_helper : public __sfinae_types { diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc new file mode 100644 index 000000000000..861105498aad --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/is_empty.cc @@ -0,0 +1,73 @@ +// 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 + +class EmptyClassOne +{ typedef int type; }; + +class EmptyClassTwo +{ static int data; }; + +class EmptyClassThree +{ int f(); }; + +class NonEmptyClassOne +{ int data; }; + +class NonEmptyClassTwo +{ virtual int f(); }; + +void test01() +{ + bool test __attribute__((unused)) = true; + using std::tr1::is_empty; + using namespace __gnu_test; + + // Positive tests. + VERIFY( (test_category(true)) ); + VERIFY( (test_category(true)) ); + VERIFY( (test_category(true)) ); + VERIFY( (test_category(true)) ); + + // Negative tests. + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); + VERIFY( (test_category(false)) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/typedefs.cc new file mode 100644 index 000000000000..9ffc041606dc --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/is_empty/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_empty 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; +}