[libcxx] r246408 - Move __lazy_* metafunctions to type traits and add tests

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 30 20:50:31 PDT 2015


Author: ericwf
Date: Sun Aug 30 22:50:31 2015
New Revision: 246408

URL: http://llvm.org/viewvc/llvm-project?rev=246408&view=rev
Log:
Move __lazy_* metafunctions to type traits and add tests

Added:
    libcxx/trunk/test/libcxx/type_traits/lazy_metafunctions.pass.cpp
Modified:
    libcxx/trunk/include/__tuple
    libcxx/trunk/include/type_traits
    libcxx/trunk/test/libcxx/type_traits/convert_to_integral.pass.cpp

Modified: libcxx/trunk/include/__tuple
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__tuple?rev=246408&r1=246407&r2=246408&view=diff
==============================================================================
--- libcxx/trunk/include/__tuple (original)
+++ libcxx/trunk/include/__tuple Sun Aug 30 22:50:31 2015
@@ -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 {};

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=246408&r1=246407&r2=246408&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Sun Aug 30 22:50:31 2015
@@ -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 <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 {};

Modified: libcxx/trunk/test/libcxx/type_traits/convert_to_integral.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/type_traits/convert_to_integral.pass.cpp?rev=246408&r1=246407&r2=246408&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/type_traits/convert_to_integral.pass.cpp (original)
+++ libcxx/trunk/test/libcxx/type_traits/convert_to_integral.pass.cpp Sun Aug 30 22:50:31 2015
@@ -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>

Added: libcxx/trunk/test/libcxx/type_traits/lazy_metafunctions.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/type_traits/lazy_metafunctions.pass.cpp?rev=246408&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/type_traits/lazy_metafunctions.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/type_traits/lazy_metafunctions.pass.cpp Sun Aug 30 22:50:31 2015
@@ -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();
+}
\ No newline at end of file




More information about the cfe-commits mailing list