D
Dan Krantz
I have the following template to ensure that a given number (val) falls into
a range (between vmin & vmax):
template<typename T> T ForceNumericRange( const T& val, const T& vmin, const
T& vmax)
{
T retVal = val;
if ( retVal < vmin )
retVal = vmin;
else if ( retVal > vmax )
retVal = vmax;
return retVal;
}
Here's the call:
float comm;
....
comm = ForceNumericRange( comm, 0.0, 100.0);
The compiler (Visual Age C++ 5 under AIX 4.3) complains: The function
template parameter "T" has been deduced to have two values: "float" and
"double"
When I took the .0 off the constant parameters, it complained about T being
float & int. I solved the problem with this change:
comm = ForceNumericRange( comm, 0.0F, 100.0F);
but it's hokey, since I don't want the caller to have to explicitly specify
the type for the range constants.
Is there a way to declare the template such that the types of the 2nd and
3rd parameter are always deduced from the type of the first argument? I'm
pretty sure I could give parameters 2 & 3 a different type declaration, and
rely on runtime conversions between the different types, but I'd like this
resolved at compile time; also, that approach might fail if vmin was a
variable and vmax was a constant.
Any suggestions?
a range (between vmin & vmax):
template<typename T> T ForceNumericRange( const T& val, const T& vmin, const
T& vmax)
{
T retVal = val;
if ( retVal < vmin )
retVal = vmin;
else if ( retVal > vmax )
retVal = vmax;
return retVal;
}
Here's the call:
float comm;
....
comm = ForceNumericRange( comm, 0.0, 100.0);
The compiler (Visual Age C++ 5 under AIX 4.3) complains: The function
template parameter "T" has been deduced to have two values: "float" and
"double"
When I took the .0 off the constant parameters, it complained about T being
float & int. I solved the problem with this change:
comm = ForceNumericRange( comm, 0.0F, 100.0F);
but it's hokey, since I don't want the caller to have to explicitly specify
the type for the range constants.
Is there a way to declare the template such that the types of the 2nd and
3rd parameter are always deduced from the type of the first argument? I'm
pretty sure I could give parameters 2 & 3 a different type declaration, and
rely on runtime conversions between the different types, but I'd like this
resolved at compile time; also, that approach might fail if vmin was a
variable and vmax was a constant.
Any suggestions?