Date: Fri, 7 Mar 2025 07:49:51 +0400
Lets say programmer want to create timeout (seconds) and want to use max()
as default (means never). Obvious (and the obvious (and seemingly only) way
to try to do this):
#include <chrono>
void do_work(std::chrono::nanoseconds timeout) {
/* do smth */
}
int main() {
std::chrono::seconds my_timeout(std::chrono::seconds::max());
do_work(my_timeout);
}
Turns out, it IS undefined behavior. Where? In IMPLICIT conversation from
seconds to nanoseconds! Timeout in 'do_work' is garbage now!
https://godbolt.org/z/rjqoWr9q4
For me its obvious, that its standard library bug and standard must say
what is expected behavior for this
as default (means never). Obvious (and the obvious (and seemingly only) way
to try to do this):
#include <chrono>
void do_work(std::chrono::nanoseconds timeout) {
/* do smth */
}
int main() {
std::chrono::seconds my_timeout(std::chrono::seconds::max());
do_work(my_timeout);
}
Turns out, it IS undefined behavior. Where? In IMPLICIT conversation from
seconds to nanoseconds! Timeout in 'do_work' is garbage now!
https://godbolt.org/z/rjqoWr9q4
For me its obvious, that its standard library bug and standard must say
what is expected behavior for this
Received on 2025-03-07 03:50:05