type_traits: Implement is_empty.

2005-01-28  Paolo Carlini  <pcarlini@suse.de>

	* 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
This commit is contained in:
Paolo Carlini 2005-01-28 17:20:43 +00:00 committed by Paolo Carlini
parent 01aa1d43e4
commit 91e390fe3d
4 changed files with 150 additions and 3 deletions

View File

@ -1,3 +1,13 @@
2005-01-28 Paolo Carlini <pcarlini@suse.de>
* 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 <pcarlini@suse.de>
* include/tr1/type_traits: Implement is_abstract, by exploiting the

View File

@ -477,10 +477,38 @@ namespace tr1
remove_all_extents<_Tp>::type>::value)>
{ };
template<typename>
struct __is_empty_helper_1
{ };
template<typename _Tp>
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<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_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<typename _Tp>
struct __is_empty_helper<_Tp, true>
{ static const bool __value = false; };
template<typename _Tp>
struct is_empty
: public integral_constant<bool, __is_empty_helper<_Tp>::__value>
{ };
// Exploit the resolution DR core/337.
template<typename _Tp, bool = (is_void<_Tp>::value
|| is_function<_Tp>::value
|| is_reference<_Tp>::value)>
template<typename _Tp, bool = !is_object<_Tp>::value>
struct __is_abstract_helper
: public __sfinae_types
{

View File

@ -0,0 +1,73 @@
// 2005-01-28 Paolo Carlini <pcarlini@suse.de>
//
// 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 <tr1/type_traits>
#include <testsuite_hooks.h>
#include <testsuite_tr1.h>
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<is_empty, ClassType>(true)) );
VERIFY( (test_category<is_empty, EmptyClassOne>(true)) );
VERIFY( (test_category<is_empty, EmptyClassTwo>(true)) );
VERIFY( (test_category<is_empty, EmptyClassThree>(true)) );
// Negative tests.
VERIFY( (test_category<is_empty, void>(false)) );
VERIFY( (test_category<is_empty, float>(false)) );
VERIFY( (test_category<is_empty, int[4]>(false)) );
VERIFY( (test_category<is_empty, int*>(false)) );
VERIFY( (test_category<is_empty, int&>(false)) );
VERIFY( (test_category<is_empty, int (ClassType::*)>(false)) );
VERIFY( (test_category<is_empty, EnumType>(false)) );
VERIFY( (test_category<is_empty, int (int)>(false)) );
VERIFY( (test_category<is_empty, AbstractClass>(false)) );
VERIFY( (test_category<is_empty, NonEmptyClassOne>(false)) );
VERIFY( (test_category<is_empty, NonEmptyClassTwo>(false)) );
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,36 @@
// 2005-01-28 Paolo Carlini <pcarlini@suse.de>
//
// 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 <tr1/type_traits>
// { dg-do compile }
void test01()
{
// Check for required typedefs
typedef std::tr1::is_empty<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;
}