From: Nobody on
"Barry Schwarz" <schwarzb(a)dqel.com> wrote in message
news:bpcmo5d4aolifsf2n627vgpmj6c8hjklbj(a)4ax.com...
> That was perfectly clear. The point I was trying to get you to see is
> that you are not creating a single token with the second ## operator.
> It serves no purpose. The open parenthesis will always be a
> stand-alone token. Removing the ## results in the exact same code
> after substitution.

Okay, I see what you and others meant. Thank you to all who responded.


From: Nobody on
I haven't used templates much, but the solution that you suggested is a
substitute for the macro, which is fine, but I think I still have to list
the line multiple times, which is what I am trying to avoid. Example:

func<0>(42, 84);
func<1>(42, 84);
func<2>(42, 84);

I can't use something like a loop construct, except by using Boost library,
or similar.


"Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
news:ucjdK4OuKHA.1796(a)TK2MSFTNGP02.phx.gbl...
Nobody wrote:
> I don't think template functions work in this case

Why not, pray tell? Have you tried it?


From: Igor Tandetnik on
Nobody wrote:
> I haven't used templates much, but the solution that you suggested is a
> substitute for the macro, which is fine, but I think I still have to list
> the line multiple times, which is what I am trying to avoid. Example:
>
> func<0>(42, 84);
> func<1>(42, 84);
> func<2>(42, 84);

Only if you want to actually make three calls in a row. You certainly don't have to declare each template instantiation separately.

> I can't use something like a loop construct, except by using Boost library,
> or similar.

Well, the code above could be written as

for (int i = 0; i < 3; ++i) {
func(i, 42, 84);
}


What are you really trying to achieve? Show a sample of your current code, with macros, and I'll show you how to do it with templates.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Nobody on
Here is a sample without Macros:

// Central function: Has 3 parameters, the first is sequence number
// This function does the real work.
int func(int seq, int para1,int para2)
{
// Some code
};

// "n" functions: Have 2 parameters. They call the central function.
int func0(int para1,int para2) { return func(0,para1,para2);};
int func1(int para1,int para2) { return func(1,para1,para2);};
int func2(int para1,int para2) { return func(2,para1,para2);};
int func3(int para1,int para2) { return func(3,para1,para2);};


With Macros(func stays the same as above), no Boost library use:

#define paster(n) int func##n(int para1,int para2) {return
func(n,para1,para2);}

paster(0);
paster(1);
paster(2);
paster(3);

Note how paster macro is used outside any function since it basically
expands to an entire function. There is no advantage for this macro except
it makes it easy to read, and I only need to change one line if I change
function names or data type.

When setting up the call back, I use another macro to fill an array with
function pointers. Example:

MYPROC ProcArray[APPMAX] = {0};

#define PasteProcArray(n) ProcArray[n] = (MYPROC) func##n; i++

void InitProcArray(void)
{
int i=0;

PasteInitProcArray(0);
PasteInitProcArray(1);
PasteInitProcArray(2);
PasteInitProcArray(3);

if (i!=APPMAX) {
// Part of the array was not defined
MessageBox(NULL,_T("Size error in InitProcArray."), NULL, MB_OK);
}
}

And the callback code:

int i, id;

for (i=0;i<APPMAX;i++) {
id = GetNextID()
SetCallBack(id, ProcArray[i]);
// id is not given to the callback, so different function
// pointers are required in this case.
}

"Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
news:OM1ae0PuKHA.4492(a)TK2MSFTNGP05.phx.gbl...
Nobody wrote:
> I haven't used templates much, but the solution that you suggested is a
> substitute for the macro, which is fine, but I think I still have to list
> the line multiple times, which is what I am trying to avoid. Example:
>
> func<0>(42, 84);
> func<1>(42, 84);
> func<2>(42, 84);

Only if you want to actually make three calls in a row. You certainly don't
have to declare each template instantiation separately.

> I can't use something like a loop construct, except by using Boost
> library,
> or similar.

Well, the code above could be written as

for (int i = 0; i < 3; ++i) {
func(i, 42, 84);
}


What are you really trying to achieve? Show a sample of your current code,
with macros, and I'll show you how to do it with templates.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925


From: Igor Tandetnik on
Nobody wrote:
> Here is a sample without Macros:
>
> // Central function: Has 3 parameters, the first is sequence number
> // This function does the real work.
> int func(int seq, int para1,int para2)
> {
> // Some code
> };
>
> // "n" functions: Have 2 parameters. They call the central function.
> int func0(int para1,int para2) { return func(0,para1,para2);};
> int func1(int para1,int para2) { return func(1,para1,para2);};
> int func2(int para1,int para2) { return func(2,para1,para2);};
> int func3(int para1,int para2) { return func(3,para1,para2);};

Like I said, you can replace all these declarations with one:

template <int seq>
int func(int para1, int para2) { return func(seq, para1, para2); }

> With Macros(func stays the same as above), no Boost library use:
>
> #define paster(n) int func##n(int para1,int para2) {return
> func(n,para1,para2);}
>
> paster(0);
> paster(1);
> paster(2);
> paster(3);

These you wouldn't need at all.

> When setting up the call back, I use another macro to fill an array with
> function pointers. Example:
>
> MYPROC ProcArray[APPMAX] = {0};
>
> #define PasteProcArray(n) ProcArray[n] = (MYPROC) func##n; i++

You'd still need this. Just replace func##n with func<n>
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: C++ Redistributables
Next: Force loading of DLL