mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 23:50:31 +03:00
Fix Issue 21249 - clamp() is not stable and is not constrained
This commit is contained in:
parent
9351e91fe4
commit
cae6379750
1 changed files with 10 additions and 1 deletions
|
@ -530,6 +530,7 @@ Returns:
|
||||||
|
|
||||||
*/
|
*/
|
||||||
auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
|
auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
|
||||||
|
if (is(typeof(max(min(val, upper), lower))))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
import std.functional : greaterThan;
|
import std.functional : greaterThan;
|
||||||
|
@ -537,7 +538,7 @@ in
|
||||||
}
|
}
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
return max(lower, min(upper, val));
|
return max(min(val, upper), lower);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -576,6 +577,14 @@ do
|
||||||
// UFCS style
|
// UFCS style
|
||||||
assert(Date(1982, 1, 4).clamp(Date.min, Date.max) == Date(1982, 1, 4));
|
assert(Date(1982, 1, 4).clamp(Date.min, Date.max) == Date(1982, 1, 4));
|
||||||
|
|
||||||
|
// Stability
|
||||||
|
struct A {
|
||||||
|
int x, y;
|
||||||
|
int opCmp(ref const A rhs) const { return (x > rhs.x) - (x < rhs.x); }
|
||||||
|
}
|
||||||
|
A x, lo, hi;
|
||||||
|
x.y = 42;
|
||||||
|
assert(x.clamp(lo, hi).y == 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmp
|
// cmp
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue