Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-09-26 05:40:42

2011/9/26 Christian Holmquist <c.holmquist_at_[hidden]>

> <snip>
> ..very lengthy complicated discussions
> </snip>
> Forgive my lack of understanding of the complicated common_type<A, B, C>
> scheme, but I would never dare to use boost's implementation of clamp if
> taking three types. I don't care much if such an algorithm can be described
> in any meaningful way, but if it can, clamp is not an optimal place for
> such
> an exercise.
> Sometimes types unfortunately have implicit conversion, and if the compiler
> finds them (I guess that what's common_type deduces?),I might end up with a
> bogus expression that unfortunately compiles.
> Since std::min/max takes one type (for good reasons), I don't see why clamp
> all of a sudden should be different.
> I would go for something like this...
> template<class T>
> T const & clamp(T const& x, T const& lo, T const &hi)
> {
> return std::min(std::max(x, lo), hi);
> }


I'd like to add my version of clamp declaration. It might be a compromise

template < class T, class L, class H >
T const& clamp( T const& x, L const& lo, H const& hi );

I mean, I propose a 3-type version, which assumes that T is the common type,
and doesn't deduce it. This way:
- clamp( "x", "asdf", std::string("zxcv") ) is illegal, so the problem of
comparing pointers is no more,
- clamp( std::string("x"), "asdf", "zxcv" ) can avoid the conversions,
- however, this proposition doesn't resolve any of Dave's concerns.


Boost list run by bdawes at, gregod at, cpdaniel at, john at