C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Chrono API provokes errors

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Fri, 7 Mar 2025 05:49:36 +0000
It's not an error, it's an overflow.

You are trying to put a larger thing into a smaller thing, it won't fit. And this larger thing is implantation defined on purpose.
Something must give.

You can only make it somewhat more well-behaved if you say that the conversion saturates. Which is better than nothing, it would still not make things portable.


________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of Nikl Kelbon via Std-Proposals <std-proposals_at_[hidden]>
Sent: Friday, March 7, 2025 4:50:12 AM
To: std-proposals <std-proposals_at_[hidden]>
Cc: Nikl Kelbon <kelbonage_at_[hidden]>
Subject: [std-proposals] Chrono API provokes errors

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


Received on 2025-03-07 05:49:38