Commit c150a271 by Paolo Carlini Committed by Paolo Carlini

type_traits: Implement is_convertible.

2005-02-23  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/type_traits: Implement is_convertible.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_convertible/is_convertible.cc: New.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_convertible/typedefs.cc: Likewise.
	* testsuite/testsuite_tr1.h: Add class DerivedType.

	* include/tr1/type_traits (is_function): Don't mistake references
	to function types for function types.
	* testsuite/tr1/4_metaprogramming/primary_type_categories/
	is_function/is_function.cc: Add testcase.

From-SVN: r95458
parent f439764f
2005-02-23 Paolo Carlini <pcarlini@suse.de>
* include/tr1/type_traits: Implement is_convertible.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_convertible/is_convertible.cc: New.
* testsuite/tr1/4_metaprogramming/relationships_between_types/
is_convertible/typedefs.cc: Likewise.
* testsuite/testsuite_tr1.h: Add class DerivedType.
* include/tr1/type_traits (is_function): Don't mistake references
to function types for function types.
* testsuite/tr1/4_metaprogramming/primary_type_categories/
is_function/is_function.cc: Add testcase.
2005-02-22 Benjamin Kosnik <bkoz@redhat.com>
* scripts/check_performance: Tweaks.
......
......@@ -208,7 +208,8 @@ namespace tr1
struct is_enum
: public integral_constant<bool, __is_enum_helper<_Tp>::__value> { };
template<typename _Tp, bool = is_void<_Tp>::value>
template<typename _Tp, bool = (is_void<_Tp>::value
|| is_reference<_Tp>::value)>
struct __is_function_helper
{
static const bool __value = (__conv_helper<typename
......@@ -440,6 +441,39 @@ namespace tr1
struct is_same<_Tp, _Tp>
: public true_type { };
template<typename _Tp>
struct __is_int_or_cref
{
typedef typename remove_reference<_Tp>::type __rr_Tp;
static const bool __value = (is_integral<_Tp>::value
|| (is_integral<__rr_Tp>::value
&& is_const<__rr_Tp>::value
&& !is_volatile<__rr_Tp>::value));
};
template<typename _From, typename _To,
bool = (is_function<_To>::value || is_array<_To>::value
// This special case is here only to avoid warnings.
|| (is_floating_point<typename
remove_reference<_From>::type>::value
&& __is_int_or_cref<_To>::__value))>
struct __is_convertible_helper
{
// "An imaginary lvalue of type From...".
static const bool __value = (__conv_helper<typename
add_reference<_From>::type, _To>::__value);
};
template<typename _From, typename _To>
struct __is_convertible_helper<_From, _To, true>
{ static const bool __value = __is_int_or_cref<_To>::__value; };
template<typename _From, typename _To>
struct is_convertible
: public integral_constant<bool,
__is_convertible_helper<_From, _To>::__value>
{ };
/// @brief const-volatile modifications [4.7.1].
template<typename _Tp>
struct remove_const
......
......@@ -113,6 +113,8 @@ namespace __gnu_test
typedef volatile ClassType vClassType;
typedef const volatile ClassType cvClassType;
class DerivedType : public ClassType { };
enum EnumType { };
struct ConvType
......
......@@ -41,6 +41,7 @@ void test01()
VERIFY( (test_category<is_function, const void>(false)) );
VERIFY( (test_category<is_function, AbstractClass>(false)) );
VERIFY( (test_category<is_function, int(&)(int)>(false)) );
// Sanity check.
VERIFY( (test_category<is_function, ClassType>(false)) );
......
// 2005-02-23 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>
void test01()
{
bool test __attribute__((unused)) = true;
using std::tr1::is_convertible;
using namespace __gnu_test;
// Positive tests.
VERIFY( (test_relationship<is_convertible, int, int>(true)) );
VERIFY( (test_relationship<is_convertible, int, const int>(true)) );
VERIFY( (test_relationship<is_convertible, volatile int, const int>(true)) );
VERIFY( (test_relationship<is_convertible, int, float>(true)) );
VERIFY( (test_relationship<is_convertible, double, float>(true)) );
VERIFY( (test_relationship<is_convertible, float, int>(true)) );
VERIFY( (test_relationship<is_convertible, int*, const int*>(true)) );
VERIFY( (test_relationship<is_convertible, int*, void*>(true)) );
VERIFY( (test_relationship<is_convertible, int[4], int*>(true)) );
VERIFY( (test_relationship<is_convertible, float&, int>(true)) );
VERIFY( (test_relationship<is_convertible, int, const int&>(true)) );
VERIFY( (test_relationship<is_convertible, const int&, int>(true)) );
VERIFY( (test_relationship<is_convertible, float, const int&>(true)) );
VERIFY( (test_relationship<is_convertible, float, volatile float&>(true)) );
VERIFY( (test_relationship<is_convertible, int(int), int(*)(int)>(true)) );
VERIFY( (test_relationship<is_convertible, int(int), int(&)(int)>(true)) );
VERIFY( (test_relationship<is_convertible, int(&)(int), int(*)(int)>(true)) );
VERIFY( (test_relationship<is_convertible, EnumType, int>(true)) );
VERIFY( (test_relationship<is_convertible, ClassType, ClassType>(true)) );
VERIFY( (test_relationship<is_convertible, DerivedType, ClassType>(true)) );
VERIFY( (test_relationship<is_convertible, DerivedType*, ClassType*>(true)) );
VERIFY( (test_relationship<is_convertible, DerivedType&, ClassType&>(true)) );
// Negative tests.
VERIFY( (test_relationship<is_convertible, const int*, int*>(false)) );
VERIFY( (test_relationship<is_convertible, int*, float*>(false)) );
VERIFY( (test_relationship<is_convertible, const int[4], int*>(false)) );
VERIFY( (test_relationship<is_convertible, int[4], int[4]>(false)) );
VERIFY( (test_relationship<is_convertible, const int&, int&>(false)) );
VERIFY( (test_relationship<is_convertible, float&, int&>(false)) );
VERIFY( (test_relationship<is_convertible, float, volatile int&>(false)) );
VERIFY( (test_relationship<is_convertible, int(int), int(int)>(false)) );
VERIFY( (test_relationship<is_convertible, int(int), int(*)(void)>(false)) );
VERIFY( (test_relationship<is_convertible, int(*)(int), int(&)(int)>(false)) );
VERIFY( (test_relationship<is_convertible, int, EnumType>(false)) );
VERIFY( (test_relationship<is_convertible, int, ClassType>(false)) );
VERIFY( (test_relationship<is_convertible, ClassType, DerivedType>(false)) );
VERIFY( (test_relationship<is_convertible, ClassType*, DerivedType*>(false)) );
VERIFY( (test_relationship<is_convertible, ClassType&, DerivedType&>(false)) );
}
int main()
{
test01();
return 0;
}
// 2005-02-23 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_convertible<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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment