Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

macro definition for different compile configurations

44 views
Skip to first unread message

thomas

unread,
Mar 27, 2012, 3:29:11 AM3/27/12
to
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

Victor Bazarov

unread,
Mar 27, 2012, 7:58:08 AM3/27/12
to
On 3/27/2012 3:29 AM, thomas wrote:
> 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
--
I do not respond to top-posted replies, please don't ask

thomas

unread,
Mar 27, 2012, 9:44:33 AM3/27/12
to
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?

Qi

unread,
Mar 27, 2012, 9:53:57 AM3/27/12
to
On 2012-3-27 21:44, thomas wrote:
> Ok...

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.


--
WQ

thomas

unread,
Mar 27, 2012, 10:05:35 AM3/27/12
to
On Tuesday, March 27, 2012 3:29:11 PM UTC+8, thomas wrote:
Seems that you don't understand what I am talking about, just judging by some keyword mentioned.

Geoff

unread,
Mar 27, 2012, 12:49:09 PM3/27/12
to
On Tue, 27 Mar 2012 07:05:35 -0700 (PDT), thomas
<fresh...@gmail.com> wrote:

>On Tuesday, March 27, 2012 3:29:11 PM UTC+8, thomas wrote:
>> Hi,
>> I'm stucked by something appears really simple at first sight.
>>
>
>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.

Juha Nieminen

unread,
Mar 27, 2012, 1:31:10 PM3/27/12
to
Qi <n...@no.com> wrote:
> 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.)

Stuart Redmann

unread,
Mar 27, 2012, 2:27:14 PM3/27/12
to
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

Geoff

unread,
Mar 27, 2012, 2:46:52 PM3/27/12
to
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?

Stuart Redmann

unread,
Mar 27, 2012, 3:39:39 PM3/27/12
to
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

Ian Collins

unread,
Mar 27, 2012, 3:49:22 PM3/27/12
to
On 03/28/12 08:39 AM, Stuart Redmann wrote:
>
> "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

--
Ian Collins

Stuart Redmann

unread,
Mar 27, 2012, 3:58:21 PM3/27/12
to
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

Geoff

unread,
Mar 27, 2012, 6:50:49 PM3/27/12
to
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."

thomas

unread,
Mar 27, 2012, 9:00:53 PM3/27/12
to
Well, I know that overloading can make the same function name work for different parameter list. But I want to see different functions for different configurations. If I define both, it's confusing which is used when. Sometimes the parameter in difference is just meaningness in some configurations.

Juha Nieminen

unread,
Mar 28, 2012, 4:14:22 AM3/28/12
to
Geoff <ge...@invalid.invalid> wrote:
> "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."

Stuart Redmann

unread,
Mar 28, 2012, 10:30:52 AM3/28/12
to
The clearest so far.


I'm still thinking about which kind of trophy I should award. Were
this an MSDN forum, I could give you a positive vote, but I guess that
this topic would never have been approved by MS.

Regards,
Stuart
0 new messages