2

I want to create a wrap for a bunch of overloaded functions

void print(int i);
void print(long i);
void print(char* s);
...

void myPrint(int x, int y, ??? toPrint){
    moveTo(x,y);
    print(toPrint);
}

How can I do it? Is it possible to go without templates?

Xun Yang
  • 4,209
  • 8
  • 39
  • 68
  • 2
    What's wrong with templates? – IronMensan Mar 29 '13 at 18:37
  • That's a classic example of a template function. Is there a reason you want to forgo that language feature? Can `toPrint` be any data type **at all**, or just `int`, `long`, and `char`? – Drew Dormann Mar 29 '13 at 18:37
  • I'm coding for a microcontroller(Arduino), template doesn't seem to be a default feature... – Xun Yang Mar 29 '13 at 18:38
  • Can't you make the argument global and then use it in your function myPrint() with out having to receive it through parameter's list? – Arslan Ali Mar 29 '13 at 18:38
  • @Arslan Sorry, forgot to mention it, the function is actually a class method so globals wouldn't do very well :) Thanks anyways! – Xun Yang Mar 29 '13 at 18:50

3 Answers3

2

If you cannot use templates, you can take the same approach with myPrint that you did with print.

void myPrint(int x, int y, int toPrint){
    moveTo(x,y);
    print(toPrint);
}

void myPrint(int x, int y, long toPrint){
    moveTo(x,y);
    print(toPrint);
}

void myPrint(int x, int y, char* toPrint){
    moveTo(x,y);
    print(toPrint);
}

Since you're doing embedded coding, you could also excuse yourself for using a macro.

#define MYPRINT( x, y, toPrint ) \
    do {                         \
        moveTo(x,y);             \
        print(toPrint);          \
    } while(false)
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Thanks @Drew, this seems to be the coolest! :) But can you do it without the do...while(false)? – Xun Yang Mar 29 '13 at 18:49
  • 1
    @XunYang Happy to help! You can omit the do/while if you're careful. This link explains why I am using it. http://stackoverflow.com/questions/154136 – Drew Dormann Mar 29 '13 at 18:52
2

I suppose you could write three individual wrappers. It's equivalent to what the template would produce (i.e. a bit of code bloat).

void myPrint(int x, int y, int toPrint) {
    moveTo(x,y);
    print(toPrint);
}

void myPrint(int x, int y, long toPrint) {
    moveTo(x,y);
    print(toPrint);
}

void myPrint(int x, int y, char *toPrint) {
    moveTo(x,y);
    print(toPrint);
}

I don't necessarily recommend this, since code-hiding macros are greatly frowned upon these days, but you could use a preprocessor macro as a template replacement.

#define DEFINE_MY_PRINT(Type)                  \
    void myPrint(int x, int y, Type toPrint) { \
        moveTo(x,y);                           \
        print(toPrint);                        \
    }

DEFINE_MY_PRINT(int)
DEFINE_MY_PRINT(long)
DEFINE_MY_PRINT(char *)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

Templates would be preferred, but you can (ab)use the preprocessor:

#define MY_PRINT(T) void myPrint(int x, int y, T toPrint) \
{ \
    moveTo(x,y); \
    print(toPrint); \
}

MY_PRINT(int)
MY_PRINT(long)
MY_PRINT(char*)
IronMensan
  • 6,761
  • 1
  • 26
  • 35