I would like to provide a templated function, that varies its implementation (->specialization) according to the sizeof the template type.
Something similar to this (omi
Simply make an auxiliary class that takes the size as a template argument:
#include
#include
template
struct ByteSwapper { };
template<>
struct ByteSwapper<2> {
static unsigned short swap(unsigned short a) {
return 2 * a;
}
};
template
T byteswap(const T& a) {
return ByteSwapper::swap(a);
}
int main() {
unsigned short s = 5;
std::cout << byteswap(s) << std::endl;
unsigned int i = 7;
// std::cout << byteswap(i) << std::endl; // error
}