Move __lazy_* metafunctions to type traits and add tests

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@246408 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier 2015-08-31 03:50:31 +00:00
parent 408369b7c9
commit cae4caba73
4 changed files with 200 additions and 26 deletions

View File

@ -136,31 +136,6 @@ get(array<_Tp, _Size>&&) _NOEXCEPT;
#if !defined(_LIBCPP_HAS_NO_VARIADICS)
// __lazy_and
template <bool _Last, class ..._Preds>
struct __lazy_and_impl;
template <class ..._Preds>
struct __lazy_and_impl<false, _Preds...> : false_type {};
template <>
struct __lazy_and_impl<true> : true_type {};
template <class _Pred>
struct __lazy_and_impl<true, _Pred> : integral_constant<bool, _Pred::type::value> {};
template <class _Hp, class ..._Tp>
struct __lazy_and_impl<true, _Hp, _Tp...> : __lazy_and_impl<_Hp::type::value, _Tp...> {};
template <class _P1, class ..._Pr>
struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {};
// __lazy_not
template <class _Pred>
struct __lazy_not : integral_constant<bool, !_Pred::type::value> {};
// __make_tuple_indices
template <size_t...> struct __tuple_indices {};

View File

@ -277,6 +277,53 @@ using bool_constant = integral_constant<bool, __b>;
typedef _LIBCPP_BOOL_CONSTANT(true) true_type;
typedef _LIBCPP_BOOL_CONSTANT(false) false_type;
#if !defined(_LIBCPP_HAS_NO_VARIADICS)
// __lazy_and
template <bool _Last, class ..._Preds>
struct __lazy_and_impl;
template <class ..._Preds>
struct __lazy_and_impl<false, _Preds...> : false_type {};
template <>
struct __lazy_and_impl<true> : true_type {};
template <class _Pred>
struct __lazy_and_impl<true, _Pred> : integral_constant<bool, _Pred::type::value> {};
template <class _Hp, class ..._Tp>
struct __lazy_and_impl<true, _Hp, _Tp...> : __lazy_and_impl<_Hp::type::value, _Tp...> {};
template <class _P1, class ..._Pr>
struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {};
// __lazy_or
template <bool _List, class ..._Preds>
struct __lazy_or_impl;
template <class ..._Preds>
struct __lazy_or_impl<true, _Preds...> : true_type {};
template <>
struct __lazy_or_impl<false> : false_type {};
template <class _Hp, class ..._Tp>
struct __lazy_or_impl<false, _Hp, _Tp...>
: __lazy_or_impl<_Hp::type::value, _Tp...> {};
template <class _P1, class ..._Pr>
struct __lazy_or : __lazy_or_impl<_P1::type::value, _Pr...> {};
// __lazy_not
template <class _Pred>
struct __lazy_not : integral_constant<bool, !_Pred::type::value> {};
#endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
// is_const
template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_const : public false_type {};

View File

@ -1,7 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// TODO: Make this test pass for all standards.
// XFAIL: c++98, c++03
// <type_traits>
// __convert_to_integral(Tp)
// Test that the __convert_to_integral functions properly converts Tp to the
// correct type and value for integral, enum and user defined types.
#include <limits>
#include <type_traits>
#include <cstdint>

View File

@ -0,0 +1,137 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++98, c++03
// <type_traits>
// __lazy_enable_if, __lazy_not, __lazy_and and __lazy_or
// Test the libc++ lazy meta-programming helpers in <type_traits>
#include <type_traits>
template <class Type>
struct Identity {
typedef Type type;
};
typedef std::true_type TrueT;
typedef std::false_type FalseT;
typedef Identity<TrueT> LazyTrueT;
typedef Identity<FalseT> LazyFalseT;
// A type that cannot be instantiated
template <class T>
struct CannotInst {
typedef T type;
static_assert(std::is_same<T, T>::value == false, "");
};
template <int Value>
struct NextInt {
typedef NextInt<Value + 1> type;
static const int value = Value;
};
template <int Value>
const int NextInt<Value>::value;
template <class Type>
struct HasTypeImp {
template <class Up, class = typename Up::type>
static TrueT test(int);
template <class>
static FalseT test(...);
typedef decltype(test<Type>(0)) type;
};
// A metafunction that returns True if Type has a nested 'type' typedef
// and false otherwise.
template <class Type>
struct HasType : HasTypeImp<Type>::type {};
void LazyEnableIfTest() {
{
typedef std::__lazy_enable_if<true, NextInt<0> > Result;
static_assert(HasType<Result>::value, "");
static_assert(Result::type::value == 1, "");
}
{
typedef std::__lazy_enable_if<false, CannotInst<int> > Result;
static_assert(!HasType<Result>::value, "");
}
}
void LazyNotTest() {
{
typedef std::__lazy_not<LazyTrueT> NotT;
static_assert(std::is_same<typename NotT::type, FalseT>::value, "");
static_assert(NotT::value == false, "");
}
{
typedef std::__lazy_not<LazyFalseT> NotT;
static_assert(std::is_same<typename NotT::type, TrueT>::value, "");
static_assert(NotT::value == true, "");
}
{
// Check that CannotInst<int> is not instantiated.
typedef std::__lazy_not<CannotInst<int> > NotT;
static_assert(std::is_same<NotT, NotT>::value, "");
}
}
void LazyAndTest() {
{ // Test that it acts as the identity function for a single value
static_assert(std::__lazy_and<LazyFalseT>::value == false, "");
static_assert(std::__lazy_and<LazyTrueT>::value == true, "");
}
{
static_assert(std::__lazy_and<LazyTrueT, LazyTrueT>::value == true, "");
static_assert(std::__lazy_and<LazyTrueT, LazyFalseT>::value == false, "");
static_assert(std::__lazy_and<LazyFalseT, LazyTrueT>::value == false, "");
static_assert(std::__lazy_and<LazyFalseT, LazyFalseT>::value == false, "");
}
{ // Test short circuiting - CannotInst<T> should never be instantiated.
static_assert(std::__lazy_and<LazyFalseT, CannotInst<int>>::value == false, "");
static_assert(std::__lazy_and<LazyTrueT, LazyFalseT, CannotInst<int>>::value == false, "");
}
}
void LazyOrTest() {
{ // Test that it acts as the identity function for a single value
static_assert(std::__lazy_or<LazyFalseT>::value == false, "");
static_assert(std::__lazy_or<LazyTrueT>::value == true, "");
}
{
static_assert(std::__lazy_or<LazyTrueT, LazyTrueT>::value == true, "");
static_assert(std::__lazy_or<LazyTrueT, LazyFalseT>::value == true, "");
static_assert(std::__lazy_or<LazyFalseT, LazyTrueT>::value == true, "");
static_assert(std::__lazy_or<LazyFalseT, LazyFalseT>::value == false, "");
}
{ // Test short circuiting - CannotInst<T> should never be instantiated.
static_assert(std::__lazy_or<LazyTrueT, CannotInst<int>>::value == true, "");
static_assert(std::__lazy_or<LazyFalseT, LazyTrueT, CannotInst<int>>::value == true, "");
}
}
int main() {
LazyEnableIfTest();
LazyNotTest();
LazyAndTest();
LazyOrTest();
}