I have a template statistics class that has range parameters.
template <typename T>
class limitStats
{
public:
limitStats(T mx, T min) :
max(mx),
min(mn),
range(mx-mn)
{;}
private:
const T max;
const T min;
const T range;
}
I would like to put default values of maximum and minimum allowable values, but the minimum value is not the same for floating point and integer types.
Normally I can write
T min_val = numeric_limits<T>::isinteger ? numeric_limits<T>::min() : -numeric_limits<T>::max();
I have found that I can’t use it as a default parameter
limitStats(T mx = std::numeric_limts<T>::max(),
T mn = numeric_limits<T>::isinteger ? numeric_limits<T>::min() : -numeric_limits<T>::max())
Is there a way of achieving something like this?
You might want to rethink your design. What are you trying to do with your
limitStatsthatstd::numeric_limitsdoesn’t provide?Don’t replicate the badness of the design of
std::numeric_limits. For example,std::numeric_limits<double>::min()is terribly misnamed. The minimum double is the additive inverse of the maximum double.std::numeric_limitsis an abuse of notation and an abuse of templates. In my opinion, of course.Your idea for
minis ill-formed. Think about your default with respect tolimitStats<unsigned int>.With the defaults, your
rangeis invalid for signed integers. For unsigned ints it replicatesmax, assuming you fix the problem withlimitStats<unsigned int>::min. For floating point types, it is either invalid or replicatesmax, depending on what you mean bylimitStats<floating_point_type>::min.Does it even make sense to allow default values? You wouldn’t even have this question if you simply don’t provide defaults and make the default constructor private/unimplemented.