type_traits: Add is_base_of.

2005-03-04  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/type_traits: Add is_base_of.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_base_of/is_base_of.cc: New.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_base_of/typedefs.cc: Likewise.

From-SVN: r95911
This commit is contained in:
Paolo Carlini 2005-03-05 00:22:48 +00:00 committed by Paolo Carlini
parent ce11b6e146
commit f4e4284dcc
4 changed files with 176 additions and 1 deletions

View File

@ -1,3 +1,11 @@
2005-03-04 Paolo Carlini <pcarlini@suse.de>
* include/tr1/type_traits: Add is_base_of.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_base_of/is_base_of.cc: New.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_base_of/typedefs.cc: Likewise.
2005-03-03 Benjamin Kosnik <bkoz@redhat.com>
* include/tr1/functional: Convert relative path.

View File

@ -42,7 +42,7 @@ namespace std
{
namespace tr1
{
// For use in is_enum, is_abstract and elsewhere.
// For use in __conv_helper, is_abstract and elsewhere.
struct __sfinae_types
{
typedef char __one;
@ -456,6 +456,43 @@ namespace tr1
struct is_same<_Tp, _Tp>
: public true_type { };
// See Daveed Vandevoorde explanation in http://tinyurl.com/502f.
// Also see Rani Sharoni in http://tinyurl.com/6jvyq.
template<typename _Base, typename _Derived,
bool = (!__is_union_or_class<_Base>::value
|| !__is_union_or_class<_Derived>::value
|| is_same<_Base, _Derived>::value)>
struct __is_base_of_helper
: public __sfinae_types
{
private:
typedef typename remove_cv<_Base>::type _NoCv_Base;
typedef typename remove_cv<_Derived>::type _NoCv_Derived;
template<typename _Up>
static __one __test(_NoCv_Derived&, _Up);
static __two __test(_NoCv_Base&, int);
struct _Conv
{
operator _NoCv_Derived&();
operator _NoCv_Base&() const;
};
public:
static const bool __value = sizeof(__test(_Conv(), 0)) == 1;
};
template<typename _Base, typename _Derived>
struct __is_base_of_helper<_Base, _Derived, true>
{ static const bool __value = is_same<_Base, _Derived>::value; };
template<typename _Base, typename _Derived>
struct is_base_of
: public integral_constant<bool,
__is_base_of_helper<_Base, _Derived>::__value>
{ };
template<typename _Tp>
struct __is_int_or_cref
{

View File

@ -0,0 +1,94 @@
// 2005-03-04 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.6 Relationships between types
#include <tr1/type_traits>
#include <testsuite_hooks.h>
#include <testsuite_tr1.h>
class HiddenCons
{
HiddenCons();
HiddenCons(const HiddenCons&);
};
class DerivedHiddenCons
: private HiddenCons
{
DerivedHiddenCons();
DerivedHiddenCons(const DerivedHiddenCons&);
};
class MultiDerivedHiddenCons
: private HiddenCons, private __gnu_test::ClassType
{
MultiDerivedHiddenCons();
MultiDerivedHiddenCons(const MultiDerivedHiddenCons&);
};
void test01()
{
bool test __attribute__((unused)) = true;
using std::tr1::is_base_of;
using namespace __gnu_test;
// Positive tests.
VERIFY( (test_relationship<is_base_of, int, int>(true)) );
VERIFY( (test_relationship<is_base_of, EnumType, EnumType>(true)) );
VERIFY( (test_relationship<is_base_of, UnionType, UnionType>(true)) );
VERIFY( (test_relationship<is_base_of, AbstractClass,
AbstractClass>(true)) );
VERIFY( (test_relationship<is_base_of, ClassType, DerivedType>(true)) );
VERIFY( (test_relationship<is_base_of, ClassType, const DerivedType>(true)) );
VERIFY( (test_relationship<is_base_of, volatile ClassType,
volatile DerivedType>(true)) );
VERIFY( (test_relationship<is_base_of, PolymorphicClass,
DerivedPolymorphic>(true)) );
VERIFY( (test_relationship<is_base_of, HiddenCons,
DerivedHiddenCons>(true)) );
VERIFY( (test_relationship<is_base_of, HiddenCons,
MultiDerivedHiddenCons>(true)) );
VERIFY( (test_relationship<is_base_of, ClassType,
MultiDerivedHiddenCons>(true)) );
// Negative tests.
VERIFY( (test_relationship<is_base_of, int, const int>(false)) );
VERIFY( (test_relationship<is_base_of, volatile UnionType,
UnionType>(false)) );
VERIFY( (test_relationship<is_base_of, int&, ClassType>(false)) );
VERIFY( (test_relationship<is_base_of, AbstractClass, ClassType>(false)) );
VERIFY( (test_relationship<is_base_of, ClassType, AbstractClass>(false)) );
VERIFY( (test_relationship<is_base_of, DerivedType, ClassType>(false)) );
VERIFY( (test_relationship<is_base_of, DerivedPolymorphic,
PolymorphicClass>(false)) );
VERIFY( (test_relationship<is_base_of, DerivedHiddenCons,
HiddenCons>(false)) );
VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
HiddenCons>(false)) );
VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
ClassType>(false)) );
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,36 @@
// 2005-03-04 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_base_of<int, 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;
}