Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -5294,7 +5294,9 @@ namespace chrono {
}

_EXPORT_STD template <class _CharT, class _Traits, class _Duration>
basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& _Os, const local_time<_Duration>& _Val) {
basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& _Os, const local_time<_Duration>& _Val)
requires requires { _Os << sys_time<_Duration>{_Val.time_since_epoch()}; }
{
return _Os << sys_time<_Duration>{_Val.time_since_epoch()};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <timezone_data.hpp>

// Extended to test LWG-4257 "Stream insertion for chrono::local_time should be constrained"

using namespace std;
using namespace chrono;

Expand Down Expand Up @@ -1061,7 +1063,42 @@ void test_locale() {
assert(stream(year_month_weekday_last{2021y / May / Tuesday[last]}) == STR("2021/Mai/Di[last]"));
}

template <typename T>
concept ostream_insertable = requires(ostream& o, const T& t) { o << t; };

template <typename Dur>
void check_stream_insertion_operator_for_duration() {
if constexpr (ostream_insertable<sys_time<Dur>>) {
ostringstream oss;
oss << sys_time<Dur>{};
assert(oss.str() == "1970-01-01 00:00:00");
}

if constexpr (ostream_insertable<local_time<Dur>>) {
ostringstream oss;
oss << local_time<Dur>{};
assert(oss.str() == "1970-01-01 00:00:00");
}
}

// Test based on example in LWG-4257
void check_stream_insertion_operator() {
// operator<< is constrained such that it does not participate when underlying duration has floating-point rep
using ok_dur = duration<long long>;
using bad_dur = duration<double>;

static_assert(ostream_insertable<sys_time<ok_dur>>);
static_assert(ostream_insertable<local_time<ok_dur>>);
check_stream_insertion_operator_for_duration<ok_dur>();

static_assert(!ostream_insertable<sys_time<bad_dur>>);
static_assert(!ostream_insertable<local_time<bad_dur>>);
check_stream_insertion_operator_for_duration<bad_dur>();
}

void test() {
check_stream_insertion_operator();

test_parse_conversion_spec<char>();
test_parse_conversion_spec<wchar_t>();

Expand Down