Make common_type SFINAE-friendly, add tests

This commit is contained in:
Peter Dimov
2015-06-08 15:34:45 +03:00
parent d124da05c8
commit 9fd4ee06b2
5 changed files with 235 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
// Copyright Peter Dimov 2015
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.tt.org/LICENSE_1_0.txt)
#include "test.hpp"
#include "check_type.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/common_type.hpp>
#endif
#include <iostream>
struct X {};
struct Y: X {};
TT_TEST_BEGIN(common_type_6)
{
// binary case
BOOST_CHECK_TYPE3(tt::common_type<void, void>::type, void);
BOOST_CHECK_TYPE3(tt::common_type<int, int>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int&, int&>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<int&, int const&>::type, int);
BOOST_CHECK_TYPE3(tt::common_type<X, X>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X&, X&>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X&, X const&>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X, Y>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X&, Y&>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X const&, Y&>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X&, Y const&>::type, X);
BOOST_CHECK_TYPE3(tt::common_type<X const&, Y const&>::type, X);
}
TT_TEST_END
+39
View File
@@ -0,0 +1,39 @@
// Copyright Peter Dimov 2015
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.tt.org/LICENSE_1_0.txt)
#include "test.hpp"
#include "check_integral_constant.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/common_type.hpp>
# include <boost/type_traits/integral_constant.hpp>
#endif
#include <iostream>
typedef char(&s1)[1];
typedef char(&s2)[2];
template<class T> s1 has_type_impl( typename T::type * );
template<class T> s2 has_type_impl( ... );
template<class T> struct has_type: tt::integral_constant<bool, sizeof(has_type_impl<T>(0)) == sizeof(s1)> {};
struct X {};
struct Y {};
TT_TEST_BEGIN(common_type_sfinae2)
{
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
BOOST_CHECK_INTEGRAL_CONSTANT( (has_type< tt::common_type<int, void*> >::value), false );
BOOST_CHECK_INTEGRAL_CONSTANT( (has_type< tt::common_type<X, Y> >::value), false );
BOOST_CHECK_INTEGRAL_CONSTANT( (has_type< tt::common_type<X&, int const*> >::value), false );
BOOST_CHECK_INTEGRAL_CONSTANT( (has_type< tt::common_type<X, Y, int, void*> >::value), false );
#endif
}
TT_TEST_END
+45
View File
@@ -0,0 +1,45 @@
// Copyright Peter Dimov 2015
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.tt.org/LICENSE_1_0.txt)
#include "test.hpp"
#include "check_type.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/common_type.hpp>
#endif
#include <iostream>
struct X {};
struct Y {};
TT_TEST_BEGIN(common_type_sfinae)
{
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
{
tt::common_type<int, void*> tmp;
(void)tmp;
}
{
tt::common_type<X, Y> tmp;
(void)tmp;
}
{
tt::common_type<X&, int const*> tmp;
(void)tmp;
}
{
tt::common_type<X, Y, int, void*> tmp;
(void)tmp;
}
#endif
}
TT_TEST_END