C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal: Integer division/remainder/division-remainder functions with exception throwing behavior

From: Jan Schultke <janschultke_at_[hidden]>
Date: Wed, 30 Apr 2025 15:14:38 +0200
Even if checking for zero wasn't the most efficient way to implement it, a
compiler is allowed to transform

> if (divisor == 0) [[unlikely]] throw ...

into something more optimal. Maybe there is a way to handle the hardware
trap on division and recover from that with an exception throw. The cost of
a division is also relatively large, and I doubt that the zero-check has
much performance impact.

Anyhow, any proposal will obviously need some feedback from implementers,
performance measurements, etc. Until proven otherwise, checking for zero is
a perfectly valid solution already, and adding such a "div_with_exception"
function would be a waste of committee time.

Received on 2025-04-30 13:14:52