From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1698) id 9E059385783D; Mon, 31 Aug 2020 19:49:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E059385783D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598903343; bh=kM+6riVZn8I6LN7AhCRqpK9ZVxP2OB8zcp9coQeTToc=; h=From:To:Subject:Date:From; b=kzH9PKbuKFQ1EJvnF8gD6NaMpCP2f0jTlVK082Afcfzpm5DhclzaQPLaCp1yjJk/8 7zosCUpmao5apEsvTCw1FYtlctvJ17YGceiOrjGYslh1GO8GvfiDSydPqRURbOvKb3 JcOsEqHnAVjdU/pPgjm00tP8KC7wsHS4rV7dMRLg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Iain D Sandoe To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/c++-coroutines] libstdc++: Fix common_type specializations for duration X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/devel/c++-coroutines X-Git-Oldrev: 82db1a42e9254c9009bbf8ac01366da4d1ab6df5 X-Git-Newrev: f2f48b68a6a586f40dd8ae0e6d391b7f88756eec Message-Id: <20200831194903.9E059385783D@sourceware.org> Date: Mon, 31 Aug 2020 19:49:03 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Aug 2020 19:49:03 -0000 https://gcc.gnu.org/g:f2f48b68a6a586f40dd8ae0e6d391b7f88756eec commit f2f48b68a6a586f40dd8ae0e6d391b7f88756eec Author: Jonathan Wakely Date: Fri Aug 28 23:41:13 2020 +0100 libstdc++: Fix common_type specializations for duration My recent change to implement P0548 ("common_type and duration") was not correct. The result of common_type_t, duration> should be duration, P::type>, not duration. The common_type specialization for two different duration types was correct, but the specializations for a single duration type (which only exist to optimize compilation time) were wrong. This fixes the partial specializations of common_type for a single duration type, and also the return types of duration::operator+ and duration::operator- which are supposed to use common_type_t. libstdc++-v3/ChangeLog: * include/std/chrono (common_type): Fix partial specializations for a single duration type to use the common_type of the rep. (duration::operator+, duration::operator-): Fix return types to also use the common_type of the rep. * testsuite/20_util/duration/requirements/reduced_period.cc: Check duration using a rep that has common_type specialized. Diff: --- libstdc++-v3/include/std/chrono | 18 +++++--- .../duration/requirements/reduced_period.cc | 52 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index fb251848da8..524d23ea6a7 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -114,13 +114,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct common_type, chrono::duration<_Rep, _Period>> - { using type = chrono::duration<_Rep, typename _Period::type>; }; + { + using type = chrono::duration::type, + typename _Period::type>; + }; /// Specialization of common_type for one chrono::duration type. /// @relates duration template struct common_type> - { using type = chrono::duration<_Rep, typename _Period::type>; }; + { + using type = chrono::duration::type, + typename _Period::type>; + }; // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) @@ -463,13 +469,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // 20.11.5.3 arithmetic - constexpr duration + constexpr duration::type, period> operator+() const - { return *this; } + { return duration::type, period>(__r); } - constexpr duration + constexpr duration::type, period> operator-() const - { return duration(-__r); } + { return duration::type, period>(-__r); } _GLIBCXX17_CONSTEXPR duration& operator++() diff --git a/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc b/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc index 9eb38a0e56f..4c7472699d2 100644 --- a/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc +++ b/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc @@ -129,3 +129,55 @@ test02() static_assert( is_same::type>::value, "unary - returns the reduced duration" ); } + +template +struct Number +{ + explicit + Number(T t = 0) : i(t) + { } + + template::value, + typename = typename std::enable_if::type> + explicit + Number(Number n) : i(n.i) + { } + + T i = 0; + + Number& operator+=(Number n) { i += n.i; return *this; } + Number& operator-=(Number n) { i -= n.i; return *this; } + Number& operator*=(Number n) { i *= n.i; return *this; } + Number& operator/=(Number n) { i /= n.i; return *this; } + Number& operator%=(Number n) { i %= n.i; return *this; } + + Number operator+(Number n) { return { i + n.i }; } + Number operator-(Number n) { return { i - n.i }; } + Number operator*(Number n) { return { i * n.i }; } + Number operator/(Number n) { return { i / n.i }; } + Number operator%(Number n) { return { i % n.i }; } +}; + +namespace std +{ + // Specialise common_type to give a different type + template<> + struct common_type, Number> + { using type = Number; }; +} + +void +test03() +{ + + using D4 = duration, ratio<49, 21>>; + static_assert( is_same::type, + duration, ratio<7, 3>>>::value, + "common_type_t> uses common_type_t" ); + + D4 d4; + static_assert( is_same::type>::value, + "unary + returns type with common_type_t as rep" ); + static_assert( is_same::type>::value, + "unary - returns type with common_type_t as rep" ); +}