I have a template class with a read() method. It needs to call a C functions: read1(), read2(), read3() etc based upon the template parameter value. Is there is a cleaner way to do this than a switch on the template value? I have tried using a MACRO:
#define READ(N) read##N##()
But this doesn’t work as X is simply replaced by the C Preprocessor as ‘N’ resulting in “readN()”
int read1()
{
}
int read2()
{
}
int read3()
{
}
...
template <int N>
struct Reader
{
int read()
{
switch (N)
{
case 1:
return read1();
case 2:
return read2();
case 3:
return read3();
}
}
/* Doesn't work as X is simply replaced by the C Preprocessor as 'N' resulting in "readN()"
#define READ(X) read##X##()
int read()
{
return READ(N);
}
*/
};