Macro parameter list

J

Jack Morgan

I got a macro like this in my code.#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))Obviously this doesn't work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?
 
A

ais523

Jack said:
I got a macro like this in my code.
#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}
And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7
Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))
Obviously this doesn't work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?

Here's an example of how to do it (with some trivial test code; this
has been tested):

#define PRINT3D(a,b,c) do {printf("%d%d%d\n",a,b,c);} while(0)
#define MACROCALL(x,y) x(y)
#define P3DARGS 1,2,3

#include <stdio.h>

int main(void)
{
MACROCALL(PRINT3D,P3DARGS);
return 0;
}

In many preprocessor problems, an extra level of macro expansion is all
that is needed. This shouldn't be too hard to adapt to your code.
 
A

Alf P. Steinbach

* Jack Morgan:
I got a macro like this in my code.
#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}
And I want to be able to do something like this
Have a generic paremeter list...
GENERIC 1,2,3,4,5,6,7
GENERICNAME(name) 1,2,3,4,5,name,7
Which then could be used like this.
DECLARE_SOMEWNDSTRUCTURE(GENERIC)
DECLARE_SOMEWNDSTRUCTURE(GENERICNAME("Jimbo"))
Obviously this doesn't work - warning C4003: not enough actual
parameters for macro...So could anybody explain how something like this
is done?

How about

#define DECLARE_NAMED(name) DECLARE_SOMEWNDSTRUCTURE(1,2,3,4,5,name,7)
#define DECLARE_DEFAULT DECLARE_NAMED( 6 )

But I guess this macro evilness is just a symptom of something more
sinister.

Technically you're trying to invent default macro arguments in order to
solve some problem. It's difficult to see what that problem is. But
let's say (correct me if I'm wrong) that the problem is

* How to provide default values for most values in a collection.

And one way to do that is to copy a default collection and simply change
the value or values that should not default, e.g.

ValueCollection defaultValues() { ... }

ValueCollection valuesWithName( std::string const& aName )
{
ValueCollection result = defaultValues();
result.name = aName;
return result;
}

Or it could be an override of a virtual function, like

ValueCollection Base::values() const { ... }

std::string Derived::name() const { return "abra kadabra"; }

ValueCollection Derived::values() const
{
ValueCollection result = Base::values();
result.name = name();
return result;
}

You could even go so far as to use one virtual function per value.
 
R

Ralph A. Moritz

First off, cross-posting is bad. My reply continues below.

Jack said:
I got a macro like this in my code.
#define DECLARE_SOMEWNDSTRUCTURE(style, nums, clrref, icon, tablenum,
name,
menuname)\
static SOMEWNDSTRUCT &GetSomeWndStruct()\
{\
static SOMEWNDSTRUCT somestruct = {\
style, bits, clrref, icon, tablenum, name, menuname};\
return somestruct ;\
}

There are a few mistakes here:

i. Every occurence of DECLARE_SOMEWNDSTRUCTURE will be replaced
by the function definition of GetSomeWndStruct. This is probably
not what you want.

ii. Putting an ampersand before the function name is a syntax
error in C. Valid identifiers must start with a letter or an
underscore.

iii. You are attempting to return a local variable.

You probably want something like the following. To be honest
though, I think this is mis-use of macros. This can all be done
much more clearly using functions. A good optimising compiler
will know when to generate inline code.

Regards,
Ralph

#include <stdio.h>
#include <stdlib.h>

struct foo {
int bar;
char *baz;
};

struct foo *tmpfoo001;

#define DECLARE_FOO(bar, baz) \
(tmpfoo001 = malloc(sizeof(struct foo))) \
? init_foo(bar, baz) \
: NULL

struct foo *init_foo(int bar, const char *baz)
{
tmpfoo001->bar = bar;
tmpfoo001->baz = baz;
return tmpfoo001;
}
 
J

John Vielerki

i. Every occurence of DECLARE_SOMEWNDSTRUCTURE will be replaced by
the function definition of GetSomeWndStruct. This is probably not what
you want.
Yea, that is what I wanted. ;)The Macros are just to allow the user from writting more code in their
derived class.
 
J

John Vielerki

Your code generates a error C4003: not enough actual parameters for
macro 'PRINT3D'
 
K

Keith Thompson

John Vielerki said:
Sry, I sometimes forget that not everyone uses Google :( Here you go.:

[ followup with context snipped ]

Thanks! But pleae don't snip the attribution lines; it can be helpful
to know who wrote what.
 
K

Keith Thompson

Mark McIntyre said:
There's around eight billion people in the world., I sincerely doubt
that most of them use google.

More like 6.5 billion, and that's not relevant to the claim that
"hardly anyone" uses Google, and who cares anyway?
 
J

Jack Morgan

My remarks were not to convey that I believe everyone uses
Google.....It was to convey that I am self absorbed and could give a
shit what they use. ;)
 
M

Mark McIntyre

More like 6.5 billion, and that's not relevant to the claim that
"hardly anyone" uses Google,

Its highly relevant. No matter how you work it, the percentage of the
worlds population that uses google isn't appreciable.

Or were you thinking of including only people who use usenet? Why?
and who cares anyway?

Quite :)
Mark McIntyre
 
D

Default User

Jack said:
My remarks were not to convey that I believe everyone uses
Google.....It was to convey that I am self absorbed and could give a
shit what they use. ;)

And we're supposed to care what you say?



Brian
 
J

Jack Morgan

And we're supposed to care what you say?
We? Are you a member of collective? ...I swear I hate people that feel
the need to pretend they are a group, like somehow their bs becomes
more meaningful.
And we're supposed to care what you say?
Could care less.
 
M

Mark McIntyre

We? Are you a member of collective?

We, thats as in the people that read this group.
Could care less.

Its worth considering thats its YOU that asked for help, and being
abusive to the people you asked is generalyl considered stupid.
Mark McIntyre
 
R

Rod Pemberton

Mark McIntyre said:
Its highly relevant. No matter how you work it, the percentage of the
worlds population that uses google isn't appreciable.

Or were you thinking of including only people who use usenet? Why?


Quite :)

Please, don't feed the troll.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top