I would like to perform similar but not identical calculations for several integer types (16, 32, 64 bits) and floating point types (float, double, long double). Most of the code is identical, but some parts must be executed differently for int and float. For example, an ints comparison can be done using a == b, while a float comparison should be done using abs (ab)
One way to do this is to isolate parts of the code that differ between int and float from small functions and specialize the template for each type. However, I would prefer not to copy the same code for each of the integer types and a different code for each of the float types. Thus, the question arises: is it possible to immediately specialize a template function for several types? Something semantically similar to the following if it was legal:
template<>
bool isEqual< short OR long OR long long >( T a, T b ) {
return a == b;
}
template<>
bool isEqual< float OR double OR long double >( T a, T b ) {
return abs( a - b ) < epsilon;
}
source
share