Skip to content

Commit 3099153

Browse files
Perform stricter checks in chrono::is_clock_v (#5892)
Co-authored-by: Stephan T. Lavavej <stl@microsoft.com>
1 parent cf4bfd3 commit 3099153

File tree

2 files changed

+285
-69
lines changed

2 files changed

+285
-69
lines changed

stl/inc/__msvc_chrono.hpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,32 @@ namespace chrono {
5858
}
5959
};
6060

61+
_EXPORT_STD template <class _Rep, class _Period = ratio<1>>
62+
class duration;
63+
64+
_EXPORT_STD template <class _Clock, class _Duration = typename _Clock::duration>
65+
class time_point;
66+
6167
#if _HAS_CXX20
6268
_EXPORT_STD template <class _Clock>
6369
_NO_SPECIALIZATIONS_OF_VARIABLE_TEMPLATES constexpr bool is_clock_v = requires {
70+
// Basic checks from N5014 [time.traits.is.clock]/1
6471
typename _Clock::rep;
6572
typename _Clock::period;
6673
typename _Clock::duration;
6774
typename _Clock::time_point;
6875
_Clock::is_steady;
6976
_Clock::now();
77+
78+
// Additional stricter checks from N5014 [time.clock.req]/2
79+
// "An arithmetic type or a class emulating an arithmetic type" is not checked
80+
requires _Is_ratio_v<typename _Clock::period>;
81+
requires same_as<typename _Clock::duration, duration<typename _Clock::rep, typename _Clock::period>>;
82+
requires same_as<typename _Clock::time_point, time_point<_Clock>>
83+
|| same_as<typename _Clock::time_point,
84+
time_point<typename _Clock::time_point::clock, typename _Clock::duration>>;
85+
{ _Clock::is_steady } -> std::same_as<const bool&>;
86+
{ _Clock::now() } -> std::same_as<typename _Clock::time_point>;
7087
};
7188
_EXPORT_STD template <class _Clock>
7289
struct _NO_SPECIALIZATIONS_CITING("N5014 [time.traits.is.clock]/2") is_clock : bool_constant<is_clock_v<_Clock>> {};
@@ -84,9 +101,6 @@ namespace chrono {
84101
true;
85102
#endif // ^^^ !_HAS_CXX20 ^^^
86103

87-
_EXPORT_STD template <class _Rep, class _Period = ratio<1>>
88-
class duration;
89-
90104
template <class _Ty>
91105
constexpr bool _Is_duration_v = _Is_specialization_v<_Ty, duration>;
92106

@@ -203,7 +217,7 @@ namespace chrono {
203217
_Rep _MyRep; // the stored rep
204218
};
205219

206-
_EXPORT_STD template <class _Clock, class _Duration = typename _Clock::duration>
220+
_EXPORT_STD template <class _Clock, class _Duration>
207221
class time_point { // represents a point in time
208222
public:
209223
using clock = _Clock;

0 commit comments

Comments
 (0)