
Le 24/09/11 22:53, Andrew Sutton a écrit :
template< class T, class L, class U> typename common_type< T const&, L const&, U const& >::type clamp(T const& x, L const& lower, U const& upper) { return x< lower ? lower : upper< x ? upper : x; }
for the use of common_type (assuming it accepts 3 parameters, which I seem to remember it did; if not, just nest); and something like Can we please not define algorithms this way? It may be possible, but that doesn't mean its a good idea. I don't know how you could possibly prove that the algorithm preserves ordering (<) when the algorithm includes 5 possibly different types.
Thats not strictly true. I do know how you can prove it preserves ordering, but I'm not going to encourage the style.
Define it in terms of a single type and let conversions happen at the call type.
Hi, I think that it is reasonable to compare different types. One example is chrono::duration. You can compare durations with a different Period. It would be great if the proposed clamp function could be used also as in auto res = clamp(val, minutes(30), hours(10)); Other examples can be found in the Unit library. Best, Vicente