From 888d91a9e4702234c8e38fa1a6cabf315fe81b96 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Mon, 31 Aug 2015 03:50:31 +0000 Subject: [PATCH] Move __lazy_* metafunctions to type traits and add tests llvm-svn: 246408 --- libcxx/include/__tuple | 25 ---- libcxx/include/type_traits | 47 ++++++ .../type_traits/convert_to_integral.pass.cpp | 17 ++- .../type_traits/lazy_metafunctions.pass.cpp | 137 ++++++++++++++++++ 4 files changed, 200 insertions(+), 26 deletions(-) create mode 100644 libcxx/test/libcxx/type_traits/lazy_metafunctions.pass.cpp diff --git a/libcxx/include/__tuple b/libcxx/include/__tuple index 2837ce708f09..57581e842862 100644 --- a/libcxx/include/__tuple +++ b/libcxx/include/__tuple @@ -136,31 +136,6 @@ get(array<_Tp, _Size>&&) _NOEXCEPT; #if !defined(_LIBCPP_HAS_NO_VARIADICS) -// __lazy_and - -template -struct __lazy_and_impl; - -template -struct __lazy_and_impl : false_type {}; - -template <> -struct __lazy_and_impl : true_type {}; - -template -struct __lazy_and_impl : integral_constant {}; - -template -struct __lazy_and_impl : __lazy_and_impl<_Hp::type::value, _Tp...> {}; - -template -struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {}; - -// __lazy_not - -template -struct __lazy_not : integral_constant {}; - // __make_tuple_indices template struct __tuple_indices {}; diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index f0defafec6d3..26ed76e6e62e 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -277,6 +277,53 @@ using bool_constant = integral_constant; typedef _LIBCPP_BOOL_CONSTANT(true) true_type; typedef _LIBCPP_BOOL_CONSTANT(false) false_type; +#if !defined(_LIBCPP_HAS_NO_VARIADICS) + +// __lazy_and + +template +struct __lazy_and_impl; + +template +struct __lazy_and_impl : false_type {}; + +template <> +struct __lazy_and_impl : true_type {}; + +template +struct __lazy_and_impl : integral_constant {}; + +template +struct __lazy_and_impl : __lazy_and_impl<_Hp::type::value, _Tp...> {}; + +template +struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {}; + +// __lazy_or + +template +struct __lazy_or_impl; + +template +struct __lazy_or_impl : true_type {}; + +template <> +struct __lazy_or_impl : false_type {}; + +template +struct __lazy_or_impl + : __lazy_or_impl<_Hp::type::value, _Tp...> {}; + +template +struct __lazy_or : __lazy_or_impl<_P1::type::value, _Pr...> {}; + +// __lazy_not + +template +struct __lazy_not : integral_constant {}; + +#endif // !defined(_LIBCPP_HAS_NO_VARIADICS) + // is_const template struct _LIBCPP_TYPE_VIS_ONLY is_const : public false_type {}; diff --git a/libcxx/test/libcxx/type_traits/convert_to_integral.pass.cpp b/libcxx/test/libcxx/type_traits/convert_to_integral.pass.cpp index b97832b5e6d7..3fdc98f5468f 100644 --- a/libcxx/test/libcxx/type_traits/convert_to_integral.pass.cpp +++ b/libcxx/test/libcxx/type_traits/convert_to_integral.pass.cpp @@ -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 +// + +// __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 #include #include diff --git a/libcxx/test/libcxx/type_traits/lazy_metafunctions.pass.cpp b/libcxx/test/libcxx/type_traits/lazy_metafunctions.pass.cpp new file mode 100644 index 000000000000..8f75080ab956 --- /dev/null +++ b/libcxx/test/libcxx/type_traits/lazy_metafunctions.pass.cpp @@ -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 + +// + +// __lazy_enable_if, __lazy_not, __lazy_and and __lazy_or + +// Test the libc++ lazy meta-programming helpers in + +#include + +template +struct Identity { + typedef Type type; +}; + +typedef std::true_type TrueT; +typedef std::false_type FalseT; + +typedef Identity LazyTrueT; +typedef Identity LazyFalseT; + +// A type that cannot be instantiated +template +struct CannotInst { + typedef T type; + static_assert(std::is_same::value == false, ""); +}; + + +template +struct NextInt { + typedef NextInt type; + static const int value = Value; +}; + +template +const int NextInt::value; + + +template +struct HasTypeImp { + template + static TrueT test(int); + template + static FalseT test(...); + + typedef decltype(test(0)) type; +}; + +// A metafunction that returns True if Type has a nested 'type' typedef +// and false otherwise. +template +struct HasType : HasTypeImp::type {}; + +void LazyEnableIfTest() { + { + typedef std::__lazy_enable_if > Result; + static_assert(HasType::value, ""); + static_assert(Result::type::value == 1, ""); + } + { + typedef std::__lazy_enable_if > Result; + static_assert(!HasType::value, ""); + } +} + +void LazyNotTest() { + { + typedef std::__lazy_not NotT; + static_assert(std::is_same::value, ""); + static_assert(NotT::value == false, ""); + } + { + typedef std::__lazy_not NotT; + static_assert(std::is_same::value, ""); + static_assert(NotT::value == true, ""); + } + { + // Check that CannotInst is not instantiated. + typedef std::__lazy_not > NotT; + + static_assert(std::is_same::value, ""); + + } +} + +void LazyAndTest() { + { // Test that it acts as the identity function for a single value + static_assert(std::__lazy_and::value == false, ""); + static_assert(std::__lazy_and::value == true, ""); + } + { + static_assert(std::__lazy_and::value == true, ""); + static_assert(std::__lazy_and::value == false, ""); + static_assert(std::__lazy_and::value == false, ""); + static_assert(std::__lazy_and::value == false, ""); + } + { // Test short circuiting - CannotInst should never be instantiated. + static_assert(std::__lazy_and>::value == false, ""); + static_assert(std::__lazy_and>::value == false, ""); + } +} + + +void LazyOrTest() { + { // Test that it acts as the identity function for a single value + static_assert(std::__lazy_or::value == false, ""); + static_assert(std::__lazy_or::value == true, ""); + } + { + static_assert(std::__lazy_or::value == true, ""); + static_assert(std::__lazy_or::value == true, ""); + static_assert(std::__lazy_or::value == true, ""); + static_assert(std::__lazy_or::value == false, ""); + } + { // Test short circuiting - CannotInst should never be instantiated. + static_assert(std::__lazy_or>::value == true, ""); + static_assert(std::__lazy_or>::value == true, ""); + } +} + + +int main() { + LazyEnableIfTest(); + LazyNotTest(); + LazyAndTest(); + LazyOrTest(); +} \ No newline at end of file