public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9347] libstdc++: Add [[nodiscard]] to chrono conversion functions Date: Tue, 28 Mar 2023 23:35:21 +0000 (GMT) [thread overview] Message-ID: <20230328233521.1506A3889E25@sourceware.org> (raw) https://gcc.gnu.org/g:a0955eb049901481f1fa7ebabb2ba13d58a0eac3 commit r12-9347-ga0955eb049901481f1fa7ebabb2ba13d58a0eac3 Author: Jonathan Wakely <jwakely@redhat.com> Date: Mon Nov 28 11:22:24 2022 +0000 libstdc++: Add [[nodiscard]] to chrono conversion functions Also add doxygen comments. libstdc++-v3/ChangeLog: * include/bits/chrono.h (duration_cast, floor, round, abs, ceil) (time_point_cast): Add [[nodiscard]] attribute and doxygen comments. (treat_as_floating_point): Add doxygen commen. (cherry picked from commit 646e979c43b8c84f0f70ea8f1709dfa2909726cd) Diff: --- libstdc++-v3/include/bits/chrono.h | 139 ++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index 36561d94bef..c585cac3d8a 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -246,8 +246,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @endcond - /// duration_cast + /** Convert a `duration` to type `ToDur`. + * + * If the duration cannot be represented accurately in the result type, + * returns the result of integer truncation (i.e., rounded towards zero). + * + * @tparam _ToDur The result type must be a `duration`. + * @param __d A duration. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++11 + */ template<typename _ToDur, typename _Rep, typename _Period> + _GLIBCXX_NODISCARD constexpr __enable_if_is_duration<_ToDur> duration_cast(const duration<_Rep, _Period>& __d) { @@ -260,7 +270,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __dc::__cast(__d); } - /// treat_as_floating_point + /** Trait indicating whether to treat a type as a floating-point type. + * + * The chrono library uses this trait to tell whether a `duration` can + * represent fractional values of the given precision, or only integral + * values. + * + * You should specialize this trait for your own numeric types that are + * used with `duration` and can represent non-integral values. + * + * @since C++11 + */ template<typename _Rep> struct treat_as_floating_point : is_floating_point<_Rep> @@ -329,8 +349,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cplusplus >= 201703L # define __cpp_lib_chrono 201611L + /** Convert a `duration` to type `ToDur` and round down. + * + * If the duration cannot be represented exactly in the result type, + * returns the closest value that is less than the argument. + * + * @tparam _ToDur The result type must be a `duration`. + * @param __d A duration. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template<typename _ToDur, typename _Rep, typename _Period> - constexpr __enable_if_is_duration<_ToDur> + [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> floor(const duration<_Rep, _Period>& __d) { auto __to = chrono::duration_cast<_ToDur>(__d); @@ -339,8 +369,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __to; } + /** Convert a `duration` to type `ToDur` and round up. + * + * If the duration cannot be represented exactly in the result type, + * returns the closest value that is greater than the argument. + * + * @tparam _ToDur The result type must be a `duration`. + * @param __d A duration. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template<typename _ToDur, typename _Rep, typename _Period> - constexpr __enable_if_is_duration<_ToDur> + [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> ceil(const duration<_Rep, _Period>& __d) { auto __to = chrono::duration_cast<_ToDur>(__d); @@ -349,8 +389,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __to; } + /** Convert a `duration` to type `ToDur` and round to the closest value. + * + * If the duration cannot be represented exactly in the result type, + * returns the closest value, rounding ties to even. + * + * @tparam _ToDur The result type must be a `duration` with a + * non-floating-point `rep` type. + * @param __d A duration. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template <typename _ToDur, typename _Rep, typename _Period> - constexpr enable_if_t< + [[nodiscard]] constexpr + enable_if_t< __and_<__is_duration<_ToDur>, __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, _ToDur> @@ -361,18 +413,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION auto __diff0 = __d - __t0; auto __diff1 = __t1 - __d; if (__diff0 == __diff1) - { + { if (__t0.count() & 1) - return __t1; + return __t1; return __t0; - } + } else if (__diff0 < __diff1) - return __t0; + return __t0; return __t1; } + /** The absolute (non-negative) value of a duration. + * + * @param __d A duration with a signed `rep` type. + * @return A duration of the same type as the argument, with value |d|. + * @since C++17 + */ template<typename _Rep, typename _Period> - constexpr + [[nodiscard]] constexpr enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>> abs(duration<_Rep, _Period> __d) { @@ -934,10 +992,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION duration __d; }; - /// time_point_cast + /** Convert a `time_point` to use `duration` type `ToDur`. + * + * The result is the same time point as measured by the same clock, but + * using the specified `duration` to represent the time. + * If the time point cannot be represented accurately in the result type, + * returns the result of integer truncation (i.e., rounded towards zero). + * + * @tparam _ToDur The `duration` type to use for the result. + * @param __t A time point. + * @return The value of `__t` converted to use type `_ToDur`. + * @since C++11 + */ template<typename _ToDur, typename _Clock, typename _Dur> - constexpr typename enable_if<__is_duration<_ToDur>::value, - time_point<_Clock, _ToDur>>::type + _GLIBCXX_NODISCARD constexpr + __enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> time_point_cast(const time_point<_Clock, _Dur>& __t) { typedef time_point<_Clock, _ToDur> __time_point; @@ -945,8 +1014,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #if __cplusplus > 201402L + /** Convert a `time_point` to type `ToDur` and round down. + * + * The result is the same time point as measured by the same clock, but + * using the specified `duration` to represent the time. + * If the time point cannot be represented exactly in the result type, + * returns the closest value that is less than the argument. + * + * @tparam _ToDur The `duration` type to use for the result. + * @param __t A time point. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template<typename _ToDur, typename _Clock, typename _Dur> - constexpr + [[nodiscard]] constexpr enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> floor(const time_point<_Clock, _Dur>& __tp) { @@ -954,8 +1035,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION chrono::floor<_ToDur>(__tp.time_since_epoch())}; } + /** Convert a `time_point` to type `ToDur` and round up. + * + * The result is the same time point as measured by the same clock, but + * using the specified `duration` to represent the time. + * If the time point cannot be represented exactly in the result type, + * returns the closest value that is greater than the argument. + * + * @tparam _ToDur The `duration` type to use for the result. + * @param __t A time point. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template<typename _ToDur, typename _Clock, typename _Dur> - constexpr + [[nodiscard]] constexpr enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> ceil(const time_point<_Clock, _Dur>& __tp) { @@ -963,8 +1056,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION chrono::ceil<_ToDur>(__tp.time_since_epoch())}; } + /** Convert a `time_point` to type `ToDur` and round to the closest value. + * + * The result is the same time point as measured by the same clock, but + * using the specified `duration` to represent the time. + * If the time point cannot be represented exactly in the result type, + * returns the closest value, rounding ties to even. + * + * @tparam _ToDur The `duration` type to use for the result, + * which must have a non-floating-point `rep` type. + * @param __t A time point. + * @return The value of `__d` converted to type `_ToDur`. + * @since C++17 + */ template<typename _ToDur, typename _Clock, typename _Dur> - constexpr enable_if_t< + [[nodiscard]] constexpr + enable_if_t< __and_<__is_duration<_ToDur>, __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, time_point<_Clock, _ToDur>>
reply other threads:[~2023-03-28 23:35 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230328233521.1506A3889E25@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).