Reputation: 55685
This is related to my previous question where I asked if std::chrono::steady_clock::now
should be noexcept
. Now that I know that it should I wonder how does this function report errors? For example, a common implementation of this function on Linux uses
clock_gettime which can return an error.
Upvotes: 4
Views: 993
Reputation: 219345
All of the errors that could be reported by the Linux clock_gettime
facility are not possible in a debugged implementation of std::chrono::steady_clock::now()
.
The errors are:
int clock_gettime(clockid_t clk_id, struct timespec *tp);
EFAULT
tp points outside the accessible address space.
There is no user-supplied tp
to get wrong in std::chrono::steady_clock::now()
. If std::chrono::steady_clock::now()
passes the wrong tp
to clock_gettime
, it is just a bug in steady_clock
that is easily fixed.
EINVAL
The clk_id specified is not supported on this system.
There is no user-supplied clk_id
to get wrong in std::chrono::steady_clock::now()
. If std::chrono::steady_clock::now()
passes the wrong clk_id
to clock_gettime
, it is just a bug in steady_clock
that must be fixed.
Note that an implementation of steady_clock
isn't guaranteed to be portable, and may have to be ported to any given platform and be tested for correctness. It might turn out that steady_clock
can not be implemented in terms of clock_gettime
on some platforms.
EPERM
clock_settime() does not have permission to set the clock indicated.
Not applicable to steady_clock
as there is no API to set the time.
Upvotes: 7
Reputation: 42594
It doesn't. There's no provision in the API for error reporting. So the Linux implementation must either handle errors internally or not return (e.g., via exit()
or std::terminate()
).
Upvotes: 1