2011年5月12日 星期四

Typeless swap

Today, one of my colleagues ask me how to create a typeless swap marco,
This is the solution.

#define MySWAP(a, b) \
do { \
typeof (a) _temp = (a);\
(a) = (b); \
(b) = _temp; \
} while(false)

int main()
{
float a=1.33;
float b=2.75;
MySWAP(a,b);
}

沒有留言:

張貼留言