Suppose for the sake of this question that I am implementing betoh16(), etc., as methods in a C++ class called OpSys. While doing so I notice that all 12 or so of these functions are mostly identical, and I don't wish to duplicate so much source.
I could opt to do the following using the preprocessor...
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
#define BETOH_XX(nbits) \
U##nbits OpSys::betoh##nbits, U##nbits val) \
{ \
if (CPU_IS_BIG_ENDIAN) \
return val; \
\
return bswap##nbits(val); \
}
BETOH_XX(16)
BETOH_XX(32)
BETOH_XX(64)
... but I wonder if there is some way to accomplish the same thing with templates, where my calls would look something like
U16 hval = betoh<16>(beval);
I see a few other questions on SO concerning templates with constant values, but nothing that seems to employ concatenation of that templatized value to some adjacent text.
Is this possible?