I’ve just dipped into limits.h by Microsoft. I tried to check what’s the return type for max() function and to my surprise I see something like this:
// TEMPLATE CLASS numeric_limits
template<class _Ty>
class numeric_limits
: public _Num_base
{ // numeric limits for arbitrary type _Ty (say little or nothing)
public:
static _Ty (__CRTDECL min)() _THROW0()
{ // return minimum value
return (_Ty(0));
}
static _Ty (__CRTDECL max)() _THROW0()
{ // return maximum value
return (_Ty(0));//EXACTLY THE SAME WHAT IN min<<------------------
}
//... other stuff
};
How is it possible that in both min and max return exactly the same? Does it mean that if I would write makeSanwich() return (_Ty(0)) it would make a sandwich for me? How is it possible that having this same code with just the function names different we are getting different results?
This is implemented through template specialization. Probably, you’ll find something like this elsewhere:
If you use
numeric_limits<int>the above specialization will be preferred over the general case. There are similar specializations forfloat,charetcetera.Only if none of these match, the general case will be selected. In that case, the compiler will try to construct an object of type
_Tyfrom the0constant. Why that is ever useful… I don’t know. I would rather have a compilation error instead.