diff options
author | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-03-05 00:22:48 +0000 |
---|---|---|
committer | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-03-05 00:22:48 +0000 |
commit | 84759018ea73f294828768299cbac924870794af (patch) | |
tree | efce5374a01e36396abcf2be3ed9384e24f5efb6 /libstdc++-v3 | |
parent | 39ffba169c09a430c31e8094a17a34cf654a318a (diff) | |
download | gcc-84759018ea73f294828768299cbac924870794af.tar.gz |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@95911 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3')
4 files changed, 176 insertions, 1 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8acc3271c8d..4ec4808e56d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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. diff --git a/libstdc++-v3/include/tr1/type_traits b/libstdc++-v3/include/tr1/type_traits index 37415881c7a..afdea0c2ad2 100644 --- a/libstdc++-v3/include/tr1/type_traits +++ b/libstdc++-v3/include/tr1/type_traits @@ -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 { diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc new file mode 100644 index 00000000000..cac05b2cb3e --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc new file mode 100644 index 00000000000..0223f4d25f4 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc @@ -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; +} |