Fix comon_type to actually work when typeof isn't in use.

Applies fixes from Vicente Botet.

[SVN r68040]
This commit is contained in:
John Maddock
2011-01-12 13:29:37 +00:00
parent 459e0bb6a0
commit c02516b25b
4 changed files with 390 additions and 18 deletions

View File

@ -72,21 +72,45 @@ struct propagate_cv< const volatile From, To >
{ typedef To const volatile type; };
/*******************************************************************************
* struct is_signable_integral<T>
* struct is_integral_or_enum<T>
*
* This metafunction determines if T is an integral type which can be made
* signed or unsigned.
******************************************************************************/
template< class T >
struct is_signable_integral
struct is_integral_or_enum
: mpl::or_< is_integral<T>, is_enum<T> >
{ };
template<>
struct is_signable_integral< bool >
struct is_integral_or_enum< bool >
: false_type
{ };
/*******************************************************************************
* struct make_unsigned_soft<T>
* struct make_signed_soft<T>
*
* These metafunction are identical to make_unsigned and make_signed,
* respetively, except for special-casing bool.
******************************************************************************/
template< class T >
struct make_unsigned_soft
: make_unsigned<T>
{ };
template<>
struct make_unsigned_soft< bool >
{ typedef bool type; };
template< class T >
struct make_signed_soft
: make_signed<T>
{ };
template<>
struct make_signed_soft< bool >
{ typedef bool type; };
/*******************************************************************************
* struct sizeof_t<N>
* typedef ... yes_type
@ -185,12 +209,21 @@ struct select< Sequence, N, N >
* { V*, W*, V'*, W'* }
* where V' is V with whatever cv-qualifiers are on W, and W' is W
* with whatever cv-qualifiers are on V
* else T' and U' are both "signable integral types" (integral and enum
* types excepting bool), then:
* else if T' and U' are both integral or enum types, then:
* define the set of NominalCandidates to be
* { unsigned(T'), unsigned(U'), signed(T'), signed(U') }
* where unsigned(X) is make_unsigned<X>::type and signed(X) is
* make_signed<X>::type
* {
* unsigned_soft(T'),
* unsigned_soft(U'),
* signed_soft(T'),
* signed_soft(U'),
* T',
* U',
* unsigned int,
* int
* }
* where unsigned_soft(X) is make_unsigned_soft<X>::type and
* signed_soft(X) is make_signed_soft<X>::type (these are all
* generally necessary to cover the various integral promotion cases)
* else
* define the set of NominalCandidates to be
* { T', U' }
@ -231,22 +264,20 @@ template<
class T, class U,
class V = typename remove_cv< typename remove_reference<T>::type >::type,
class W = typename remove_cv< typename remove_reference<U>::type >::type,
bool = is_signable_integral<V>::value && is_signable_integral<W>::value
bool = is_integral_or_enum<V>::value && is_integral_or_enum<W>::value
>
struct nominal_candidates;
template< class T, class U, class V, class W >
struct nominal_candidates< T, U, V, W, false >
struct nominal_candidates
{ typedef mpl::vector2<V,W> type; };
template< class T, class U, class V, class W >
struct nominal_candidates< T, U, V, W, true >
{
typedef mpl::vector4<
typename make_unsigned<V>::type,
typename make_unsigned<W>::type,
typename make_signed<V>::type,
typename make_signed<W>::type
typedef boost::mpl::vector8<
typename make_unsigned_soft<V>::type,
typename make_unsigned_soft<W>::type,
typename make_signed_soft<V>::type,
typename make_signed_soft<W>::type,
V, W, unsigned int, int
> type;
};

View File

@ -40,6 +40,32 @@ do{\
<< BOOST_STRINGIZE(expression) << "\"");\
}while(0)
#define BOOST_CHECK_TYPE3(type_expression, type_expression_suffix, expected_type)\
do{\
if(!::boost::is_same< type_expression, type_expression_suffix, expected_type >::value){\
BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression)\
<< "\" did not have the expected type:\n\tevaluating: boost::is_same<"\
<< BOOST_STRINGIZE((type_expression, type_expression_suffix)) << ", " << BOOST_STRINGIZE(expected_type)\
<< ">" << "\n\tfound: "\
<< typeid(::boost::is_same< type_expression, type_expression_suffix, expected_type >).name());\
}else\
BOOST_CHECK_MESSAGE(true, "Validating Type Expression: \""\
<< BOOST_STRINGIZE(expression) << "\"");\
}while(0)
#define BOOST_CHECK_TYPE4(type_expression, suffix1, suffix2, expected_type)\
do{\
if(!::boost::is_same< type_expression, suffix1, suffix2, expected_type >::value){\
BOOST_CHECK_MESSAGE(false, "The expression: \"" << BOOST_STRINGIZE(expression)\
<< "\" did not have the expected type:\n\tevaluating: boost::is_same<"\
<< BOOST_STRINGIZE((type_expression, suffix1, suffix2)) << ", " << BOOST_STRINGIZE(expected_type)\
<< ">" << "\n\tfound: "\
<< typeid(::boost::is_same< type_expression, suffix1, suffix2, expected_type >).name());\
}else\
BOOST_CHECK_MESSAGE(true, "Validating Type Expression: \""\
<< BOOST_STRINGIZE(expression) << "\"");\
}while(0)
#endif

212
test/common_type_2_test.cpp Normal file
View File

@ -0,0 +1,212 @@
// common_type_test.cpp ----------------------------------------------------//
// Copyright 2010 Beman Dawes
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
#define BOOST_COMMON_TYPE_DONT_USE_TYPEOF 1
#include "test.hpp"
#include "check_type.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/common_type.hpp>
#endif
#include <iostream>
#ifdef BOOST_INTEL
#pragma warning(disable: 304 383)
#endif
struct C1 {};
struct C2 {};
struct C3 : C2 {};
struct C1C2 {
C1C2() {}
C1C2(C1 const&) {}
C1C2(C2 const&) {}
C1C2& operator=(C1C2 const&) {
return *this;
}
};
template <typename C, typename A>
void proc2(typename boost::common_type<A, C>::type const& ) {}
template <typename C, typename A, typename B>
void proc3(typename boost::common_type<C, A, B>::type const& ) {}
template <typename C, typename A>
void assignation_2() {
typedef typename boost::common_type<A, C>::type AC;
A a;
C c;
AC ac;
ac=a;
ac=c;
proc2<C, A>(a);
proc2<C, A>(c);
}
template <typename C, typename A, typename B>
void assignation_3() {
typedef typename boost::common_type<C, A, B>::type ABC;
A a;
B b;
C c;
ABC abc;
abc=a;
abc=b;
abc=c;
proc3<C, A, B>(a);
proc3<C, A, B>(b);
proc3<C, A, B>(c);
}
C1C2 c1c2;
C1 c1;
int f(C1C2 ) { return 1;}
int f(C1 ) { return 2;}
template <typename OSTREAM>
OSTREAM& operator<<(OSTREAM& os, C1 const&) {return os;}
C1C2& declval_C1C2() {return c1c2;}
C1& declval_C1(){return c1;}
bool declval_bool(){return true;}
TT_TEST_BEGIN(common_type)
{
assignation_2<C1C2, C1>();
typedef tt::common_type<C1C2&, C1&>::type T1;
typedef tt::common_type<C3*, C2*>::type T2;
typedef tt::common_type<int*, int const*>::type T3;
#if defined(BOOST_NO_DECLTYPE) && !defined(BOOST_COMMON_TYPE_DONT_USE_TYPEOF)
// fails if BOOST_COMMON_TYPE_DONT_USE_TYPEOF:
typedef tt::common_type<int volatile*, int const*>::type T4;
#endif
typedef tt::common_type<int*, int volatile*>::type T5;
assignation_2<C1, C1C2>();
assignation_2<C1C2, C2>();
assignation_2<C2, C1C2>();
assignation_3<C1, C1C2, C2>();
assignation_3<C1C2, C1, C2>();
assignation_3<C2, C1C2, C1>();
assignation_3<C1C2, C2, C1>();
//assignation_3<C1, C2, C1C2>(); // fails because the common type is the third
BOOST_CHECK_TYPE(tt::common_type<int>::type, int);
BOOST_CHECK_TYPE(tt::common_type<char>::type, char);
BOOST_CHECK_TYPE3(tt::common_type<char, char>::type, char);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<char, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<char, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned char>::type, unsigned char);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<short, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, short>::type, short);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<short, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<short, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned short>::type, unsigned short);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<int, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<int, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<int, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, char>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned char>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, short>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned short>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, int>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, double>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, char>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned char>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, short>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned short>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, int>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned int>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::ulong_long_type>::type, boost::ulong_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, double>::type, double);
#endif
BOOST_CHECK_TYPE3(tt::common_type<double, char>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned char>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, short>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned short>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, int>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned int>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<double, boost::long_long_type>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, boost::ulong_long_type>::type, double);
#endif
BOOST_CHECK_TYPE3(tt::common_type<double, double>::type, double);
BOOST_CHECK_TYPE4(tt::common_type<double, char, int>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE4(tt::common_type<unsigned, char, boost::long_long_type>::type, boost::long_long_type);
#endif
}
TT_TEST_END

View File

@ -103,5 +103,108 @@ TT_TEST_BEGIN(common_type)
assignation_3<C2, C1C2, C1>();
assignation_3<C1C2, C2, C1>();
//assignation_3<C1, C2, C1C2>(); // fails because the common type is the third
BOOST_CHECK_TYPE(tt::common_type<int>::type, int);
BOOST_CHECK_TYPE(tt::common_type<char>::type, char);
BOOST_CHECK_TYPE3(tt::common_type<char, char>::type, char);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<char, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<char, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned char>::type, unsigned char);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned char, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<short, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, short>::type, short);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<short, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<short, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned short>::type, unsigned short);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned short, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<int, char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned char>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned short>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<int, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<int, double>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, char>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned char>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, short>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned short>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, int>::type, unsigned int);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, unsigned int>::type, unsigned int);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, boost::ulong_long_type>::type, boost::ulong_long_type);
#endif
BOOST_CHECK_TYPE3(tt::common_type<unsigned int, double>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, char>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned char>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, short>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned short>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, int>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, unsigned int>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::long_long_type>::type, boost::long_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, boost::ulong_long_type>::type, boost::ulong_long_type);
BOOST_CHECK_TYPE3(tt::common_type<boost::long_long_type, double>::type, double);
#endif
BOOST_CHECK_TYPE3(tt::common_type<double, char>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned char>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, short>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned short>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, int>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, unsigned int>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE3(tt::common_type<double, boost::long_long_type>::type, double);
BOOST_CHECK_TYPE3(tt::common_type<double, boost::ulong_long_type>::type, double);
#endif
BOOST_CHECK_TYPE3(tt::common_type<double, double>::type, double);
BOOST_CHECK_TYPE4(tt::common_type<double, char, int>::type, double);
#ifndef BOOST_NO_LONG_LONG
BOOST_CHECK_TYPE4(tt::common_type<unsigned, char, boost::long_long_type>::type, boost::long_long_type);
#endif
}
TT_TEST_END