c++ - Defining value traits for floating points -
while writing code in c++, want express notion component of type x, min value kminvalue
, max value kmaxvalue
. purpose, did like:
template <typename componenttype> struct comptraits { }; template <> struct comptraits<unsigned char> { typedef unsigned char componenttype; enum{ kminvalue = 0, kmaxvalue = 255 }; };
and, can refer comptraits<unsigned char>::kminvalue
. but, not able understand trick floating data types. please in defining same thing floats.
thanks in advance.
you can use std::numeric_limits
, instead of constants, if want kminvalue
, kmaxvalue
- can use this
c++03
template<> struct comptraits<float> { static const float kminvalue; static const float kmaxvalue; }; const float comptraits<float>::kminvalue = std::numeric_limits<float>::min(); const float comptraits<float>::kmaxvalue = std::numeric_limits<float>::max();
c++11
template<> struct comptraits<float> { static constexpr float kminvalue = std::numeric_limits<float>::min(); static constexpr float kmaxvalue = std::numeric_limits<float>::max(); };
for case should use
template<> struct comptraits<float> { static const float kminvalue = 0.0f; static const float kmaxvalue = 1.0f; };
Comments
Post a Comment