aboutsummaryrefslogtreecommitdiff
path: root/libphobos/src/std/algorithm/comparison.d
diff options
context:
space:
mode:
Diffstat (limited to 'libphobos/src/std/algorithm/comparison.d')
-rw-r--r--libphobos/src/std/algorithm/comparison.d20
1 files changed, 18 insertions, 2 deletions
diff --git a/libphobos/src/std/algorithm/comparison.d b/libphobos/src/std/algorithm/comparison.d
index b810fbb..5ecb4f6 100644
--- a/libphobos/src/std/algorithm/comparison.d
+++ b/libphobos/src/std/algorithm/comparison.d
@@ -577,14 +577,24 @@ Returns:
and `T3` are different.
*/
T1 clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
-if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
+if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val))
+ && (is(T2 : T1) && is(T3 : T1)))
+// cannot use :
+// `if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
+// because of https://issues.dlang.org/show_bug.cgi?id=16235.
+// Once that is fixed, we can simply use the ternary in both the template constraint
+// and the template body
in
{
assert(!lower.greaterThan(upper), "Lower can't be greater than upper.");
}
do
{
- return val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val;
+ if (val.lessThan(lower))
+ return lower;
+ else if (val.greaterThan(upper))
+ return upper;
+ return val;
}
///
@@ -637,6 +647,12 @@ do
assert(x.clamp(lo, hi).y == 42);
}
+// https://issues.dlang.org/show_bug.cgi?id=23268
+@safe pure nothrow @nogc unittest
+{
+ static assert(__traits(compiles, clamp(short.init, short.init, cast(const) short.init)));
+}
+
// cmp
/**********************************
Performs a lexicographical comparison on two