For elaborate objects defining comparison, clamp() should return the leftmost object unless it falls outside the limits established by the other two parameters. Consider: 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); Also, clamp() needs to be constrained appropriately.
@andralex created dlang/phobos pull request #7634 "Fix Issue 21249 - clamp() is not stable and is not constrained" fixing this issue: - Fix Issue 21249 - clamp() is not stable and is not constrained https://github.com/dlang/phobos/pull/7634
dlang/phobos pull request #7634 "Fix Issue 21249 - clamp() is not stable and is not constrained" was merged into master: - a7d574bcba1e0104938c203615339f4fb081ed7b by Andrei Alexandrescu: Fix Issue 21249 - clamp() is not stable and is not constrained https://github.com/dlang/phobos/pull/7634