最終更新日時(UTC):
が更新

履歴 編集

function template
<numeric>

std::div_sat(C++26)

namespace std {
  template<class T>
    constexpr T div_sat(T x, T y) noexcept;
}

概要

飽和除算 x / y を計算する。

テンプレートパラメータ制約

Tは符号付き整数型または符号無し整数型であること。

事前条件

y != 0

戻り値

例外

投げない

定数式に評価される条件

事前条件を満たすこと

#include <cstdint>
#include <numeric>
#include <print>

int main()
{
  // 10 / 3 = 3
  std::println("{}", std::div_sat(10, 3));

  // -128 * -1 = 128 -> 127(2**7-1)
  std::int8_t x = -128, y = -1;
  std::println("{}", std::div_sat(x, y));
}

出力

3
127

バージョン

言語

  • C++26

処理系

参照