mirror of https://github.com/CGAL/cgal
Add CGAL::is_convertible_without_narrowing
... and `CGAL::cpp20::type_identity`.
This commit is contained in:
parent
1d5851e013
commit
ca1f11d715
|
|
@ -29,6 +29,12 @@ struct is_same_or_derived :
|
|||
|
||||
namespace cpp20 {
|
||||
|
||||
template<class T>
|
||||
struct type_identity { using type = T; };
|
||||
|
||||
template<class T>
|
||||
using type_identity_t = typename type_identity<T>::type;
|
||||
|
||||
template< class T >
|
||||
struct remove_cvref {
|
||||
typedef std::remove_cv_t<std::remove_reference_t<T>> type;
|
||||
|
|
@ -39,6 +45,26 @@ namespace cpp20 {
|
|||
|
||||
} // end namespace cpp20
|
||||
|
||||
namespace details {
|
||||
template <typename From, typename To, typename = void>
|
||||
struct is_convertible_without_narrowing : std::false_type
|
||||
{};
|
||||
|
||||
template <typename From, typename To>
|
||||
struct is_convertible_without_narrowing<From,
|
||||
To,
|
||||
std::void_t<decltype(cpp20::type_identity_t<To[]>{std::declval<From>()})>>
|
||||
: std::is_convertible<From, To>
|
||||
{};
|
||||
}
|
||||
|
||||
template <typename From, typename To>
|
||||
struct is_convertible_without_narrowing : details::is_convertible_without_narrowing<From, To>
|
||||
{};
|
||||
|
||||
template <typename From, typename To>
|
||||
inline constexpr bool is_convertible_without_narrowing_v = is_convertible_without_narrowing<From, To>::value;
|
||||
|
||||
} // end namespace CGAL
|
||||
|
||||
#endif // CGAL_TYPE_TRAITS_H
|
||||
|
|
|
|||
|
|
@ -18,14 +18,22 @@ struct B : public A {};
|
|||
typedef A C;
|
||||
|
||||
int main() {
|
||||
assert( ( ::CGAL::is_same_or_derived< A,A >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< A,B >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< B,A >::value == 0 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< B,B >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< A,C >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< B,C >::value == 0 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< C,C >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< C,A >::value == 1 ) );
|
||||
assert( ( ::CGAL::is_same_or_derived< C,B >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< A,A >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< A,B >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< B,A >::value == 0 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< B,B >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< A,C >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< B,C >::value == 0 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< C,C >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< C,A >::value == 1 ) );
|
||||
static_assert( ( ::CGAL::is_same_or_derived< C,B >::value == 1 ) );
|
||||
|
||||
static_assert( CGAL::is_convertible_without_narrowing_v<int, int> );
|
||||
static_assert( ! CGAL::is_convertible_without_narrowing_v<int, signed char> );
|
||||
static_assert( CGAL::is_convertible_without_narrowing_v<signed char, int> );
|
||||
static_assert( ! CGAL::is_convertible_without_narrowing_v<int, double> );
|
||||
static_assert( CGAL::is_convertible_without_narrowing_v<float, double> );
|
||||
static_assert( ! CGAL::is_convertible_without_narrowing_v<double, float> );
|
||||
static_assert( ! CGAL::is_convertible_without_narrowing_v<A, int> );
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue