macro definition for different compile configurations

T

thomas

Hi,
I'm stucked by something appears really simple at first sight.

I have a function definition with different parameter list in different compile configurations. for example.

--------------------------
CONF1:
int f(int x, int y){}
int g(int x){}

CONF2:
int f(int y){}
int g(){}
----------------------------
I'm trying to define some marco like

#if CONF1
#define PARAM(list) int x, list
#elseif CONF2
#define PARAM(list) list
#endif

But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.

It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.

thanks.

tom
 
V

Victor Bazarov

Hi,
I'm stucked by something appears really simple at first sight.

I have a function definition with different parameter list in different compile configurations. for example.

--------------------------
CONF1:
int f(int x, int y){}
int g(int x){}

CONF2:
int f(int y){}
int g(){}

Why don't your functions declared returning a value return a value? Are
those supposed to be definitions or declarations?

You *do* know that in C++ you're allowed to have functions with the same
name and different argument list, right? That's called "overloading".
You *can* declare/define your 'f' and 'g' all in the same scope.
----------------------------
I'm trying to define some marco like

#if CONF1
#define PARAM(list) int x, list
#elseif CONF2
#define PARAM(list) list
#endif

And use it HOW? To achieve WHAT?
But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.

"Cannot work" for what?
It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.

What exactly are you *trying to do*? You're stating that your solution
is not working, but what is *the problem* that you're trying to solve?
(And please don't tell us that the problem is that the solution is not
working)

V
 
T

thomas

Ok...

#ifdef _A_
#define PARAM_WITH_COMMA int x,
#defien PARAM_NO_COMMA int x
#else
#define PARAM_WITH_COMMA
#define PARAM_NO_COMMA
#endif

And I have two versions of following functions
#ifdef _A_
int f(int x, int y);
int f(int x);
#else
int f(int y);
int f();
#endif

And I can write the two versions of the functions with the following declaration.
int f(PARAM_WITH_COMMA int y);
int f(PARAM_NO_COMMA);

In short I want to make my code neat and easy to read, that's to say, fewer "#ifdef" like stuff.

So I'm trying to write some macros to shorten some word usage.

I just gave an example above using two marco definitions.

Can I use just one macro to achieve the same thing?
 
Q

Qi


Please top quote. And keep your line width within 76
characters.
And I can write the two versions of the functions with the following declaration.
int f(PARAM_WITH_COMMA int y);
int f(PARAM_NO_COMMA);

In short I want to make my code neat and easy to read, that's to say, fewer "#ifdef" like stuff.

So I'm trying to write some macros to shorten some word usage.

Using macro is neither neat nor easy to read.
Whenever you can avoid macros, just avoid them.

You are using C++. C++ can do most stuff that macro can do.
When you want to write a macro, rethink again.

I just gave an example above using two marco definitions.

Can I use just one macro to achieve the same thing?

There is comma_if in Boost library. You may check Boost
preprocessor library.
 
T

thomas

Hi,
I'm stucked by something appears really simple at first sight.

I have a function definition with different parameter list in different compile configurations. for example.

--------------------------
CONF1:
int f(int x, int y){}
int g(int x){}

CONF2:
int f(int y){}
int g(){}
----------------------------
I'm trying to define some marco like

#if CONF1
#define PARAM(list) int x, list
#elseif CONF2
#define PARAM(list) list
#endif

But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.

It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.

thanks.

tom

Seems that you don't understand what I am talking about, just judging by some keyword mentioned.
 
G

Geoff

Seems that you don't understand what I am talking about, just judging by some keyword mentioned.

Try replying to the people your post is intended to reply to instead
of yourself.

You have not clearly stated your problem.

If you are coding C++ you do not need to write macros to define your
functions using compile time switches. You can simply define your
functions simultaneously in the same scope and let the compiler choose
which overloaded function to use based on the function call.

Defining:

int f(int x, int y){}
int f(int x){}
int f(){}

Calling:

i = f(x,y);
i = f(x);
i = f();

The compiler will choose the proper function that matches the
arguments in the function call.
 
J

Juha Nieminen

Qi said:
C++ can do most stuff that macro can do.

Not everything, though. For instance, there's a reason why assert()
is still a macro in C++. (Because it typically prints the file name
and line number where the assertion failed, plus the expression that
failed, that's basically impossible to achieve without a preprocessor
macro. Even if there was a way to get the line number otherwise (which
there isn't), you still couldn't stringify the expression without the
preprocessor.)
 
S

Stuart Redmann

On March, 27th, Geoff wrote:

[snip]
Try replying to the people your post is intended to reply to instead
of yourself.

[snip]

Gosh, it took me almost one minute to figure out what your sentence
actually means. I take it from your profile that you are a native
speaker, so I assume that such sentences are nothing strange. Still, I
wonder whether this is considered good English or not. A comma would
be a great hint for non-native speakers, but I guess that your
sentence is grammatically correct.

Regards,
Stuart
 
G

Geoff

On March, 27th, Geoff wrote:

[snip]
Try replying to the people your post is intended to reply to instead
of yourself.

[snip]

Gosh, it took me almost one minute to figure out what your sentence
actually means. I take it from your profile that you are a native
speaker, so I assume that such sentences are nothing strange. Still, I
wonder whether this is considered good English or not. A comma would
be a great hint for non-native speakers, but I guess that your
sentence is grammatically correct.

Stuart,
I pumped it through LibreOffice just to see if it would flag the
grammar, it did not.

However, I suppose it could have been phrased better and more
formally:

"You should reply and quote the people to whom you are intending to
reply, rather than replying to yourself."

Better?
 
S

Stuart Redmann

On March, 27th, Geoff wrote:
Try replying to the people your post is intended to reply to instead
of yourself.

Gosh, it took me almost one minute to figure out what your sentence
actually means. I take it from your profile that you are a native
speaker, so I assume that such sentences are nothing strange. Still, I
wonder whether this is considered good English or not. A comma would
be a great hint for non-native speakers, but I guess that your
sentence is grammatically correct.

Stuart,
I pumped it through LibreOffice just to see if it would flag the
grammar, it did not.

However, I suppose it could have been phrased better and more
formally:

"You should reply and quote the people to whom you are intending to
reply, rather than replying to yourself."

Better?

Still looks weird to me.

"You should reply to the people to whom you are intending to reply,
rather than replying to yourself." looks somewhat better, even though
it may not be grammatically correct. Besides, it does not contain the
quoting part. Maybe like this:"You should reply to and quote the
people to whom you are intending to reply, rather than replying to
yourself."

Would that be OK?

Thanks,
Stuart
 
I

Ian Collins

"You should reply to the people to whom you are intending to reply,
rather than replying to yourself." looks somewhat better, even though
it may not be grammatically correct. Besides, it does not contain the
quoting part. Maybe like this:"You should reply to and quote the
people to whom you are intending to reply, rather than replying to
yourself."

Would that be OK?

Or simply direct the OP to

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
 
G

Geoff

On 27 Mrz., Ian Collins wrote:

[snip]

Acknowledged. However, I was intending to improve my English.



In that matter, I'd rather say
"You should reply to the people whom you are intending to reply to,
[...]"
than
"You should reply to the people to whom you are intending to reply,
[...]"

Would that still be correct?

Regards,
Stuart

I have to say the latter is more correct. putting the 'to' after
'reply' is splitting the infinitive. The 'to' belongs to 'whom' and
not to the verb 'reply'. But it is also more formal. You were correct
when you called me out on the 'to reply to'. You should probably also
discard the 'to the people'.

"You should reply to whom you are intending to reply, rather than
replying to yourself."
 
J

Juha Nieminen

Geoff said:
"You should reply and quote the people to whom you are intending to
reply, rather than replying to yourself."

"Rather than replying to yourself, you should reply to the people you
are intending to reply. It makes it clearer who you are talking to."
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top