|
Boost : |
Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-09-23 14:05:40
Phil Endecott wrote:
>
> template<typename V>
> V clamp ( V val, V lo, V hi );
>
> Like min & max, clamp has a single type template parameter;
> what are the implications of this? For example, if I pass a
> mixture of std::string and const char* arguments, what will
> happen? Ideally, (a) all combinations would compile, and (b)
> conversions from const char* to std::string will only happen
> when necessary, not unconditionally for all of the arguments.
Let's examine the implications:
std::string value("2");
clamp(value, "1", "3");
That results in the following code:
std::string const lo("1");
std::string const hi("2");
std::less<std::string> p;
return p(value, lo) ? lo : p(hi, value) ? value : hi;
Since the return type must match _val_ (or _middle_ ;-), it is not unreasonable to permit _lo_ and _hi_ to have different types:
template <class V, class L, class H>
V
clamp(V _value, L _low, H _high);
(I'll discuss pass-by-value later.)
Assuming the current implementation, that would result in the following code:
std::less<std::string> p;
std::string const low("1");
std::string const high("2");
return p(value, low)
? std::string("1")
: p(high, value) ? std::string("2") : value;
This code is the result because to std::less<T>::operator () requiring two T const & arguments.
An optimizer may recognize that it needn't create additional temporaries for the return value, resulting in this code:
std::less<std::string> p;
std::string const low ("1");
std::string const high ("2");
return p(value, low) ? low : p(high, value) ? high: value;
If the non-predicate overload didn't use std::less<V>, then the code would be:
return value < "1"
? std::string("1")
: "2" < value ? std::string("2") : value;
Since std::string provides < that works with char * directly, the comparisons don't require instantiating a std::string. The result is zero or one temporary versus always two.
Conclusion: The multiple-template-parameter version is better, but only if the non-predicate overload doesn't use std::less<V>.
As for pass-by-value versus references, std::string is an excellent argument against pass-by-value. However, without the extra template parameters, calling clamp(value, "1", "2") would still create temporary std::strings from "1" and "2" to bind to the std::string const & function parameters. With the extra template parameters, however, pass-by-reference-to-const is viable. (Of course call_traits<T>::param_type can be used to select the parameter type to use pass-by-value for appropriately small built-in types.)
_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk