c++ - Using numeric_limits to default parameter values -
i have template statistics class 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 put default values of maximum , minimum allowable values, minimum value not same floating point , integer types.
normally can write
t min_val = numeric_limits<t>::isinteger ? numeric_limits<t>::min() : -numeric_limits<t>::max();
i have found can't use 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 way of achieving this?
you might want rethink design. trying limitstats
std::numeric_limits
doesn't provide?
don't replicate badness of design of std::numeric_limits
. example, std::numeric_limits<double>::min()
terribly misnamed. minimum double additive inverse of maximum double. std::numeric_limits
abuse of notation , abuse of templates. in opinion, of course.
your idea min
ill-formed. think default respect limitstats<unsigned int>
.
with defaults, range
invalid signed integers. unsigned ints replicates max
, assuming fix problem limitstats<unsigned int>::min
. floating point types, either invalid or replicates max
, depending on mean limitstats<floating_point_type>::min
.
does make sense allow default values? wouldn't have question if don't provide defaults , make default constructor private/unimplemented.
Comments
Post a Comment