|
|
In article <397c5123@news.povray.org> , Warp <war### [at] tagpovrayorg> wrote:
> Are you sure? How do you make this extremely simple template in C:
>
> template<typename T>
> void swap(T& x, T& y)
> {
> T tmp = x;
> x = y;
> y = tmp;
> }
I would do it with include files that are not protected from
multiple-inclusion. It could look like this:
FILE: mytmpl.h
void TEMPLATE_swap(T& x, T& y);
void TEMPLATE_swap(TEMPLATE_T& x, TEMPLATE_T& y)
// OK, there are no references in C...
{
TEMPLATE_T tmp = x;
x = y;
y = tmp;
}
FILE: mymain.c
#define T int
#define TEMPLATE_swap swap1
#include "mytmpl.h"
#undef TEMPLATE_swap
#undef T
#define T char *
#define TEMPLATE_swap swap2
#include "mytmpl.h"
#undef TEMPLATE_swap
#undef T
While this is more work than just two functions, it does exactly the same.
However, if you have complex "classes" (structs) you only have to create a
new class name but can reuse the code without having to rename each
function.
Thorsten
____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde
Visit POV-Ray on the web: http://mac.povray.org
Post a reply to this message
|
|