|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4400" status="New"> |
| 5 | +<title>`enable_nonlocking_formatter_optimization` for durations with custom rep</title> |
| 6 | +<section><sref ref="[time.format]"/></section> |
| 7 | +<submitter>Tomasz Kamiński</submitter> |
| 8 | +<date>02 Oct 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +Currently the `enable_nonlocking_formatter_optimization` is enabled for |
| 14 | +<tt>duration<Rep, Ratio></tt> if it is enabled for `Rep`. |
| 15 | +</p> |
| 16 | +<blockquote><pre> |
| 17 | +template<class Rep, class Period> |
| 18 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::duration<Rep, Period>> |
| 19 | + = enable_nonlocking_formatter_optimization<Rep>; |
| 20 | +</pre></blockquote> |
| 21 | +<p> |
| 22 | +However, this does not take into the consideration that for custom `Rep` types, the arithmetic |
| 23 | +operations on `Rep` may also lock the stream leading to deadlock (for example log on overflow). |
| 24 | +Since they are required to handle the specifiers such as `%S` we should specialize |
| 25 | +`enable_nonlocking_formatter_optimization` only for built-in types: |
| 26 | +</p> |
| 27 | +<blockquote><pre> |
| 28 | +template<class Rep, class Period> |
| 29 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::duration<Rep, Period>> |
| 30 | + = is_arithmetic_v<Rep>; |
| 31 | +</pre></blockquote> |
| 32 | +<p> |
| 33 | +Furtheremore, for all types that are currently templated on `Duration` (`hh_mm_ss`, `sys_time`, |
| 34 | +`local_time`, etc.), we `enable_nonlocking_formatter_optimization` by default. This again does |
| 35 | +not take into consideration the arithmetic operations performed as duration. We should specialize |
| 36 | +`enable_nonlocking_formatter_optimization` for all of them to be enabled if |
| 37 | +`enable_nonlocking_formatter_optimization` is enabled for `duration`: |
| 38 | +</p> |
| 39 | +<blockquote><pre> |
| 40 | +template<class Duration> |
| 41 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::hh_mm_ss<Duration>> |
| 42 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 43 | +</pre></blockquote> |
| 44 | +<p> |
| 45 | +Note, that forwarding to `enable_nonlocking_formatter_optimization` on `Duration` instead of |
| 46 | +checking `Duration::rep` allows users to specialize `enable_nonlocking_formatter_optimization` for |
| 47 | +Durations with there custom representation types. |
| 48 | +<p/> |
| 49 | +The proposed wording has recently been implemented in |
| 50 | +<a href="https://gcc.gnu.org/pipermail/libstdc++/2025-October/063758.html">gcc's libstdc++</a>. |
| 51 | +</p> |
| 52 | +</discussion> |
| 53 | + |
| 54 | +<resolution> |
| 55 | +<p> |
| 56 | +This wording is relative to <paper num="N5014"/>. |
| 57 | +</p> |
| 58 | + |
| 59 | +<ol> |
| 60 | + |
| 61 | +<li><p>Modify <sref ref="[time.format]"/> as indicated:</p> |
| 62 | + |
| 63 | +<blockquote> |
| 64 | +<p> |
| 65 | +-8- For `chrono::duration`<ins>, `chrono::hh_mm_ss`, `chrono::sys_time`, `chrono::utc_time`, |
| 66 | +`chrono::tai_time`, `chrono::gps_time`, `chrono::file_time`, `chrono::local_time`, |
| 67 | +<tt>chrono::<i>local-time-format-t</i></tt>, and `chrono::zoned_time`</ins> the library only |
| 68 | +provides the following specialization<ins>s</ins> of `enable_nonlocking_formatter_optimization`: |
| 69 | +</p> |
| 70 | +<blockquote> |
| 71 | +<pre> |
| 72 | +template<class Rep, class Period> |
| 73 | + constexpr bool enable_nonlocking_formatter_optimization< |
| 74 | + chrono::duration<Rep, Period>> = |
| 75 | + <del>enable_nonlocking_formatter_optimization</del><ins>is_arithmetic_v</ins><Rep>; |
| 76 | + |
| 77 | +<ins>template<class Duration> |
| 78 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::hh_mm_ss<Duration>> |
| 79 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 80 | + |
| 81 | +template<class Duration> |
| 82 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::sys_time<Duration>> |
| 83 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 84 | + |
| 85 | +template<class Duration> |
| 86 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::utc_time<Duration>> |
| 87 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 88 | + |
| 89 | +template<class Duration> |
| 90 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::tai_time<Duration>> |
| 91 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 92 | + |
| 93 | +template<class Duration> |
| 94 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::gps_time<Duration>> |
| 95 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 96 | + |
| 97 | +template<class Duration> |
| 98 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::file_time<Duration>> |
| 99 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 100 | + |
| 101 | +template<class Duration> |
| 102 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::local_time<Duration>> |
| 103 | + = enable_nonlocking_formatter_optimization<Duration>; |
| 104 | + |
| 105 | +template<class Duration> |
| 106 | + constexpr bool enable_nonlocking_formatter_optimization<chrono::<i>local-time-format-t</i><Duration>> |
| 107 | + = enable_nonlocking_formatter_optimization<Duration>;</ins> |
| 108 | +</pre> |
| 109 | +</blockquote> |
| 110 | +<p> |
| 111 | +<del>-9- For `chrono::zoned_time` the library only provides the following specialization of |
| 112 | +`enable_nonlocking_formatter_optimization`:</del> |
| 113 | +</p> |
| 114 | +<blockquote> |
| 115 | +<pre> |
| 116 | +template<class Duration> |
| 117 | + constexpr bool enable_nonlocking_formatter_optimization< |
| 118 | + chrono::zoned_time<Duration, const std::chrono::time_zone*>> |
| 119 | + = <del>true</del><ins>enable_nonlocking_formatter_optimization<Duration></ins>; |
| 120 | +</pre> |
| 121 | +</blockquote> |
| 122 | +</blockquote> |
| 123 | +</li> |
| 124 | +</ol> |
| 125 | + |
| 126 | +</resolution> |
| 127 | + |
| 128 | +</issue> |
0 commit comments