- numeric[meta header]
- function template[meta id-type]
- std[meta namespace]
- cpp26[meta cpp]
namespace std {
template<class T>
constexpr T mul_sat(T x, T y) noexcept;
}飽和乗算 x * y を計算する。
Tは符号付き整数型または符号無し整数型であること。
- 無限の範囲で計算した値
x * yが型Tで表現可能ならば、x * yを返す - そうでないとき、型
Tで表現可能な最大値または最小値のうちx * yに近い方の値を返す
投げない
#include <cstdint>
#include <numeric>
#include <print>
int main()
{
// 2 * 3 = 6
std::println("{}", std::mul_sat(2, 3));
// 20 * 20 = 400 -> 255(2**8-1)
std::uint8_t n = 20;
std::println("{}", std::mul_sat(n, n));
// -128 * -1 = 128 -> 127(2**7-1)
std::int8_t x = -128, y = -1;
std::println("{}", std::mul_sat(x, y));
}- std::mul_sat[color ff0000]
6
255
127
- C++26
- Clang: ??
- GCC: ??
- ICC: ??
- Visual C++: ??