From 2baafd4a3ab2691c03f540353b4eb5e3fa50cb53 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sat, 25 Aug 2018 11:34:17 -0400 Subject: [PATCH] Add enable_if_ and enable_if_t. Sever dependency on Core. --- doc/enable_if.qbk | 44 +++++++++ doc/type_traits.qbk | 1 + include/boost/type_traits.hpp | 1 + .../type_traits/detail/is_likely_lambda.hpp | 4 +- include/boost/type_traits/enable_if.hpp | 37 ++++++++ .../is_nothrow_move_assignable.hpp | 4 +- .../is_nothrow_move_constructible.hpp | 4 +- test/enable_if_test.cpp | 92 +++++++++++++++++++ test/is_convertible_test.cpp | 4 +- 9 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 doc/enable_if.qbk create mode 100644 include/boost/type_traits/enable_if.hpp create mode 100644 test/enable_if_test.cpp diff --git a/doc/enable_if.qbk b/doc/enable_if.qbk new file mode 100644 index 0000000..227bdb0 --- /dev/null +++ b/doc/enable_if.qbk @@ -0,0 +1,44 @@ +[/ +Copyright 2018 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, +Version 1.0. (See accompanying file LICENSE_1_0.txt +or copy at http://www.boost.org/LICENSE_1_0.txt). +] + +[section:enable_if enable_if_] + + template + struct enable_if_; + + template + using enable_if_t = typename enable_if_::type; + +__type If `B` is true, then the member `type` is defined to be `T`. Otherwise +there is no member `type`. + +__header `#include ` + +__examples + +The following function can be used to destroy each element of an array and +specially handle arrays of trivially destrucibtle types. + + template + typename boost::enable_if_::value>::type + destroy(T* ptr, std::size_t size) + { + while (size > 0) { + ptr[--size].~T(); + } + } + + template + typename boost::enable_if_::value>::type + destroy(T*, std::size_t) { } + +[all_compilers] The type alias `enable_if_t` is only available if the compiler +supports template aliases. + +[endsect] diff --git a/doc/type_traits.qbk b/doc/type_traits.qbk index ebc545b..abc2da2 100644 --- a/doc/type_traits.qbk +++ b/doc/type_traits.qbk @@ -309,6 +309,7 @@ that is the result of the transformation. [include declval.qbk] [include detected.qbk] [include detected_or.qbk] +[include enable_if.qbk] [include extent.qbk] [include floating_point_promotion.qbk] [include function_traits.qbk] diff --git a/include/boost/type_traits.hpp b/include/boost/type_traits.hpp index 9f024b1..a767e41 100644 --- a/include/boost/type_traits.hpp +++ b/include/boost/type_traits.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/include/boost/type_traits/detail/is_likely_lambda.hpp b/include/boost/type_traits/detail/is_likely_lambda.hpp index 6da8aca..39aa528 100644 --- a/include/boost/type_traits/detail/is_likely_lambda.hpp +++ b/include/boost/type_traits/detail/is_likely_lambda.hpp @@ -30,7 +30,7 @@ struct is_likely_stateless_lambda : public false_type {}; #elif !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !BOOST_WORKAROUND(BOOST_MSVC, < 1900) #include -#include +#include namespace boost{ @@ -70,7 +70,7 @@ struct is_likely_stateless_lambda : false_type{}; template struct is_likely_stateless_lambda< T, - typename boost::enable_if_c::value>::type> : + typename boost::enable_if_::value>::type> : boost::is_convertible::type >{}; diff --git a/include/boost/type_traits/enable_if.hpp b/include/boost/type_traits/enable_if.hpp new file mode 100644 index 0000000..3cdc281 --- /dev/null +++ b/include/boost/type_traits/enable_if.hpp @@ -0,0 +1,37 @@ +/* +Copyright 2003 The Trustees of Indiana University + +Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) + Jeremiah Willcock (jewillco at osl.iu.edu) + Andrew Lumsdaine (lums at osl.iu.edu) + +Copyright 2018 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, +Version 1.0. (See accompanying file LICENSE_1_0.txt +or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ +#ifndef BOOST_TT_ENABLE_IF_HPP_INCLUDED +#define BOOST_TT_ENABLE_IF_HPP_INCLUDED + +#include + +namespace boost { + +template +struct enable_if_ { + typedef T type; +}; + +template +struct enable_if_ { }; + +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) +template +using enable_if_t = typename enable_if_::type; +#endif + +} /* boost */ + +#endif diff --git a/include/boost/type_traits/is_nothrow_move_assignable.hpp b/include/boost/type_traits/is_nothrow_move_assignable.hpp index 5f63cb7..c6194de 100644 --- a/include/boost/type_traits/is_nothrow_move_assignable.hpp +++ b/include/boost/type_traits/is_nothrow_move_assignable.hpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -48,7 +48,7 @@ struct false_or_cpp11_noexcept_move_assignable: public ::boost::false_type {}; template struct false_or_cpp11_noexcept_move_assignable < T, - typename ::boost::enable_if_c() = ::boost::declval())>::type + typename ::boost::enable_if_() = ::boost::declval())>::type > : public ::boost::integral_constant() = ::boost::declval())> {}; diff --git a/include/boost/type_traits/is_nothrow_move_constructible.hpp b/include/boost/type_traits/is_nothrow_move_constructible.hpp index 30b6a16..60c2994 100644 --- a/include/boost/type_traits/is_nothrow_move_constructible.hpp +++ b/include/boost/type_traits/is_nothrow_move_constructible.hpp @@ -35,7 +35,7 @@ template struct is_nothrow_move_constructible : publ #elif !defined(BOOST_NO_CXX11_NOEXCEPT) && !defined(BOOST_NO_SFINAE_EXPR) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40700) #include -#include +#include namespace boost{ namespace detail{ @@ -45,7 +45,7 @@ struct false_or_cpp11_noexcept_move_constructible: public ::boost::false_type {} template struct false_or_cpp11_noexcept_move_constructible < T, - typename ::boost::enable_if_c()))>::type + typename ::boost::enable_if_()))>::type > : public ::boost::integral_constant()))> {}; diff --git a/test/enable_if_test.cpp b/test/enable_if_test.cpp new file mode 100644 index 0000000..f3a30a0 --- /dev/null +++ b/test/enable_if_test.cpp @@ -0,0 +1,92 @@ +/* +Copyright 2018 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, +Version 1.0. (See accompanying file LICENSE_1_0.txt +or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ +#include +#include "test.hpp" + +template +struct Constant { + enum { + value = B + }; +}; + +template +struct Check + : Constant { }; + +template<> +struct Check + : Constant { }; + +class Construct { +public: + template + Construct(T, typename boost::enable_if_::value>::type* = 0) + : value_(true) { } + template + Construct(T, typename boost::enable_if_::value>::type* = 0) + : value_(false) { } + bool value() const { + return value_; + } +private: + bool value_; +}; + +template +struct Specialize; + +template +struct Specialize::value>::type> + : Constant { }; + +template +struct Specialize::value>::type> + : Constant { }; + +template +typename boost::enable_if_::value, bool>::type Returns(T) +{ + return true; +} + +template +typename boost::enable_if_::value, bool>::type Returns(T) +{ + return false; +} + +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) +template +boost::enable_if_t::value, bool> Alias(T) +{ + return true; +} + +template +boost::enable_if_t::value, bool> Alias(T) +{ + return false; +} +#endif + +TT_TEST_BEGIN(enable_if) + +BOOST_CHECK(!Construct(1).value()); +BOOST_CHECK(Construct(1L).value()); +BOOST_CHECK(!Specialize::value); +BOOST_CHECK(Specialize::value); +BOOST_CHECK(!Returns(1)); +BOOST_CHECK(Returns(1L)); +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) +BOOST_CHECK(!Alias(1)); +BOOST_CHECK(Alias(1L)); +#endif + +TT_TEST_END diff --git a/test/is_convertible_test.cpp b/test/is_convertible_test.cpp index 8b33c03..92321db 100644 --- a/test/is_convertible_test.cpp +++ b/test/is_convertible_test.cpp @@ -11,7 +11,7 @@ #endif #include "test.hpp" #include "check_integral_constant.hpp" -#include +#include template @@ -30,7 +30,7 @@ template struct test_bug_4530 { template - test_bug_4530(A&&, typename boost::enable_if< ::tt::is_convertible >::type* =0); + test_bug_4530(A&&, typename boost::enable_if_< ::tt::is_convertible::value >::type* =0); }; struct A4530