From cd6615fd7186e397b4318d8b77c82b9de52ed45f Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 18 May 2015 23:21:06 +0000 Subject: [PATCH] Add support for N4389 - std::bool_constant llvm-svn: 237636 --- libcxx/include/ratio | 12 +++---- libcxx/include/type_traits | 25 +++++++++++---- .../meta/meta.hel/bool_constant.pass.cpp | 32 +++++++++++++++++++ libcxx/www/cxx1z_status.html | 2 +- 4 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 libcxx/test/std/utilities/meta/meta.hel/bool_constant.pass.cpp diff --git a/libcxx/include/ratio b/libcxx/include/ratio index 25ab503abfb9..6ddc19bf3e35 100644 --- a/libcxx/include/ratio +++ b/libcxx/include/ratio @@ -398,11 +398,11 @@ struct _LIBCPP_TYPE_VIS_ONLY ratio_subtract template struct _LIBCPP_TYPE_VIS_ONLY ratio_equal - : public integral_constant {}; + : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {}; template struct _LIBCPP_TYPE_VIS_ONLY ratio_not_equal - : public integral_constant::value> {}; + : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {}; // ratio_less @@ -461,19 +461,19 @@ struct __ratio_less<_R1, _R2, -1LL, -1LL> template struct _LIBCPP_TYPE_VIS_ONLY ratio_less - : public integral_constant::value> {}; + : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {}; template struct _LIBCPP_TYPE_VIS_ONLY ratio_less_equal - : public integral_constant::value> {}; + : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {}; template struct _LIBCPP_TYPE_VIS_ONLY ratio_greater - : public integral_constant::value> {}; + : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {}; template struct _LIBCPP_TYPE_VIS_ONLY ratio_greater_equal - : public integral_constant::value> {}; + : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {}; template struct __ratio_gcd diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index 49eecacca017..4753a61a60f2 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -19,8 +19,13 @@ namespace std // helper class: template struct integral_constant; - typedef integral_constant true_type; - typedef integral_constant false_type; + typedef integral_constant true_type; // C++11 + typedef integral_constant false_type; // C++11 + + template // C++14 + using bool_constant = integral_constant; // C++14 + typedef bool_constant true_type; // C++14 + typedef bool_constant false_type; // C++14 // helper traits template struct enable_if; @@ -260,8 +265,16 @@ struct _LIBCPP_TYPE_VIS_ONLY integral_constant template _LIBCPP_CONSTEXPR const _Tp integral_constant<_Tp, __v>::value; -typedef integral_constant true_type; -typedef integral_constant false_type; +#if _LIBCPP_STD_VER > 14 +template +using bool_constant = integral_constant; +#define _LIBCPP_BOOL_CONSTANT(__b) bool_constant<(__b)> +#else +#define _LIBCPP_BOOL_CONSTANT(__b) integral_constant +#endif + +typedef _LIBCPP_BOOL_CONSTANT(true) true_type; +typedef _LIBCPP_BOOL_CONSTANT(false) false_type; // is_const @@ -679,7 +692,7 @@ template using add_pointer_t = typename add_pointer<_Tp>::type; // is_signed template ::value> -struct __libcpp_is_signed_impl : public integral_constant {}; +struct __libcpp_is_signed_impl : public _LIBCPP_BOOL_CONSTANT(_Tp(-1) < _Tp(0)) {}; template struct __libcpp_is_signed_impl<_Tp, false> : public true_type {}; // floating point @@ -694,7 +707,7 @@ template struct _LIBCPP_TYPE_VIS_ONLY is_signed : public __libcpp_is // is_unsigned template ::value> -struct __libcpp_is_unsigned_impl : public integral_constant {}; +struct __libcpp_is_unsigned_impl : public _LIBCPP_BOOL_CONSTANT(_Tp(0) < _Tp(-1)) {}; template struct __libcpp_is_unsigned_impl<_Tp, false> : public false_type {}; // floating point diff --git a/libcxx/test/std/utilities/meta/meta.hel/bool_constant.pass.cpp b/libcxx/test/std/utilities/meta/meta.hel/bool_constant.pass.cpp new file mode 100644 index 000000000000..71110ea3bfb0 --- /dev/null +++ b/libcxx/test/std/utilities/meta/meta.hel/bool_constant.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// type_traits + +// bool_constant + +#include +#include + +int main() +{ +#if __cplusplus > 201402L + typedef std::bool_constant _t; + static_assert(_t::value, ""); + static_assert((std::is_same<_t::value_type, bool>::value), ""); + static_assert((std::is_same<_t::type, _t>::value), ""); + static_assert((_t() == true), ""); + + typedef std::bool_constant _f; + static_assert(!_f::value, ""); + static_assert((std::is_same<_f::value_type, bool>::value), ""); + static_assert((std::is_same<_f::type, _f>::value), ""); + static_assert((_f() == false), ""); +#endif +} diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 818e5749730b..e39584bdbeb6 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -64,7 +64,7 @@ N4285CWGCleanup for exception-specification and throw-expression.Urbana N4387LWGimproving pair and tupleLenexa - N4389LWGbool_constantLenexa + N4389LWGbool_constantLenexaComplete N4508LWGshared_mutex for C++17Lenexa N4366LWGLWG 2228 missing SFINAE ruleLenexa N4510LWGMinimal incomplete type support for standard containers, revision 4Lenexa