Skip to content

Commit

Permalink
more constexpr
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Mar 7, 2023
1 parent 59a3dc9 commit e99fd71
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/safe_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ T add(T summand_1, T summand_2) {
* when `num == std::numeric_limits<T>::min()`.
*/
template <typename T>
std::enable_if_t<std::is_signed_v<T>, T> abs(T num) noexcept {
if (num == std::numeric_limits<T>::min()) {
return std::numeric_limits<T>::max();
T abs(T num) noexcept {
if constexpr (std::is_signed_v<T>) {
if (num == std::numeric_limits<T>::min())
return std::numeric_limits<T>::max();
return num < 0 ? -num : num;
}
return num < 0 ? -num : num;
return std::abs(num);
}

} // namespace Safe
Expand Down

0 comments on commit e99fd71

Please sign in to comment.