|
20 | 20 |
|
21 | 21 | #include <timezone_data.hpp> |
22 | 22 |
|
| 23 | +// Extended to test LWG-4257 "Stream insertion for chrono::local_time should be constrained" |
| 24 | + |
23 | 25 | using namespace std; |
24 | 26 | using namespace chrono; |
25 | 27 |
|
@@ -1067,9 +1069,44 @@ void test_unsigned_sys_time_format_after_LWG_4274() { |
1067 | 1069 | assert(s == "1970-01-01 00:00:00"); |
1068 | 1070 | } |
1069 | 1071 |
|
| 1072 | +template <typename T> |
| 1073 | +concept ostream_insertable = requires(ostream& o, const T& t) { o << t; }; |
| 1074 | + |
| 1075 | +template <typename Dur> |
| 1076 | +void check_stream_insertion_operator_for_duration() { |
| 1077 | + if constexpr (ostream_insertable<sys_time<Dur>>) { |
| 1078 | + ostringstream oss; |
| 1079 | + oss << sys_time<Dur>{}; |
| 1080 | + assert(oss.str() == "1970-01-01 00:00:00"); |
| 1081 | + } |
| 1082 | + |
| 1083 | + if constexpr (ostream_insertable<local_time<Dur>>) { |
| 1084 | + ostringstream oss; |
| 1085 | + oss << local_time<Dur>{}; |
| 1086 | + assert(oss.str() == "1970-01-01 00:00:00"); |
| 1087 | + } |
| 1088 | +} |
| 1089 | + |
| 1090 | +// Test based on example in LWG-4257 |
| 1091 | +void check_stream_insertion_operator() { |
| 1092 | + // operator<< is constrained such that it does not participate when underlying duration has floating-point rep |
| 1093 | + using ok_dur = duration<long long>; |
| 1094 | + using bad_dur = duration<double>; |
| 1095 | + |
| 1096 | + static_assert(ostream_insertable<sys_time<ok_dur>>); |
| 1097 | + static_assert(ostream_insertable<local_time<ok_dur>>); |
| 1098 | + check_stream_insertion_operator_for_duration<ok_dur>(); |
| 1099 | + |
| 1100 | + static_assert(!ostream_insertable<sys_time<bad_dur>>); |
| 1101 | + static_assert(!ostream_insertable<local_time<bad_dur>>); |
| 1102 | + check_stream_insertion_operator_for_duration<bad_dur>(); |
| 1103 | +} |
| 1104 | + |
1070 | 1105 | void test() { |
1071 | 1106 | test_unsigned_sys_time_format_after_LWG_4274(); |
1072 | 1107 |
|
| 1108 | + check_stream_insertion_operator(); |
| 1109 | + |
1073 | 1110 | test_parse_conversion_spec<char>(); |
1074 | 1111 | test_parse_conversion_spec<wchar_t>(); |
1075 | 1112 |
|
|
0 commit comments