From cf6e0db0b9caaf49dee2cdee1bdceeb60429935a Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 21 Mar 2017 18:38:57 +0000 Subject: [PATCH] Implement P0548: 'common_type and duration' This involves a subtle change in the return type of the unary +/- operators for std::chrono::duration, though I expect that no one will notice. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@298416 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/chrono | 12 +++++------ include/type_traits | 4 ++-- .../meta.trans.other/common_type.pass.cpp | 21 +++++++++++++++++++ .../time.duration.arithmetic/op_+.pass.cpp | 6 ++++-- .../time.duration.arithmetic/op_-.pass.cpp | 18 ++++++++++++++-- www/cxx1z_status.html | 2 +- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/include/chrono b/include/chrono index 4e82ef2b5..657808736 100644 --- a/include/chrono +++ b/include/chrono @@ -48,7 +48,7 @@ class duration static_assert(Period::num > 0, "duration period must be positive"); public: typedef Rep rep; - typedef Period period; + typedef typename _Period::type period; constexpr duration() = default; template @@ -75,8 +75,8 @@ public: // arithmetic - constexpr duration operator+() const; - constexpr duration operator-() const; + constexpr common_type::type operator+() const; + constexpr common_type::type operator-() const; constexpr duration& operator++(); constexpr duration operator++(int); constexpr duration& operator--(); @@ -523,7 +523,7 @@ class _LIBCPP_TEMPLATE_VIS duration public: typedef _Rep rep; - typedef _Period period; + typedef typename _Period::type period; private: rep __rep_; public: @@ -565,8 +565,8 @@ public: // arithmetic - _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;} - _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);} + _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type::type operator+() const {return typename common_type::type(*this);} + _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type::type operator-() const {return typename common_type::type(-__rep_);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);} _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;} diff --git a/include/type_traits b/include/type_traits index 3aa84605a..8facb3bb9 100644 --- a/include/type_traits +++ b/include/type_traits @@ -1958,7 +1958,7 @@ template struct _LIBCPP_TEMPLATE_VIS common_type<_Tp, void, void> { public: - typedef typename decay<_Tp>::type type; + typedef typename common_type<_Tp, _Tp>::type type; }; template @@ -1981,7 +1981,7 @@ struct _LIBCPP_TEMPLATE_VIS common_type {}; template struct _LIBCPP_TEMPLATE_VIS common_type<_Tp> { - typedef typename decay<_Tp>::type type; + typedef typename common_type<_Tp, _Tp>::type type; }; // bullet 3 - sizeof...(Tp) == 2 diff --git a/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp b/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp index 61523e487..736f74b4c 100644 --- a/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp +++ b/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp @@ -37,6 +37,12 @@ namespace std typedef S type; }; +// P0548 + template + struct common_type< ::S, ::S > { + typedef S type; + }; + template <> struct common_type< ::S, long> {}; template <> struct common_type > {}; } @@ -284,4 +290,19 @@ int main() test_bullet_three_two(); test_bullet_four(); #endif + +// P0548 + static_assert((std::is_same >::type, S >::value), ""); + static_assert((std::is_same, S >::type, S >::value), ""); + + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + static_assert((std::is_same::type, int>::value), ""); + } diff --git a/test/std/utilities/time/time.duration/time.duration.arithmetic/op_+.pass.cpp b/test/std/utilities/time/time.duration/time.duration.arithmetic/op_+.pass.cpp index c0f10147e..e4a22cfad 100644 --- a/test/std/utilities/time/time.duration/time.duration.arithmetic/op_+.pass.cpp +++ b/test/std/utilities/time/time.duration/time.duration.arithmetic/op_+.pass.cpp @@ -11,11 +11,13 @@ // duration -// duration operator+() const; +// constexpr common_type_t operator+() const; #include #include +#include + int main() { { @@ -23,7 +25,7 @@ int main() std::chrono::minutes m2 = +m; assert(m.count() == m2.count()); } -#ifndef _LIBCPP_HAS_NO_CONSTEXPR +#if TEST_STD_VER >= 11 { constexpr std::chrono::minutes m(3); constexpr std::chrono::minutes m2 = +m; diff --git a/test/std/utilities/time/time.duration/time.duration.arithmetic/op_-.pass.cpp b/test/std/utilities/time/time.duration/time.duration.arithmetic/op_-.pass.cpp index 00da6f69c..bc1328c9d 100644 --- a/test/std/utilities/time/time.duration/time.duration.arithmetic/op_-.pass.cpp +++ b/test/std/utilities/time/time.duration/time.duration.arithmetic/op_-.pass.cpp @@ -11,11 +11,13 @@ // duration -// duration operator-() const; +// constexpr common_type_t operator-() const; #include #include +#include + int main() { { @@ -23,11 +25,23 @@ int main() std::chrono::minutes m2 = -m; assert(m2.count() == -m.count()); } -#ifndef _LIBCPP_HAS_NO_CONSTEXPR +#if TEST_STD_VER >= 11 { constexpr std::chrono::minutes m(3); constexpr std::chrono::minutes m2 = -m; static_assert(m2.count() == -m.count(), ""); } #endif + +// P0548 + { + typedef std::chrono::duration > D10; + typedef std::chrono::duration > D1; + D10 zero{0}; + D10 one{1}; + static_assert( (std::is_same< decltype(-one), decltype(zero-one) >::value), ""); + static_assert( (std::is_same< decltype(zero-one), D1>::value), ""); + static_assert( (std::is_same< decltype(-one), D1>::value), ""); + static_assert( (std::is_same< decltype(+one), D1>::value), ""); + } } diff --git a/www/cxx1z_status.html b/www/cxx1z_status.html index f3cffb79b..906585f95 100644 --- a/www/cxx1z_status.html +++ b/www/cxx1z_status.html @@ -153,7 +153,7 @@ P0492R2LWGProposed Resolution of C++17 National Body Comments for FilesystemsKona P0518R1LWGAllowing copies as arguments to function objects given to parallel algorithms in response to CH11Kona P0523R1LWGWording for CH 10: Complexity of parallel algorithmsKona - P0548R1LWGcommon_type and durationKona + P0548R1LWGcommon_type and durationKonaComplete5.0 P0558R1LWGResolving atomic<T> named base class inconsistenciesKona P0574R1LWGAlgorithm Complexity Constraints and Parallel OverloadsKona P0599R1LWGnoexcept for hash functionsKona