template<typename T>
T get_lim( const T & x)
{
return numeric_limits<T>::max();
}
the good thing is that you can use it without explicitly specifying T:
size_t l = get_lim(34);
template<typename T>
T get_lim( const T & x)
{
return numeric_limits<T>::max();
}
the good thing is that you can use it without explicitly specifying T:
size_t l = get_lim(34);
numeric_limits<typeof(a)>
works with GCC. (If you have it in standards-compliant mode, you may need to use __typeof__
instead.)
numeric_limits is what is known as a type trait. It stores information relative to a type, in an unobtrusive way.
Concerning your question, you can just define a template function that will determine the type of the variable for you.
template <typename T>
T valued_max( const T& v )
{
return numeric_limits<T>::max();
};
template <typename T>
T valued_min( const T& v )
{
return numeric_limits<T>::min();
};
or just create a small type returning structure:
template <typename T>
struct TypeOf
{
typedef T type;
};
template <typename T>
TypeOf<T> type_of( const T& v )
{
return TypeOf<T>();
}
int a;
numeric_limits<type_of(a)::type>::max();
Just FWIW, C++ 0x will also have decltype
, which is nearly the same as typeof
. They picked a new name primarily because the semantics are different in one case. The existing implementation of typeof
(gcc) drops references from types, so typeof(int &) == int
. The standard requires that decltype(int &) == int&
. This doesn't matter very often, but they decided to use a different name to prevent any silent changes to existing code.