Is there a way to assign a variable of arbitrary type T
it's minimum or maximum value?
template <typename T>
void setMax(T& var){
var=MAXIMUM_OF_TYPE_T; //can this be done?
}
T toBeMaxed;
setMax(toBeMaxed);
In case that T
was int
, I could as well do
var=std::numeric_limits<int>::max();
instead.
If you are only dealing with types that have a specialization of std::numeric_limits<T>::max();
, you can implement your function as follows:
#include <limits>
template <typename T>
void setMax(T& var){
var=std::numeric_limits<T>::max();
}
int main() {
int intvar;
setMax(intvar);
float floatvar;
setMax(floatvar);
char charvar;
setMax(charvar);
}